Lines Matching +full:usb +full:- +full:current +full:- +full:limit +full:- +full:microamp
1 // SPDX-License-Identifier: GPL-2.0-only
10 * Copyright (C) 2009-2010 Motorola, Inc.
27 #include <linux/usb/phy_companion.h>
29 #include <linux/usb/otg.h>
31 #include <linux/mfd/motorola-cpcap.h>
38 * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
44 #define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */
45 #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */
53 #define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */
90 * values in "Table 8-3. Charge Path Regulator Current Limit
135 struct power_supply *usb; member
137 struct phy_companion comparator; /* For USB VBUS */
181 channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE]; in cpcap_charger_get_charge_voltage()
184 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); in cpcap_charger_get_charge_voltage()
197 channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT]; in cpcap_charger_get_charge_current()
200 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error); in cpcap_charger_get_charge_current()
212 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent); in cpcap_charger_get_property()
216 val->intval = ddata->status; in cpcap_charger_get_property()
219 val->intval = ddata->limit_current; in cpcap_charger_get_property()
222 val->intval = ddata->voltage; in cpcap_charger_get_property()
225 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) in cpcap_charger_get_property()
226 val->intval = cpcap_charger_get_charge_voltage(ddata) * in cpcap_charger_get_property()
229 val->intval = 0; in cpcap_charger_get_property()
232 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING) in cpcap_charger_get_property()
233 val->intval = cpcap_charger_get_charge_current(ddata) * in cpcap_charger_get_property()
236 val->intval = 0; in cpcap_charger_get_property()
239 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING; in cpcap_charger_get_property()
242 return -EINVAL; in cpcap_charger_get_property()
251 case 0 ... 4100000 - 1: return 3800000; in cpcap_charger_match_voltage()
252 case 4100000 ... 4120000 - 1: return 4100000; in cpcap_charger_match_voltage()
253 case 4120000 ... 4150000 - 1: return 4120000; in cpcap_charger_match_voltage()
254 case 4150000 ... 4170000 - 1: return 4150000; in cpcap_charger_match_voltage()
255 case 4170000 ... 4200000 - 1: return 4170000; in cpcap_charger_match_voltage()
256 case 4200000 ... 4230000 - 1: return 4200000; in cpcap_charger_match_voltage()
257 case 4230000 ... 4250000 - 1: return 4230000; in cpcap_charger_match_voltage()
258 case 4250000 ... 4270000 - 1: return 4250000; in cpcap_charger_match_voltage()
259 case 4270000 ... 4300000 - 1: return 4270000; in cpcap_charger_match_voltage()
260 case 4300000 ... 4330000 - 1: return 4300000; in cpcap_charger_match_voltage()
261 case 4330000 ... 4350000 - 1: return 4330000; in cpcap_charger_match_voltage()
262 case 4350000 ... 4380000 - 1: return 4350000; in cpcap_charger_match_voltage()
263 case 4380000 ... 4400000 - 1: return 4380000; in cpcap_charger_match_voltage()
264 case 4400000 ... 4420000 - 1: return 4400000; in cpcap_charger_match_voltage()
265 case 4420000 ... 4440000 - 1: return 4420000; in cpcap_charger_match_voltage()
276 int voltage = ddata->voltage; in cpcap_charger_get_bat_const_charge_voltage()
293 static int cpcap_charger_current_to_regval(int microamp) in cpcap_charger_current_to_regval() argument
295 int miliamp = microamp / 1000; in cpcap_charger_current_to_regval()
299 return -EINVAL; in cpcap_charger_current_to_regval()
307 res = microamp / 88666; in cpcap_charger_current_to_regval()
317 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent); in cpcap_charger_set_property()
322 if (cpcap_charger_current_to_regval(val->intval) < 0) in cpcap_charger_set_property()
323 return -EINVAL; in cpcap_charger_set_property()
324 ddata->limit_current = val->intval; in cpcap_charger_set_property()
325 schedule_delayed_work(&ddata->detect_work, 0); in cpcap_charger_set_property()
328 voltage = cpcap_charger_match_voltage(val->intval); in cpcap_charger_set_property()
332 ddata->voltage = voltage; in cpcap_charger_set_property()
333 schedule_delayed_work(&ddata->detect_work, 0); in cpcap_charger_set_property()
336 return -EINVAL; in cpcap_charger_set_property()
357 if (!ddata->gpio[0]) in cpcap_charger_set_cable_path()
360 gpiod_set_value(ddata->gpio[0], enabled); in cpcap_charger_set_cable_path()
366 if (!ddata->gpio[1]) in cpcap_charger_set_inductive_path()
369 gpiod_set_value(ddata->gpio[1], enabled); in cpcap_charger_set_inductive_path()
378 dev_warn(ddata->dev, "unknown state: %i\n", state); in cpcap_charger_update_state()
383 ddata->status = state; in cpcap_charger_update_state()
402 dev_dbg(ddata->dev, "state: %s\n", status); in cpcap_charger_update_state()
409 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, in cpcap_charger_disable()
413 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); in cpcap_charger_disable()
425 return -EINVAL; in cpcap_charger_enable()
427 dev_dbg(ddata->dev, "enable: %i %i %i\n", in cpcap_charger_enable()
430 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff, in cpcap_charger_enable()
438 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); in cpcap_charger_enable()
447 ddata->channels[CPCAP_CHARGER_IIO_VBUS]; in cpcap_charger_vbus_valid()
453 dev_err(ddata->dev, "error reading VBUS: %i\n", error); in cpcap_charger_vbus_valid()
458 /* VBUS control functions for the USB PHY companion */
468 if (ddata->vbus_enabled) { in cpcap_charger_vbus_work()
471 dev_dbg(ddata->dev, "VBUS already provided\n"); in cpcap_charger_vbus_work()
476 ddata->feeding_vbus = true; in cpcap_charger_vbus_work()
487 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC, in cpcap_charger_vbus_work()
493 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, in cpcap_charger_vbus_work()
499 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC, in cpcap_charger_vbus_work()
504 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, in cpcap_charger_vbus_work()
511 ddata->feeding_vbus = false; in cpcap_charger_vbus_work()
518 dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__, in cpcap_charger_vbus_work()
519 ddata->vbus_enabled ? "enable" : "disable", error); in cpcap_charger_vbus_work()
529 ddata->vbus_enabled = enabled; in cpcap_charger_set_vbus()
530 schedule_delayed_work(&ddata->vbus_work, 0); in cpcap_charger_set_vbus()
542 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val); in cpcap_charger_get_ints_state()
546 s->chrg_det = val & BIT(13); in cpcap_charger_get_ints_state()
547 s->rvrs_chrg = val & BIT(12); in cpcap_charger_get_ints_state()
548 s->vbusov = val & BIT(11); in cpcap_charger_get_ints_state()
550 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val); in cpcap_charger_get_ints_state()
554 s->chrg_se1b = val & BIT(13); in cpcap_charger_get_ints_state()
555 s->rvrs_mode = val & BIT(6); in cpcap_charger_get_ints_state()
556 s->chrgcurr2 = val & BIT(5); in cpcap_charger_get_ints_state()
557 s->chrgcurr1 = val & BIT(4); in cpcap_charger_get_ints_state()
558 s->vbusvld = val & BIT(3); in cpcap_charger_get_ints_state()
560 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val); in cpcap_charger_get_ints_state()
564 s->battdetb = val & BIT(6); in cpcap_charger_get_ints_state()
574 case 0 ... 4100000 - 1: in cpcap_charger_voltage_to_regval()
576 case 4100000 ... 4200000 - 1: in cpcap_charger_voltage_to_regval()
579 case 4200000 ... 4300000 - 1: in cpcap_charger_voltage_to_regval()
582 case 4300000 ... 4380000 - 1: in cpcap_charger_voltage_to_regval()
583 offset = -1; in cpcap_charger_voltage_to_regval()
586 offset = -2; in cpcap_charger_voltage_to_regval()
592 return ((voltage - 4100000) / 20000) + offset; in cpcap_charger_voltage_to_regval()
604 power_supply_changed(ddata->usb); in cpcap_charger_disconnect()
617 power_supply_changed(ddata->usb); in cpcap_charger_disconnect()
618 schedule_delayed_work(&ddata->detect_work, delay); in cpcap_charger_disconnect()
646 if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) { in cpcap_usb_detect()
654 /* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */ in cpcap_usb_detect()
658 switch (ddata->status) { in cpcap_usb_detect()
684 if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) && in cpcap_usb_detect()
693 dev_err(ddata->dev, "battery power_supply not available %li\n", in cpcap_usb_detect()
706 dev_info(ddata->dev, "battery not inserted, charging disabled\n"); in cpcap_usb_detect()
710 if (max_current > ddata->limit_current) in cpcap_usb_detect()
711 max_current = ddata->limit_current; in cpcap_usb_detect()
714 vchrg = cpcap_charger_voltage_to_regval(ddata->voltage); in cpcap_usb_detect()
730 power_supply_changed(ddata->usb); in cpcap_usb_detect()
735 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); in cpcap_usb_detect()
742 if (!atomic_read(&ddata->active)) in cpcap_charger_irq_thread()
745 schedule_delayed_work(&ddata->detect_work, 0); in cpcap_charger_irq_thread()
759 return -ENODEV; in cpcap_usb_init_irq()
761 error = devm_request_threaded_irq(ddata->dev, irq, NULL, in cpcap_usb_init_irq()
766 dev_err(ddata->dev, "could not get irq %s: %i\n", in cpcap_usb_init_irq()
772 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL); in cpcap_usb_init_irq()
774 return -ENOMEM; in cpcap_usb_init_irq()
776 d->name = name; in cpcap_usb_init_irq()
777 d->irq = irq; in cpcap_usb_init_irq()
778 list_add(&d->node, &ddata->irq_list); in cpcap_usb_init_irq()
813 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", in cpcap_charger_init_optional_gpios()
815 if (IS_ERR(ddata->gpio[i])) { in cpcap_charger_init_optional_gpios()
816 dev_info(ddata->dev, "no mode change GPIO%i: %li\n", in cpcap_charger_init_optional_gpios()
817 i, PTR_ERR(ddata->gpio[i])); in cpcap_charger_init_optional_gpios()
818 ddata->gpio[i] = NULL; in cpcap_charger_init_optional_gpios()
831 ddata->channels[i] = devm_iio_channel_get(ddata->dev, in cpcap_charger_init_iio()
833 if (IS_ERR(ddata->channels[i])) { in cpcap_charger_init_iio()
834 error = PTR_ERR(ddata->channels[i]); in cpcap_charger_init_iio()
838 if (!ddata->channels[i]->indio_dev) { in cpcap_charger_init_iio()
839 error = -ENXIO; in cpcap_charger_init_iio()
847 if (error != -EPROBE_DEFER) in cpcap_charger_init_iio()
848 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", in cpcap_charger_init_iio()
859 .name = "usb",
871 .compatible = "motorola,mapphone-cpcap-charger",
886 &pdev->dev); in cpcap_charger_probe()
888 return -EINVAL; in cpcap_charger_probe()
890 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); in cpcap_charger_probe()
892 return -ENOMEM; in cpcap_charger_probe()
894 ddata->dev = &pdev->dev; in cpcap_charger_probe()
895 ddata->voltage = 4200000; in cpcap_charger_probe()
896 ddata->limit_current = 532000; in cpcap_charger_probe()
898 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL); in cpcap_charger_probe()
899 if (!ddata->reg) in cpcap_charger_probe()
900 return -ENODEV; in cpcap_charger_probe()
902 INIT_LIST_HEAD(&ddata->irq_list); in cpcap_charger_probe()
903 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); in cpcap_charger_probe()
904 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work); in cpcap_charger_probe()
911 atomic_set(&ddata->active, 1); in cpcap_charger_probe()
913 psy_cfg.of_node = pdev->dev.of_node; in cpcap_charger_probe()
918 ddata->usb = devm_power_supply_register(ddata->dev, in cpcap_charger_probe()
921 if (IS_ERR(ddata->usb)) { in cpcap_charger_probe()
922 error = PTR_ERR(ddata->usb); in cpcap_charger_probe()
923 dev_err(ddata->dev, "failed to register USB charger: %i\n", in cpcap_charger_probe()
933 ddata->comparator.set_vbus = cpcap_charger_set_vbus; in cpcap_charger_probe()
934 error = omap_usb2_set_comparator(&ddata->comparator); in cpcap_charger_probe()
935 if (error == -ENODEV) { in cpcap_charger_probe()
936 dev_info(ddata->dev, "charger needs phy, deferring probe\n"); in cpcap_charger_probe()
937 return -EPROBE_DEFER; in cpcap_charger_probe()
942 schedule_delayed_work(&ddata->detect_work, 0); in cpcap_charger_probe()
952 atomic_set(&ddata->active, 0); in cpcap_charger_shutdown()
955 dev_warn(ddata->dev, "could not clear USB comparator: %i\n", in cpcap_charger_shutdown()
961 dev_warn(ddata->dev, "could not clear charger: %i\n", in cpcap_charger_shutdown()
965 cancel_delayed_work_sync(&ddata->vbus_work); in cpcap_charger_shutdown()
966 cancel_delayed_work_sync(&ddata->detect_work); in cpcap_charger_shutdown()
979 .name = "cpcap-charger",
990 MODULE_ALIAS("platform:cpcap-charger");