/** * struct ab8500_charger_interrupts - ab8500 interrupts * @name: name of the interrupt * @isr function pointer to the isr
*/ struct ab8500_charger_interrupts { char *name;
irqreturn_t (*isr)(int irq, void *data);
};
struct ab8500_charger_info { int charger_connected; int charger_online; int charger_voltage_uv; int cv_active; bool wd_expired; int charger_current_ua;
};
struct ab8500_charger_usb_state { int usb_current_ua; int usb_current_tmp_ua; enum ab8500_usb_state state; enum ab8500_usb_state state_tmp;
spinlock_t usb_lock;
};
struct ab8500_charger_max_usb_in_curr { int usb_type_max_ua; int set_max_ua; int calculated_max_ua;
};
/** * struct ab8500_charger - ab8500 Charger device information * @dev: Pointer to the structure device * @vbus_detected: VBUS detected * @vbus_detected_start: * VBUS detected during startup * @ac_conn: This will be true when the AC charger has been plugged * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC * charger is enabled * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB * charger is enabled * @vbat Battery voltage * @old_vbat Previously measured battery voltage * @usb_device_is_unrecognised USB device is unrecognised by the hardware * @autopower Indicate if we should have automatic pwron after pwrloss * @autopower_cfg platform specific power config support for "pwron after pwrloss" * @invalid_charger_detect_state State when forcing AB to use invalid charger * @is_aca_rid: Incicate if accessory is ACA type * @current_stepping_sessions: * Counter for current stepping sessions * @parent: Pointer to the struct ab8500 * @adc_main_charger_v ADC channel for main charger voltage * @adc_main_charger_c ADC channel for main charger current * @adc_vbus_v ADC channel for USB charger voltage * @adc_usb_charger_c ADC channel for USB charger current * @bm: Platform specific battery management information * @flags: Structure for information about events triggered * @usb_state: Structure for usb stack information * @max_usb_in_curr: Max USB charger input current * @ac_chg: AC charger power supply * @usb_chg: USB charger power supply * @ac: Structure that holds the AC charger properties * @usb: Structure that holds the USB charger properties * @regu: Pointer to the struct regulator * @charger_wq: Work queue for the IRQs and checking HW state * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals * @pm_lock: Lock to prevent system to suspend * @check_vbat_work Work for checking vbat threshold to adjust vbus current * @check_hw_failure_work: Work for checking HW state * @check_usbchgnotok_work: Work for checking USB charger not ok status * @kick_wd_work: Work for kicking the charger watchdog in case * of ABB rev 1.* due to the watchog logic bug * @ac_charger_attached_work: Work for checking if AC charger is still * connected * @usb_charger_attached_work: Work for checking if USB charger is still * connected * @ac_work: Work for checking AC charger connection * @detect_usb_type_work: Work for detecting the USB type connected * @usb_link_status_work: Work for checking the new USB link status * @usb_state_changed_work: Work for checking USB state * @attach_work: Work for detecting USB type * @vbus_drop_end_work: Work for detecting VBUS drop end * @check_main_thermal_prot_work: * Work for checking Main thermal status * @check_usb_thermal_prot_work: * Work for checking USB thermal status * @charger_attached_mutex: For controlling the wakelock
*/ struct ab8500_charger { struct device *dev; bool vbus_detected; bool vbus_detected_start; bool ac_conn; bool vddadc_en_ac; bool vddadc_en_usb; int vbat; int old_vbat; bool usb_device_is_unrecognised; bool autopower; bool autopower_cfg; int invalid_charger_detect_state; int is_aca_rid;
atomic_t current_stepping_sessions; struct ab8500 *parent; struct iio_channel *adc_main_charger_v; struct iio_channel *adc_main_charger_c; struct iio_channel *adc_vbus_v; struct iio_channel *adc_usb_charger_c; struct ab8500_bm_data *bm; struct ab8500_charger_event_flags flags; struct ab8500_charger_usb_state usb_state; struct ab8500_charger_max_usb_in_curr max_usb_in_curr; struct ux500_charger ac_chg; struct ux500_charger usb_chg; struct ab8500_charger_info ac; struct ab8500_charger_info usb; struct regulator *regu; struct workqueue_struct *charger_wq; struct mutex usb_ipt_crnt_lock; struct delayed_work check_vbat_work; struct delayed_work check_hw_failure_work; struct delayed_work check_usbchgnotok_work; struct delayed_work kick_wd_work; struct delayed_work usb_state_changed_work; struct delayed_work attach_work; struct delayed_work ac_charger_attached_work; struct delayed_work usb_charger_attached_work; struct delayed_work vbus_drop_end_work; struct work_struct ac_work; struct work_struct detect_usb_type_work; struct work_struct usb_link_status_work; struct work_struct check_main_thermal_prot_work; struct work_struct check_usb_thermal_prot_work; struct usb_phy *usb_phy; struct notifier_block nb; struct mutex charger_attached_mutex;
};
/* * Function for enabling and disabling sw fallback mode * should always be disabled when no charger is connected.
*/ staticvoid ab8500_enable_disable_sw_fallback(struct ab8500_charger *di, bool fallback)
{
u8 val;
u8 reg;
u8 bank;
u8 bit; int ret;
dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
if (is_ab8500(di->parent)) {
bank = 0x15;
reg = 0x0;
bit = 3;
} else {
bank = AB8500_SYS_CTRL1_BLOCK;
reg = AB8500_SW_CONTROL_FALLBACK;
bit = 0;
}
/* read the register containing fallback bit */
ret = abx500_get_register_interruptible(di->dev, bank, reg, &val); if (ret < 0) {
dev_err(di->dev, "%d read failed\n", __LINE__); return;
}
if (is_ab8500(di->parent)) { /* enable the OPT emulation registers */
ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); if (ret) {
dev_err(di->dev, "%d write failed\n", __LINE__); goto disable_otp;
}
}
if (fallback)
val |= (1 << bit); else
val &= ~(1 << bit);
/* write back the changed fallback bit value to register */
ret = abx500_set_register_interruptible(di->dev, bank, reg, val); if (ret) {
dev_err(di->dev, "%d write failed\n", __LINE__);
}
disable_otp: if (is_ab8500(di->parent)) { /* disable the set OTP registers again */
ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); if (ret) {
dev_err(di->dev, "%d write failed\n", __LINE__);
}
}
}
/** * ab8500_power_supply_changed - a wrapper with local extensions for * power_supply_changed * @di: pointer to the ab8500_charger structure * @psy: pointer to power_supply_that have changed. *
*/ staticvoid ab8500_power_supply_changed(struct ab8500_charger *di, struct power_supply *psy)
{ /* * This happens if we get notifications or interrupts and * the platform has been configured not to support one or * other type of charging.
*/ if (!psy) return;
/* * Sometimes the platform is configured not to support * USB charging and no psy has been created, but we still * will get these notifications.
*/ if (di->usb_chg.psy) {
sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL, "present");
}
if (connected) {
mutex_lock(&di->charger_attached_mutex);
mutex_unlock(&di->charger_attached_mutex);
/** * ab8500_charger_get_ac_voltage() - get ac charger voltage * @di: pointer to the ab8500_charger structure * * Returns ac charger voltage in microvolt (on success)
*/ staticint ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
{ int vch, ret;
/* Only measure voltage if the charger is connected */ if (di->ac.charger_connected) { /* Convert to microvolt, IIO returns millivolt */
ret = iio_read_channel_processed_scale(di->adc_main_charger_v,
&vch, 1000); if (ret < 0) {
dev_err(di->dev, "%s ADC conv failed\n", __func__); return ret;
}
} else {
vch = 0;
} return vch;
}
/** * ab8500_charger_ac_cv() - check if the main charger is in CV mode * @di: pointer to the ab8500_charger structure * * Returns ac charger CV mode (on success) else error code
*/ staticint ab8500_charger_ac_cv(struct ab8500_charger *di)
{
u8 val; int ret = 0;
/* Only check CV mode if the charger is online */ if (di->ac.charger_online) {
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_STATUS1_REG, &val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return 0;
}
if (val & MAIN_CH_CV_ON)
ret = 1; else
ret = 0;
}
return ret;
}
/** * ab8500_charger_get_vbus_voltage() - get vbus voltage * @di: pointer to the ab8500_charger structure * * This function returns the vbus voltage. * Returns vbus voltage in microvolt (on success)
*/ staticint ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
{ int vch, ret;
/* Only measure voltage if the charger is connected */ if (di->usb.charger_connected) { /* Convert to microvolt, IIO returns millivolt */
ret = iio_read_channel_processed_scale(di->adc_vbus_v,
&vch, 1000); if (ret < 0) {
dev_err(di->dev, "%s ADC conv failed\n", __func__); return ret;
}
} else {
vch = 0;
} return vch;
}
/** * ab8500_charger_get_usb_current() - get usb charger current * @di: pointer to the ab8500_charger structure * * This function returns the usb charger current. * Returns usb current in microamperes (on success) and error code on failure
*/ staticint ab8500_charger_get_usb_current(struct ab8500_charger *di)
{ int ich, ret;
/* Only measure current if the charger is online */ if (di->usb.charger_online) { /* Return microamperes */
ret = iio_read_channel_processed_scale(di->adc_usb_charger_c,
&ich, 1000); if (ret < 0) {
dev_err(di->dev, "%s ADC conv failed\n", __func__); return ret;
}
} else {
ich = 0;
} return ich;
}
/** * ab8500_charger_get_ac_current() - get ac charger current * @di: pointer to the ab8500_charger structure * * This function returns the ac charger current. * Returns ac current in microamperes (on success) and error code on failure.
*/ staticint ab8500_charger_get_ac_current(struct ab8500_charger *di)
{ int ich, ret;
/* Only measure current if the charger is online */ if (di->ac.charger_online) { /* Return microamperes */
ret = iio_read_channel_processed_scale(di->adc_main_charger_c,
&ich, 1000); if (ret < 0) {
dev_err(di->dev, "%s ADC conv failed\n", __func__); return ret;
}
} else {
ich = 0;
} return ich;
}
/** * ab8500_charger_usb_cv() - check if the usb charger is in CV mode * @di: pointer to the ab8500_charger structure * * Returns ac charger CV mode (on success) else error code
*/ staticint ab8500_charger_usb_cv(struct ab8500_charger *di)
{ int ret;
u8 val;
/* Only check CV mode if the charger is online */ if (di->usb.charger_online) {
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_USBCH_STAT1_REG, &val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return 0;
}
if (val & USB_CH_CV_ON)
ret = 1; else
ret = 0;
} else {
ret = 0;
}
return ret;
}
/** * ab8500_charger_detect_chargers() - Detect the connected chargers * @di: pointer to the ab8500_charger structure * @probe: if probe, don't delay and wait for HW * * Returns the type of charger connected. * For USB it will not mean we can actually charge from it * but that there is a USB cable connected that we have to * identify. This is used during startup when we don't get * interrupts of the charger detection * * Returns an integer value, that means, * NO_PW_CONN no power supply is connected * AC_PW_CONN if the AC power supply is connected * USB_PW_CONN if the USB power supply is connected * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
*/ staticint ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
{ int result = NO_PW_CONN; int ret;
u8 val;
/* Check for AC charger */
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_STATUS1_REG, &val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return ret;
}
if (val & MAIN_CH_DET)
result = AC_PW_CONN;
/* Check for USB charger */
if (!probe) { /* * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100 * when disconnecting ACA even though no * charger was connected. Try waiting a little * longer than the 100 ms of VBUS_DET_DBNC100...
*/
msleep(110);
}
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_USBCH_STAT1_REG, &val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return ret;
}
dev_dbg(di->dev, "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
val); if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
result |= USB_PW_CONN;
return result;
}
/** * ab8500_charger_max_usb_curr() - get the max curr for the USB type * @di: pointer to the ab8500_charger structure * @link_status: the identified USB type * * Get the maximum current that is allowed to be drawn from the host * based on the USB type. * Returns error code in case of failure else 0 on success
*/ staticint ab8500_charger_max_usb_curr(struct ab8500_charger *di, enum ab8500_charger_link_status link_status)
{ int ret = 0;
di->usb_device_is_unrecognised = false;
/* * Platform only supports USB 2.0. * This means that charging current from USB source * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_* * should set USB_CH_IP_CUR_LVL_0P5.
*/
switch (link_status) { case USB_STAT_STD_HOST_NC: case USB_STAT_STD_HOST_C_NS: case USB_STAT_STD_HOST_C_S:
dev_dbg(di->dev, "USB Type - Standard host is " "detected through USB driver\n");
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0; break; case USB_STAT_HOST_CHG_HS_CHIRP:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0; break; case USB_STAT_HOST_CHG_HS:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0; break; case USB_STAT_ACA_RID_C_HS:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P9;
di->is_aca_rid = 0; break; case USB_STAT_ACA_RID_A: /* * Dedicated charger level minus maximum current accessory * can consume (900mA). Closest level is 500mA
*/
dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 1; break; case USB_STAT_ACA_RID_B: /* * Dedicated charger level minus 120mA (20mA for ACA and * 100mA for potential accessory). Closest level is 1300mA
*/
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P3;
dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
di->max_usb_in_curr.usb_type_max_ua);
di->is_aca_rid = 1; break; case USB_STAT_HOST_CHG_NM:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
di->is_aca_rid = 0; break; case USB_STAT_DEDICATED_CHG:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P5;
di->is_aca_rid = 0; break; case USB_STAT_ACA_RID_C_HS_CHIRP: case USB_STAT_ACA_RID_C_NM:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_1P5;
di->is_aca_rid = 1; break; case USB_STAT_NOT_CONFIGURED: if (di->vbus_detected) {
di->usb_device_is_unrecognised = true;
dev_dbg(di->dev, "USB Type - Legacy charger.\n");
di->max_usb_in_curr.usb_type_max_ua =
USB_CH_IP_CUR_LVL_1P5; break;
}
fallthrough; case USB_STAT_HM_IDGND:
dev_err(di->dev, "USB Type - Charging not allowed\n");
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
ret = -ENXIO; break; case USB_STAT_RESERVED: if (is_ab8500(di->parent)) {
di->flags.vbus_collapse = true;
dev_err(di->dev, "USB Type - USB_STAT_RESERVED " "VBUS has collapsed\n");
ret = -ENXIO; break;
} else {
dev_dbg(di->dev, "USB Type - Charging not allowed\n");
di->max_usb_in_curr.usb_type_max_ua =
USB_CH_IP_CUR_LVL_0P05;
dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
link_status,
di->max_usb_in_curr.usb_type_max_ua);
ret = -ENXIO; break;
} case USB_STAT_CARKIT_1: case USB_STAT_CARKIT_2: case USB_STAT_ACA_DOCK_CHARGER: case USB_STAT_CHARGER_LINE_1:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5;
dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
di->max_usb_in_curr.usb_type_max_ua); break; case USB_STAT_NOT_VALID_LINK:
dev_err(di->dev, "USB Type invalid - try charging anyway\n");
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5; break;
default:
dev_err(di->dev, "USB Type - Unknown\n");
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
ret = -ENXIO; break;
}
/** * ab8500_charger_read_usb_type() - read the type of usb connected * @di: pointer to the ab8500_charger structure * * Detect the type of the plugged USB * Returns error code in case of failure else 0 on success
*/ staticint ab8500_charger_read_usb_type(struct ab8500_charger *di)
{ int ret;
u8 val;
ret = abx500_get_register_interruptible(di->dev,
AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return ret;
} if (is_ab8500(di->parent))
ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
AB8500_USB_LINE_STAT_REG, &val); else
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return ret;
}
/* get the USB type */ if (is_ab8500(di->parent))
val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT; else
val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
ret = ab8500_charger_max_usb_curr(di,
(enum ab8500_charger_link_status) val);
return ret;
}
/** * ab8500_charger_detect_usb_type() - get the type of usb connected * @di: pointer to the ab8500_charger structure * * Detect the type of the plugged USB * Returns error code in case of failure else 0 on success
*/ staticint ab8500_charger_detect_usb_type(struct ab8500_charger *di)
{ int i, ret;
u8 val;
/* * On getting the VBUS rising edge detect interrupt there * is a 250ms delay after which the register UsbLineStatus * is filled with valid data.
*/ for (i = 0; i < 10; i++) {
msleep(250);
ret = abx500_get_register_interruptible(di->dev,
AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
&val);
dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
__func__, val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return ret;
}
if (is_ab8500(di->parent))
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINE_STAT_REG, &val); else
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return ret;
}
dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
val); /* * Until the IT source register is read the UsbLineStatus * register is not updated, hence doing the same * Revisit this:
*/
/* get the USB type */ if (is_ab8500(di->parent))
val = (val & AB8500_USB_LINK_STATUS) >>
USB_LINK_STATUS_SHIFT; else
val = (val & AB8505_USB_LINK_STATUS) >>
USB_LINK_STATUS_SHIFT; if (val) break;
}
ret = ab8500_charger_max_usb_curr(di,
(enum ab8500_charger_link_status) val);
return ret;
}
/* * This array maps the raw hex value to charger voltage used by the AB8500 * Values taken from the UM0836, in microvolt.
*/ staticint ab8500_charger_voltage_map[] = {
3500000,
3525000,
3550000,
3575000,
3600000,
3625000,
3650000,
3675000,
3700000,
3725000,
3750000,
3775000,
3800000,
3825000,
3850000,
3875000,
3900000,
3925000,
3950000,
3975000,
4000000,
4025000,
4050000,
4060000,
4070000,
4080000,
4090000,
4100000,
4110000,
4120000,
4130000,
4140000,
4150000,
4160000,
4170000,
4180000,
4190000,
4200000,
4210000,
4220000,
4230000,
4240000,
4250000,
4260000,
4270000,
4280000,
4290000,
4300000,
4310000,
4320000,
4330000,
4340000,
4350000,
4360000,
4370000,
4380000,
4390000,
4400000,
4410000,
4420000,
4430000,
4440000,
4450000,
4460000,
4470000,
4480000,
4490000,
4500000,
4510000,
4520000,
4530000,
4540000,
4550000,
4560000,
4570000,
4580000,
4590000,
4600000,
};
staticint ab8500_voltage_to_regval(int voltage_uv)
{ int i;
/* Special case for voltage below 3.5V */ if (voltage_uv < ab8500_charger_voltage_map[0]) return LOW_VOLT_REG;
for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) { if (voltage_uv < ab8500_charger_voltage_map[i]) return i - 1;
}
/* If not last element, return error */
i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1; if (voltage_uv == ab8500_charger_voltage_map[i]) return i; else return -1;
}
/* This array maps the raw register value to charger input current */ staticint ab8500_charge_input_curr_map[] = {
50000, 98000, 193000, 290000, 380000, 450000, 500000, 600000,
700000, 800000, 900000, 1000000, 1100000, 1300000, 1400000, 1500000,
};
/* This array maps the raw register value to charger output current */ staticint ab8500_charge_output_curr_map[] = {
100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000,
900000, 1000000, 1100000, 1200000, 1300000, 1400000, 1500000, 1500000,
};
staticint ab8500_current_to_regval(struct ab8500_charger *di, int curr_ua)
{ int i;
if (curr_ua < ab8500_charge_output_curr_map[0]) return 0;
for (i = 0; i < ARRAY_SIZE(ab8500_charge_output_curr_map); i++) { if (curr_ua < ab8500_charge_output_curr_map[i]) return i - 1;
}
/* If not last element, return error */
i = ARRAY_SIZE(ab8500_charge_output_curr_map) - 1; if (curr_ua == ab8500_charge_output_curr_map[i]) return i; else return -1;
}
staticint ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr_ua)
{ int i;
if (curr_ua < ab8500_charge_input_curr_map[0]) return 0;
for (i = 0; i < ARRAY_SIZE(ab8500_charge_input_curr_map); i++) { if (curr_ua < ab8500_charge_input_curr_map[i]) return i - 1;
}
/* If not last element, return error */
i = ARRAY_SIZE(ab8500_charge_input_curr_map) - 1; if (curr_ua == ab8500_charge_input_curr_map[i]) return i; else return -1;
}
/** * ab8500_charger_get_usb_cur() - get usb current * @di: pointer to the ab8500_charger structure * * The usb stack provides the maximum current that can be drawn from * the standard usb host. This will be in uA. * This function converts current in uA to a value that can be written * to the register. Returns -1 if charging is not allowed
*/ staticint ab8500_charger_get_usb_cur(struct ab8500_charger *di)
{ int ret = 0; switch (di->usb_state.usb_current_ua) { case 100000:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P09; break; case 200000:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P19; break; case 300000:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P29; break; case 400000:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P38; break; case 500000:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P5; break; default:
di->max_usb_in_curr.usb_type_max_ua = USB_CH_IP_CUR_LVL_0P05;
ret = -EPERM; break;
}
di->max_usb_in_curr.set_max_ua = di->max_usb_in_curr.usb_type_max_ua; return ret;
}
/** * ab8500_charger_check_continue_stepping() - Check to allow stepping * @di: pointer to the ab8500_charger structure * @reg: select what charger register to check * * Check if current stepping should be allowed to continue. * Checks if charger source has not collapsed. If it has, further stepping * is not allowed.
*/ staticbool ab8500_charger_check_continue_stepping(struct ab8500_charger *di, int reg)
{ if (reg == AB8500_USBCH_IPT_CRNTLVL_REG) return !di->flags.vbus_drop_end; else returntrue;
}
/** * ab8500_charger_set_current() - set charger current * @di: pointer to the ab8500_charger structure * @ich_ua: charger current, in uA * @reg: select what charger register to set * * Set charger current. * There is no state machine in the AB to step up/down the charger * current to avoid dips and spikes on MAIN, VBUS and VBAT when * charging is started. Instead we need to implement * this charger current step-up/down here. * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_set_current(struct ab8500_charger *di, int ich_ua, int reg)
{ int ret = 0; int curr_index, prev_curr_index, shift_value, i;
u8 reg_value;
u32 step_udelay; bool no_stepping = false;
atomic_inc(&di->current_stepping_sessions);
ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
reg, ®_value); if (ret < 0) {
dev_err(di->dev, "%s read failed\n", __func__); goto exit_set_current;
}
if (!di->usb.charger_connected && !di->ac.charger_connected)
no_stepping = true;
break; default:
dev_err(di->dev, "%s current register not valid\n", __func__);
ret = -ENXIO; goto exit_set_current;
}
if (curr_index < 0) {
dev_err(di->dev, "requested current limit out-of-range\n");
ret = -ENXIO; goto exit_set_current;
}
/* only update current if it's been changed */ if (prev_curr_index == curr_index) {
dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
__func__, reg);
ret = 0; goto exit_set_current;
}
dev_dbg(di->dev, "%s set charger current: %d uA for reg: 0x%02x\n",
__func__, ich_ua, reg);
if (no_stepping) {
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
reg, (u8)curr_index << shift_value); if (ret)
dev_err(di->dev, "%s write failed\n", __func__);
} elseif (prev_curr_index > curr_index) { for (i = prev_curr_index - 1; i >= curr_index; i--) {
dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
(u8) i << shift_value, reg);
ret = abx500_set_register_interruptible(di->dev,
AB8500_CHARGER, reg, (u8)i << shift_value); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); goto exit_set_current;
} if (i != curr_index)
usleep_range(step_udelay, step_udelay * 2);
}
} else { bool allow = true; for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
(u8)i << shift_value, reg);
ret = abx500_set_register_interruptible(di->dev,
AB8500_CHARGER, reg, (u8)i << shift_value); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); goto exit_set_current;
} if (i != curr_index)
usleep_range(step_udelay, step_udelay * 2);
/** * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit * @di: pointer to the ab8500_charger structure * @ich_in_ua: charger input current limit in microampere * * Sets the current that can be drawn from the USB host * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, int ich_in_ua)
{ int min_value; int ret;
/* We should always use to lowest current limit */
min_value = min(di->bm->chg_params->usb_curr_max_ua, ich_in_ua); if (di->max_usb_in_curr.set_max_ua > 0)
min_value = min(di->max_usb_in_curr.set_max_ua, min_value);
if (di->usb_state.usb_current_ua >= 0)
min_value = min(di->usb_state.usb_current_ua, min_value);
switch (min_value) { case 100000: if (di->vbat < VBAT_TRESH_IP_CUR_RED)
min_value = USB_CH_IP_CUR_LVL_0P05; break; case 500000: if (di->vbat < VBAT_TRESH_IP_CUR_RED)
min_value = USB_CH_IP_CUR_LVL_0P45; break; default: break;
}
dev_info(di->dev, "VBUS input current limit set to %d uA\n", min_value);
mutex_lock(&di->usb_ipt_crnt_lock);
ret = ab8500_charger_set_current(di, min_value,
AB8500_USBCH_IPT_CRNTLVL_REG);
mutex_unlock(&di->usb_ipt_crnt_lock);
return ret;
}
/** * ab8500_charger_set_main_in_curr() - set main charger input current * @di: pointer to the ab8500_charger structure * @ich_in_ua: input charger current, in uA * * Set main charger input current. * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_set_main_in_curr(struct ab8500_charger *di, int ich_in_ua)
{ return ab8500_charger_set_current(di, ich_in_ua,
AB8500_MCH_IPT_CURLVL_REG);
}
/** * ab8500_charger_set_output_curr() - set charger output current * @di: pointer to the ab8500_charger structure * @ich_out_ua: output charger current, in uA * * Set charger output current. * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_set_output_curr(struct ab8500_charger *di, int ich_out_ua)
{ return ab8500_charger_set_current(di, ich_out_ua,
AB8500_CH_OPT_CRNTLVL_REG);
}
/** * ab8500_charger_led_en() - turn on/off chargign led * @di: pointer to the ab8500_charger structure * @on: flag to turn on/off the chargign led * * Power ON/OFF charging LED indication * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_led_en(struct ab8500_charger *di, int on)
{ int ret;
if (on) { /* Power ON charging LED indicator, set LED current to 5mA */
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_LED_INDICATOR_PWM_CTRL,
(LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA)); if (ret) {
dev_err(di->dev, "Power ON LED failed\n"); return ret;
} /* LED indicator PWM duty cycle 252/256 */
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_LED_INDICATOR_PWM_DUTY,
LED_INDICATOR_PWM_DUTY_252_256); if (ret) {
dev_err(di->dev, "Set LED PWM duty cycle failed\n"); return ret;
}
} else { /* Power off charging LED indicator */
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_LED_INDICATOR_PWM_CTRL,
LED_INDICATOR_PWM_DIS); if (ret) {
dev_err(di->dev, "Power-off LED failed\n"); return ret;
}
}
return ret;
}
/** * ab8500_charger_ac_en() - enable or disable ac charging * @di: pointer to the ab8500_charger structure * @enable: enable/disable flag * @vset_uv: charging voltage in microvolt * @iset_ua: charging current in microampere * * Enable/Disable AC/Mains charging and turns on/off the charging led * respectively.
**/ staticint ab8500_charger_ac_en(struct ux500_charger *charger, int enable, int vset_uv, int iset_ua)
{ int ret; int volt_index; int curr_index; int input_curr_index;
u8 overshoot = 0;
/* * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts * will be triggered every time we enable the VDD ADC supply. * This will turn off charging for a short while. * It can be avoided by having the supply on when * there is a charger enabled. Normally the VDD ADC supply * is enabled every time a GPADC conversion is triggered. * We will force it to be enabled from this driver to have * the GPADC module independent of the AB8500 chargers
*/ if (!di->vddadc_en_ac) {
ret = regulator_enable(di->regu); if (ret)
dev_warn(di->dev, "Failed to enable regulator\n"); else
di->vddadc_en_ac = true;
}
/* Check if the requested voltage or current is valid */
volt_index = ab8500_voltage_to_regval(vset_uv);
curr_index = ab8500_current_to_regval(di, iset_ua);
input_curr_index = ab8500_current_to_regval(di,
di->bm->chg_params->ac_curr_max_ua); if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
dev_err(di->dev, "Charger voltage or current too high, " "charging not started\n"); return -ENXIO;
}
/* ChVoltLevel: maximum battery charging voltage */
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_VOLT_LVL_REG, (u8) volt_index); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
} /* MainChInputCurr: current that can be drawn from the charger*/
ret = ab8500_charger_set_main_in_curr(di,
di->bm->chg_params->ac_curr_max_ua); if (ret) {
dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
__func__); return ret;
} /* ChOutputCurentLevel: protected output current */
ret = ab8500_charger_set_output_curr(di, iset_ua); if (ret) {
dev_err(di->dev, "%s " "Failed to set ChOutputCurentLevel\n",
__func__); return ret;
}
/* Check if VBAT overshoot control should be enabled */ if (!di->bm->enable_overshoot)
overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
/* Enable Main Charger */
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
}
/* Power on charging LED indication */
ret = ab8500_charger_led_en(di, true); if (ret < 0)
dev_err(di->dev, "failed to enable LED\n");
di->ac.charger_online = 1;
} else { /* Disable AC charging */ if (is_ab8500_1p1_or_earlier(di->parent)) { /* * For ABB revision 1.0 and 1.1 there is a bug in the * watchdog logic. That means we have to continuously * kick the charger watchdog even when no charger is * connected. This is only valid once the AC charger * has been enabled. This is a bug that is not handled * by the algorithm and the watchdog have to be kicked * by the charger driver when the AC charger * is disabled
*/ if (di->ac_conn) {
queue_delayed_work(di->charger_wq,
&di->kick_wd_work,
round_jiffies(WD_KICK_INTERVAL));
}
/* * We can't turn off charging completely * due to a bug in AB8500 cut1. * If we do, charging will not start again. * That is why we set the lowest voltage * and current possible
*/
ret = abx500_set_register_interruptible(di->dev,
AB8500_CHARGER,
AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
}
ret = ab8500_charger_set_output_curr(di, 0); if (ret) {
dev_err(di->dev, "%s " "Failed to set ChOutputCurentLevel\n",
__func__); return ret;
}
} else {
ret = abx500_set_register_interruptible(di->dev,
AB8500_CHARGER,
AB8500_MCH_CTRL1, 0); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
}
}
ret = ab8500_charger_led_en(di, false); if (ret < 0)
dev_err(di->dev, "failed to disable LED\n");
/* Disable regulator if enabled */ if (di->vddadc_en_ac) {
regulator_disable(di->regu);
di->vddadc_en_ac = false;
}
dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
}
ab8500_power_supply_changed(di, di->ac_chg.psy);
return ret;
}
/** * ab8500_charger_usb_en() - enable usb charging * @di: pointer to the ab8500_charger structure * @enable: enable/disable flag * @vset_uv: charging voltage in microvolt * @ich_out_ua: charger output current in microampere * * Enable/Disable USB charging and turns on/off the charging led respectively. * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_usb_en(struct ux500_charger *charger, int enable, int vset_uv, int ich_out_ua)
{ int ret; int volt_index; int curr_index;
u8 overshoot = 0;
if (enable) { /* Check if USB is connected */ if (!di->usb.charger_connected) {
dev_err(di->dev, "USB charger not connected\n"); return -ENXIO;
}
/* * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts * will be triggered every time we enable the VDD ADC supply. * This will turn off charging for a short while. * It can be avoided by having the supply on when * there is a charger enabled. Normally the VDD ADC supply * is enabled every time a GPADC conversion is triggered. * We will force it to be enabled from this driver to have * the GPADC module independent of the AB8500 chargers
*/ if (!di->vddadc_en_usb) {
ret = regulator_enable(di->regu); if (ret)
dev_warn(di->dev, "Failed to enable regulator\n"); else
di->vddadc_en_usb = true;
}
/* Check if the requested voltage or current is valid */
volt_index = ab8500_voltage_to_regval(vset_uv);
curr_index = ab8500_current_to_regval(di, ich_out_ua); if (volt_index < 0 || curr_index < 0) {
dev_err(di->dev, "Charger voltage or current too high, " "charging not started\n"); return -ENXIO;
}
/* * ChVoltLevel: max voltage up to which battery can be * charged
*/
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CH_VOLT_LVL_REG, (u8) volt_index); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
} /* Check if VBAT overshoot control should be enabled */ if (!di->bm->enable_overshoot)
overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
/* Enable USB Charger */
dev_dbg(di->dev, "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
}
/* If success power on charging LED indication */
ret = ab8500_charger_led_en(di, true); if (ret < 0)
dev_err(di->dev, "failed to enable LED\n");
di->usb.charger_online = 1;
/* USBChInputCurr: current that can be drawn from the usb */
ret = ab8500_charger_set_vbus_in_curr(di,
di->max_usb_in_curr.usb_type_max_ua); if (ret) {
dev_err(di->dev, "setting USBChInputCurr failed\n"); return ret;
}
/* ChOutputCurentLevel: protected output current */
ret = ab8500_charger_set_output_curr(di, ich_out_ua); if (ret) {
dev_err(di->dev, "%s " "Failed to set ChOutputCurentLevel\n",
__func__); return ret;
}
} else { /* Disable USB charging */
dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
ret = abx500_set_register_interruptible(di->dev,
AB8500_CHARGER,
AB8500_USBCH_CTRL1_REG, 0); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
}
ret = ab8500_charger_led_en(di, false); if (ret < 0)
dev_err(di->dev, "failed to disable LED\n"); /* USBChInputCurr: current that can be drawn from the usb */
ret = ab8500_charger_set_vbus_in_curr(di, 0); if (ret) {
dev_err(di->dev, "setting USBChInputCurr failed\n"); return ret;
}
/* ChOutputCurentLevel: protected output current */
ret = ab8500_charger_set_output_curr(di, 0); if (ret) {
dev_err(di->dev, "%s " "Failed to reset ChOutputCurentLevel\n",
__func__); return ret;
}
di->usb.charger_online = 0;
di->usb.wd_expired = false;
/* Disable regulator if enabled */ if (di->vddadc_en_usb) {
regulator_disable(di->regu);
di->vddadc_en_usb = false;
}
dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
/* Cancel any pending Vbat check work */
cancel_delayed_work(&di->check_vbat_work);
/** * ab8500_charger_usb_check_enable() - enable usb charging * @charger: pointer to the ux500_charger structure * @vset_uv: charging voltage in microvolt * @iset_ua: charger output current in microampere * * Check if the VBUS charger has been disconnected and reconnected without * AB8500 rising an interrupt. Returns 0 on success.
*/ staticint ab8500_charger_usb_check_enable(struct ux500_charger *charger, int vset_uv, int iset_ua)
{
u8 usbch_ctrl1 = 0; int ret = 0;
ret = ab8500_charger_usb_en(&di->usb_chg, true, vset_uv, iset_ua); if (ret < 0) {
dev_err(di->dev, "Failed to enable VBUS charger %d\n",
__LINE__); return ret;
}
} return ret;
}
/** * ab8500_charger_ac_check_enable() - enable usb charging * @charger: pointer to the ux500_charger structure * @vset_uv: charging voltage in microvolt * @iset_ua: charger output current in micrompere * * Check if the AC charger has been disconnected and reconnected without * AB8500 rising an interrupt. Returns 0 on success.
*/ staticint ab8500_charger_ac_check_enable(struct ux500_charger *charger, int vset_uv, int iset_ua)
{
u8 mainch_ctrl1 = 0; int ret = 0;
ret = ab8500_charger_ac_en(&di->usb_chg, true, vset_uv, iset_ua); if (ret < 0) {
dev_err(di->dev, "failed to enable AC charger %d\n",
__LINE__); return ret;
}
} return ret;
}
/** * ab8500_charger_watchdog_kick() - kick charger watchdog * @di: pointer to the ab8500_charger structure * * Kick charger watchdog * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_watchdog_kick(struct ux500_charger *charger)
{ int ret; struct ab8500_charger *di;
if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
di = to_ab8500_charger_ac_device_info(charger); elseif (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
di = to_ab8500_charger_usb_device_info(charger); else return -ENXIO;
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); if (ret)
dev_err(di->dev, "Failed to kick WD!\n");
return ret;
}
/** * ab8500_charger_update_charger_current() - update charger current * @charger: pointer to the ab8500_charger structure * @ich_out_ua: desired output current in microampere * * Update the charger output current for the specified charger * Returns error code in case of failure else 0(on success)
*/ staticint ab8500_charger_update_charger_current(struct ux500_charger *charger, int ich_out_ua)
{ int ret; struct ab8500_charger *di;
if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
di = to_ab8500_charger_ac_device_info(charger); elseif (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
di = to_ab8500_charger_usb_device_info(charger); else return -ENXIO;
ret = ab8500_charger_set_output_curr(di, ich_out_ua); if (ret) {
dev_err(di->dev, "%s " "Failed to set ChOutputCurentLevel\n",
__func__); return ret;
}
/* Reset the main and usb drop input current measurement counter */
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CHARGER_CTRL, DROP_COUNT_RESET); if (ret) {
dev_err(di->dev, "%s write failed\n", __func__); return ret;
}
/* * For all psy where the driver name appears in any supplied_to * in practice what we will find will always be "ab8500_fg" as * the fuel gauge is responsible of keeping track of VBAT.
*/
j = match_string(supplicants, ext->num_supplicants, psy->desc->name); if (j < 0) return 0;
/* Go through all properties for the psy */ for (j = 0; j < ext->desc->num_properties; j++) { enum power_supply_property prop;
prop = ext->desc->properties[j];
if (power_supply_get_property(ext, prop, &ret)) continue;
switch (prop) { case POWER_SUPPLY_PROP_VOLTAGE_NOW: switch (ext->desc->type) { case POWER_SUPPLY_TYPE_BATTERY: /* This will always be "ab8500_fg" */
dev_dbg(di->dev, "get VBAT from %s\n",
dev_name(&ext->dev));
di->vbat = ret.intval; break; default: break;
} break; default: break;
}
} return 0;
}
/** * ab8500_charger_check_vbat_work() - keep vbus current within spec * @work pointer to the work_struct structure * * Due to a asic bug it is necessary to lower the input current to the vbus * charger when charging with at some specific levels. This issue is only valid * for below a certain battery voltage. This function makes sure that * the allowed current limit isn't exceeded.
*/ staticvoid ab8500_charger_check_vbat_work(struct work_struct *work)
{ int t = 10; struct ab8500_charger *di = container_of(work, struct ab8500_charger, check_vbat_work.work);
/* * No need to check the battery voltage every second when not close to * the threshold.
*/ if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100000) &&
(di->vbat > (VBAT_TRESH_IP_CUR_RED - 100000)))
t = 1;
queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
}
/** * ab8500_charger_check_hw_failure_work() - check main charger failure * @work: pointer to the work_struct structure * * Work queue function for checking the main charger status
*/ staticvoid ab8500_charger_check_hw_failure_work(struct work_struct *work)
{ int ret;
u8 reg_value;
/* Check if the status bits for HW failure is still active */ if (di->flags.mainextchnotok) {
ret = abx500_get_register_interruptible(di->dev,
AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return;
} if (!(reg_value & MAIN_CH_NOK)) {
di->flags.mainextchnotok = false;
ab8500_power_supply_changed(di, di->ac_chg.psy);
}
} if (di->flags.vbus_ovv) {
ret = abx500_get_register_interruptible(di->dev,
AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
®_value); if (ret < 0) {
dev_err(di->dev, "%s ab8500 read failed\n", __func__); return;
} if (!(reg_value & VBUS_OVV_TH)) {
di->flags.vbus_ovv = false;
ab8500_power_supply_changed(di, di->usb_chg.psy);
}
} /* If we still have a failure, schedule a new check */ if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
queue_delayed_work(di->charger_wq,
&di->check_hw_failure_work, round_jiffies(HZ));
}
}
/** * ab8500_charger_kick_watchdog_work() - kick the watchdog * @work: pointer to the work_struct structure * * Work queue function for kicking the charger watchdog. * * For ABB revision 1.0 and 1.1 there is a bug in the watchdog * logic. That means we have to continuously kick the charger * watchdog even when no charger is connected. This is only * valid once the AC charger has been enabled. This is * a bug that is not handled by the algorithm and the * watchdog have to be kicked by the charger driver * when the AC charger is disabled
*/ staticvoid ab8500_charger_kick_watchdog_work(struct work_struct *work)
{ int ret;
ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); if (ret)
dev_err(di->dev, "Failed to kick WD!\n");
/* Schedule a new watchdog kick */
queue_delayed_work(di->charger_wq,
&di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
}
/** * ab8500_charger_ac_work() - work to get and set main charger status * @work: pointer to the work_struct structure * * Work queue function for checking the main charger status
*/ staticvoid ab8500_charger_ac_work(struct work_struct *work)
{ int ret;
/* * Since we can't be sure that the events are received * synchronously, we have the check if the main charger is * connected by reading the status register
*/
ret = ab8500_charger_detect_chargers(di, false); if (ret < 0) return;
/** * ab8500_charger_detect_usb_type_work() - work to detect USB type * @work: Pointer to the work_struct structure * * Detect the type of USB plugged
*/ staticvoid ab8500_charger_detect_usb_type_work(struct work_struct *work)
{ int ret;
/* * Since we can't be sure that the events are received * synchronously, we have the check if is * connected by reading the status register
*/
ret = ab8500_charger_detect_chargers(di, false); if (ret < 0) return;
if (is_ab8500_1p1_or_earlier(di->parent)) {
ret = ab8500_charger_detect_usb_type(di); if (!ret) {
ab8500_charger_set_usb_connected(di, true);
ab8500_power_supply_changed(di,
di->usb_chg.psy);
}
} else { /* * For ABB cut2.0 and onwards we have an IRQ, * USB_LINK_STATUS that will be triggered when the USB * link status changes. The exception is USB connected * during startup. Then we don't get a * USB_LINK_STATUS IRQ
*/ if (di->vbus_detected_start) {
di->vbus_detected_start = false;
ret = ab8500_charger_detect_usb_type(di); if (!ret) {
ab8500_charger_set_usb_connected(di, true);
ab8500_power_supply_changed(di,
di->usb_chg.psy);
}
}
}
}
}
/** * ab8500_charger_usb_link_attach_work() - work to detect USB type * @work: pointer to the work_struct structure * * Detect the type of USB plugged
*/ staticvoid ab8500_charger_usb_link_attach_work(struct work_struct *work)
{ struct ab8500_charger *di =
container_of(work, struct ab8500_charger, attach_work.work); int ret;
/* Update maximum input current if USB enumeration is not detected */ if (!di->usb.charger_online) {
ret = ab8500_charger_set_vbus_in_curr(di,
di->max_usb_in_curr.usb_type_max_ua); if (ret) return;
}
/** * ab8500_charger_usb_link_status_work() - work to detect USB type * @work: pointer to the work_struct structure * * Detect the type of USB plugged
*/ staticvoid ab8500_charger_usb_link_status_work(struct work_struct *work)
{ int detected_chargers; int ret;
u8 val;
u8 link_status;
/* * Since we can't be sure that the events are received * synchronously, we have the check if is * connected by reading the status register
*/
detected_chargers = ab8500_charger_detect_chargers(di, false); if (detected_chargers < 0) return;
/* * Some chargers that breaks the USB spec is * identified as invalid by AB8500 and it refuse * to start the charging process. but by jumping * through a few hoops it can be forced to start.
*/ if (is_ab8500(di->parent))
ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
AB8500_USB_LINE_STAT_REG, &val); else
ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
AB8500_USB_LINK1_STAT_REG, &val);
if (ret >= 0)
dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val); else
dev_dbg(di->dev, "Error reading USB link status\n");
if (is_ab8500(di->parent))
link_status = AB8500_USB_LINK_STATUS; else
link_status = AB8505_USB_LINK_STATUS;
if (detected_chargers & USB_PW_CONN) { if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
USB_STAT_NOT_VALID_LINK &&
di->invalid_charger_detect_state == 0) {
dev_dbg(di->dev, "Invalid charger detected, state= 0\n"); /*Enable charger*/
abx500_mask_and_set_register_interruptible(di->dev,
AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
USB_CH_ENA, USB_CH_ENA); /*Enable charger detection*/
abx500_mask_and_set_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
USB_CH_DET, USB_CH_DET);
di->invalid_charger_detect_state = 1; /*exit and wait for new link status interrupt.*/ return;
} if (di->invalid_charger_detect_state == 1) {
dev_dbg(di->dev, "Invalid charger detected, state= 1\n"); /*Stop charger detection*/
abx500_mask_and_set_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
USB_CH_DET, 0x00); /*Check link status*/ if (is_ab8500(di->parent))
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINE_STAT_REG,
&val); else
ret = abx500_get_register_interruptible(di->dev,
AB8500_USB, AB8500_USB_LINK1_STAT_REG,
&val);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.