Lines Matching +full:usb +full:- +full:charger +full:- +full:detection
1 // SPDX-License-Identifier: GPL-2.0-only
3 * tusb1210.c - TUSB1210 USB ULPI PHY driver
42 /* TUSB1210 charger detection work states */
75 ret = ulpi_write(tusb->ulpi, reg, val); in tusb1210_ulpi_write()
77 dev_err(&tusb->ulpi->dev, "error %d writing val 0x%02x to reg 0x%02x\n", in tusb1210_ulpi_write()
87 ret = ulpi_read(tusb->ulpi, reg); in tusb1210_ulpi_read()
92 dev_err(&tusb->ulpi->dev, "error %d reading reg 0x%02x\n", ret, reg); in tusb1210_ulpi_read()
102 gpiod_set_value_cansleep(tusb->gpio_reset, 1); in tusb1210_power_on()
103 gpiod_set_value_cansleep(tusb->gpio_cs, 1); in tusb1210_power_on()
108 tusb1210_ulpi_write(tusb, TUSB1210_VENDOR_SPECIFIC2, tusb->vendor_specific2); in tusb1210_power_on()
117 gpiod_set_value_cansleep(tusb->gpio_reset, 0); in tusb1210_power_off()
118 gpiod_set_value_cansleep(tusb->gpio_cs, 0); in tusb1210_power_off()
154 tusb->otg_ctrl = reg; in tusb1210_set_mode()
172 gpiod_set_value_cansleep(tusb->gpio_reset, 0); in tusb1210_reset()
174 gpiod_set_value_cansleep(tusb->gpio_reset, 1); in tusb1210_reset()
180 dev_dbg(&tusb->ulpi->dev, "charger type: %d\n", type); in tusb1210_chg_det_set_type()
181 tusb->chg_type = type; in tusb1210_chg_det_set_type()
182 tusb->chg_det_retries = 0; in tusb1210_chg_det_set_type()
183 power_supply_changed(tusb->psy); in tusb1210_chg_det_set_type()
191 dev_dbg(&tusb->ulpi->dev, "chg_det new state %s in %d ms\n", in tusb1210_chg_det_set_state()
194 tusb->chg_det_state = new_state; in tusb1210_chg_det_set_state()
195 mod_delayed_work(system_long_wq, &tusb->chg_det_work, in tusb1210_chg_det_set_state()
202 if (tusb->chg_det_retries < TUSB1210_CHG_DET_MAX_RETRIES) { in tusb1210_chg_det_handle_ulpi_error()
203 tusb->chg_det_retries++; in tusb1210_chg_det_handle_ulpi_error()
213 * Boards using a TUSB121x for charger-detection have 3 power_supply class devs:
215 * tusb1211-charger-detect(1) -> charger -> fuel-gauge
217 * To determine if an USB charger is connected to the board, the online prop of
218 * the charger psy needs to be read. Since the tusb1211-charger-detect psy is
219 * the start of the supplier -> supplied-to chain, power_supply_am_i_supplied()
223 * these boards and the charger psy is looked up by name from this list.
225 * (1) modelling the external USB charger
228 "bq24190-charger",
233 struct power_supply *charger = NULL; in tusb1210_get_online() local
238 for (i = 0; i < ARRAY_SIZE(tusb1210_chargers) && !charger; i++) in tusb1210_get_online()
239 charger = power_supply_get_by_name(tusb1210_chargers[i]); in tusb1210_get_online()
241 if (!charger) in tusb1210_get_online()
244 ret = power_supply_get_property(charger, POWER_SUPPLY_PROP_ONLINE, &val); in tusb1210_get_online()
248 power_supply_put(charger); in tusb1210_get_online()
260 dev_dbg(&tusb->ulpi->dev, "chg_det state %s vbus_present %d\n", in tusb1210_chg_det_work()
261 tusb1210_chg_det_states[tusb->chg_det_state], vbus_present); in tusb1210_chg_det_work()
263 switch (tusb->chg_det_state) { in tusb1210_chg_det_work()
265 tusb->chg_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; in tusb1210_chg_det_work()
266 tusb->chg_det_retries = 0; in tusb1210_chg_det_work()
267 /* Power on USB controller for ulpi_read()/_write() */ in tusb1210_chg_det_work()
268 ret = pm_runtime_resume_and_get(tusb->ulpi->dev.parent); in tusb1210_chg_det_work()
270 dev_err(&tusb->ulpi->dev, "error %d runtime-resuming\n", ret); in tusb1210_chg_det_work()
271 /* Should never happen, skip charger detection */ in tusb1210_chg_det_work()
279 * Use the builtin charger detection FSM to keep things simple. in tusb1210_chg_det_work()
281 * boards which actually rely on the phy for charger detection. in tusb1210_chg_det_work()
283 mutex_lock(&tusb->phy->mutex); in tusb1210_chg_det_work()
286 mutex_unlock(&tusb->phy->mutex); in tusb1210_chg_det_work()
292 /* Wait 400 ms for the charger detection FSM to finish */ in tusb1210_chg_det_work()
296 mutex_lock(&tusb->phy->mutex); in tusb1210_chg_det_work()
298 mutex_unlock(&tusb->phy->mutex); in tusb1210_chg_det_work()
312 mutex_lock(&tusb->phy->mutex); in tusb1210_chg_det_work()
314 /* Set SW_CONTROL to stop the charger-det FSM */ in tusb1210_chg_det_work()
318 /* Clear DP_VSRC_EN which may have been enabled by the charger-det FSM */ in tusb1210_chg_det_work()
322 /* Clear CHGD_IDP_SRC_EN (may have been enabled by the charger-det FSM) */ in tusb1210_chg_det_work()
332 /* Restore phy-parameters and OTG_CTRL register */ in tusb1210_chg_det_work()
333 tusb1210_ulpi_write(tusb, ULPI_OTG_CTRL, tusb->otg_ctrl); in tusb1210_chg_det_work()
335 tusb->vendor_specific2); in tusb1210_chg_det_work()
337 mutex_unlock(&tusb->phy->mutex); in tusb1210_chg_det_work()
339 pm_runtime_put(tusb->ulpi->dev.parent); in tusb1210_chg_det_work()
348 * The phy seems to take approx. 600ms longer then the charger in tusb1210_chg_det_work()
358 * after a Vbus-session end. Reset it to work around this. in tusb1210_chg_det_work()
377 if (psy != tusb->psy && psy->desc->type == POWER_SUPPLY_TYPE_USB) in tusb1210_psy_notifier()
378 queue_delayed_work(system_long_wq, &tusb->chg_det_work, 0); in tusb1210_psy_notifier()
391 val->intval = tusb1210_get_online(tusb); in tusb1210_psy_get_prop()
394 val->intval = tusb->chg_type; in tusb1210_psy_get_prop()
397 if (tusb->chg_type == POWER_SUPPLY_USB_TYPE_DCP) in tusb1210_psy_get_prop()
398 val->intval = 2000000; in tusb1210_psy_get_prop()
400 val->intval = 500000; in tusb1210_psy_get_prop()
403 return -EINVAL; in tusb1210_psy_get_prop()
422 .name = "tusb1211-charger-detect",
431 /* Setup charger detection if requested, on errors continue without chg-det */
435 struct device *dev = &tusb->ulpi->dev; in tusb1210_probe_charger_detect()
438 if (!device_property_read_bool(dev->parent, "linux,phy_charger_detect")) in tusb1210_probe_charger_detect()
441 if (tusb->ulpi->id.product != 0x1508) { in tusb1210_probe_charger_detect()
442 dev_err(dev, "error charger detection is only supported on the TUSB1211\n"); in tusb1210_probe_charger_detect()
446 ret = tusb1210_ulpi_read(tusb, ULPI_OTG_CTRL, &tusb->otg_ctrl); in tusb1210_probe_charger_detect()
450 tusb->psy = power_supply_register(dev, &tusb1210_psy_desc, &psy_cfg); in tusb1210_probe_charger_detect()
451 if (IS_ERR(tusb->psy)) in tusb1210_probe_charger_detect()
455 * Delay initial run by 2 seconds to allow the charger driver, in tusb1210_probe_charger_detect()
458 tusb->chg_det_state = TUSB1210_CHG_DET_DISCONNECTED; in tusb1210_probe_charger_detect()
459 INIT_DELAYED_WORK(&tusb->chg_det_work, tusb1210_chg_det_work); in tusb1210_probe_charger_detect()
460 queue_delayed_work(system_long_wq, &tusb->chg_det_work, 2 * HZ); in tusb1210_probe_charger_detect()
462 tusb->psy_nb.notifier_call = tusb1210_psy_notifier; in tusb1210_probe_charger_detect()
463 power_supply_reg_notifier(&tusb->psy_nb); in tusb1210_probe_charger_detect()
469 if (!IS_ERR_OR_NULL(tusb->psy)) { in tusb1210_remove_charger_detect()
470 power_supply_unreg_notifier(&tusb->psy_nb); in tusb1210_remove_charger_detect()
471 cancel_delayed_work_sync(&tusb->chg_det_work); in tusb1210_remove_charger_detect()
472 power_supply_unregister(tusb->psy); in tusb1210_remove_charger_detect()
493 tusb = devm_kzalloc(&ulpi->dev, sizeof(*tusb), GFP_KERNEL); in tusb1210_probe()
495 return -ENOMEM; in tusb1210_probe()
497 tusb->ulpi = ulpi; in tusb1210_probe()
499 tusb->gpio_reset = devm_gpiod_get_optional(&ulpi->dev, "reset", in tusb1210_probe()
501 if (IS_ERR(tusb->gpio_reset)) in tusb1210_probe()
502 return PTR_ERR(tusb->gpio_reset); in tusb1210_probe()
504 gpiod_set_value_cansleep(tusb->gpio_reset, 1); in tusb1210_probe()
506 tusb->gpio_cs = devm_gpiod_get_optional(&ulpi->dev, "cs", in tusb1210_probe()
508 if (IS_ERR(tusb->gpio_cs)) in tusb1210_probe()
509 return PTR_ERR(tusb->gpio_cs); in tusb1210_probe()
511 gpiod_set_value_cansleep(tusb->gpio_cs, 1); in tusb1210_probe()
523 if (!device_property_read_u8(&ulpi->dev, "ihstx", &val)) in tusb1210_probe()
527 if (!device_property_read_u8(&ulpi->dev, "zhsdrv", &val)) in tusb1210_probe()
531 if (!device_property_read_u8(&ulpi->dev, "datapolarity", &val)) in tusb1210_probe()
538 tusb->vendor_specific2 = reg; in tusb1210_probe()
542 tusb->phy = ulpi_phy_create(ulpi, &phy_ops); in tusb1210_probe()
543 if (IS_ERR(tusb->phy)) { in tusb1210_probe()
544 ret = PTR_ERR(tusb->phy); in tusb1210_probe()
548 phy_set_drvdata(tusb->phy, tusb); in tusb1210_probe()
561 ulpi_phy_destroy(ulpi, tusb->phy); in tusb1210_remove()