Lines Matching +full:chg +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
23 * GPIOs
24 * chg, flt, dcm and usus are optional.
31 struct gpio_desc *chg; /* Charger status output */ member
33 struct gpio_desc *dcm; /* Current-Limit Mode input (1: DC, 2: USB) */
54 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; in max8903_get_property()
55 if (data->chg) { in max8903_get_property()
56 if (gpiod_get_value(data->chg)) in max8903_get_property()
57 /* CHG asserted */ in max8903_get_property()
58 val->intval = POWER_SUPPLY_STATUS_CHARGING; in max8903_get_property()
59 else if (data->usb_in || data->ta_in) in max8903_get_property()
60 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in max8903_get_property()
62 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in max8903_get_property()
66 val->intval = 0; in max8903_get_property()
67 if (data->usb_in || data->ta_in) in max8903_get_property()
68 val->intval = 1; in max8903_get_property()
71 val->intval = POWER_SUPPLY_HEALTH_GOOD; in max8903_get_property()
72 if (data->fault) in max8903_get_property()
73 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; in max8903_get_property()
76 return -EINVAL; in max8903_get_property()
95 ta_in = gpiod_get_value(data->dok); in max8903_dcin()
97 if (ta_in == data->ta_in) in max8903_dcin()
100 data->ta_in = ta_in; in max8903_dcin()
102 /* Set Current-Limit-Mode 1:DC 0:USB */ in max8903_dcin()
103 if (data->dcm) in max8903_dcin()
104 gpiod_set_value(data->dcm, ta_in); in max8903_dcin()
107 if (data->cen) { in max8903_dcin()
113 else if (data->usb_in) in max8903_dcin()
117 /* Else default-disable */ in max8903_dcin()
120 gpiod_set_value(data->cen, val); in max8903_dcin()
123 dev_dbg(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ? in max8903_dcin()
126 old_type = data->psy_desc.type; in max8903_dcin()
128 if (data->ta_in) in max8903_dcin()
129 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; in max8903_dcin()
130 else if (data->usb_in) in max8903_dcin()
131 data->psy_desc.type = POWER_SUPPLY_TYPE_USB; in max8903_dcin()
133 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY; in max8903_dcin()
135 if (old_type != data->psy_desc.type) in max8903_dcin()
136 power_supply_changed(data->psy); in max8903_dcin()
154 usb_in = gpiod_get_value(data->uok); in max8903_usbin()
156 if (usb_in == data->usb_in) in max8903_usbin()
159 data->usb_in = usb_in; in max8903_usbin()
161 /* Do not touch Current-Limit-Mode */ in max8903_usbin()
164 if (data->cen) { in max8903_usbin()
170 else if (data->ta_in) in max8903_usbin()
174 /* Else default-disable */ in max8903_usbin()
177 gpiod_set_value(data->cen, val); in max8903_usbin()
180 dev_dbg(data->dev, "USB Charger %s.\n", usb_in ? in max8903_usbin()
183 old_type = data->psy_desc.type; in max8903_usbin()
185 if (data->ta_in) in max8903_usbin()
186 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; in max8903_usbin()
187 else if (data->usb_in) in max8903_usbin()
188 data->psy_desc.type = POWER_SUPPLY_TYPE_USB; in max8903_usbin()
190 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY; in max8903_usbin()
192 if (old_type != data->psy_desc.type) in max8903_usbin()
193 power_supply_changed(data->psy); in max8903_usbin()
210 fault = gpiod_get_value(data->flt); in max8903_fault()
212 if (fault == data->fault) in max8903_fault()
215 data->fault = fault; in max8903_fault()
218 dev_err(data->dev, "Charger suffers a fault and stops.\n"); in max8903_fault()
220 dev_err(data->dev, "Charger recovered from a fault.\n"); in max8903_fault()
228 struct device *dev = &pdev->dev; in max8903_setup_gpios()
233 data->dok = devm_gpiod_get_optional(dev, "dok", GPIOD_IN); in max8903_setup_gpios()
234 if (IS_ERR(data->dok)) in max8903_setup_gpios()
235 return dev_err_probe(dev, PTR_ERR(data->dok), in max8903_setup_gpios()
237 if (data->dok) { in max8903_setup_gpios()
238 gpiod_set_consumer_name(data->dok, data->psy_desc.name); in max8903_setup_gpios()
245 ta_in = gpiod_get_value(data->dok); in max8903_setup_gpios()
248 data->uok = devm_gpiod_get_optional(dev, "uok", GPIOD_IN); in max8903_setup_gpios()
249 if (IS_ERR(data->uok)) in max8903_setup_gpios()
250 return dev_err_probe(dev, PTR_ERR(data->uok), in max8903_setup_gpios()
252 if (data->uok) { in max8903_setup_gpios()
253 gpiod_set_consumer_name(data->uok, data->psy_desc.name); in max8903_setup_gpios()
260 usb_in = gpiod_get_value(data->uok); in max8903_setup_gpios()
264 if (!data->dok && !data->uok) { in max8903_setup_gpios()
266 return -EINVAL; in max8903_setup_gpios()
282 data->cen = devm_gpiod_get(dev, "cen", flags); in max8903_setup_gpios()
283 if (IS_ERR(data->cen)) in max8903_setup_gpios()
284 return dev_err_probe(dev, PTR_ERR(data->cen), in max8903_setup_gpios()
286 gpiod_set_consumer_name(data->cen, data->psy_desc.name); in max8903_setup_gpios()
296 data->dcm = devm_gpiod_get_optional(dev, "dcm", flags); in max8903_setup_gpios()
297 if (IS_ERR(data->dcm)) in max8903_setup_gpios()
298 return dev_err_probe(dev, PTR_ERR(data->dcm), in max8903_setup_gpios()
300 gpiod_set_consumer_name(data->dcm, data->psy_desc.name); in max8903_setup_gpios()
302 data->chg = devm_gpiod_get_optional(dev, "chg", GPIOD_IN); in max8903_setup_gpios()
303 if (IS_ERR(data->chg)) in max8903_setup_gpios()
304 return dev_err_probe(dev, PTR_ERR(data->chg), in max8903_setup_gpios()
305 "failed to get CHG GPIO"); in max8903_setup_gpios()
306 gpiod_set_consumer_name(data->chg, data->psy_desc.name); in max8903_setup_gpios()
308 data->flt = devm_gpiod_get_optional(dev, "flt", GPIOD_IN); in max8903_setup_gpios()
309 if (IS_ERR(data->flt)) in max8903_setup_gpios()
310 return dev_err_probe(dev, PTR_ERR(data->flt), in max8903_setup_gpios()
312 gpiod_set_consumer_name(data->flt, data->psy_desc.name); in max8903_setup_gpios()
314 data->usus = devm_gpiod_get_optional(dev, "usus", GPIOD_IN); in max8903_setup_gpios()
315 if (IS_ERR(data->usus)) in max8903_setup_gpios()
316 return dev_err_probe(dev, PTR_ERR(data->usus), in max8903_setup_gpios()
318 gpiod_set_consumer_name(data->usus, data->psy_desc.name); in max8903_setup_gpios()
320 data->fault = false; in max8903_setup_gpios()
321 data->ta_in = ta_in; in max8903_setup_gpios()
322 data->usb_in = usb_in; in max8903_setup_gpios()
330 struct device *dev = &pdev->dev; in max8903_probe()
336 return -ENOMEM; in max8903_probe()
338 data->dev = dev; in max8903_probe()
345 data->psy_desc.name = "max8903_charger"; in max8903_probe()
346 data->psy_desc.type = (data->ta_in) ? POWER_SUPPLY_TYPE_MAINS : in max8903_probe()
347 ((data->usb_in) ? POWER_SUPPLY_TYPE_USB : in max8903_probe()
349 data->psy_desc.get_property = max8903_get_property; in max8903_probe()
350 data->psy_desc.properties = max8903_charger_props; in max8903_probe()
351 data->psy_desc.num_properties = ARRAY_SIZE(max8903_charger_props); in max8903_probe()
353 psy_cfg.of_node = dev->of_node; in max8903_probe()
356 data->psy = devm_power_supply_register(dev, &data->psy_desc, &psy_cfg); in max8903_probe()
357 if (IS_ERR(data->psy)) { in max8903_probe()
359 return PTR_ERR(data->psy); in max8903_probe()
362 if (data->dok) { in max8903_probe()
363 ret = devm_request_threaded_irq(dev, gpiod_to_irq(data->dok), in max8903_probe()
370 gpiod_to_irq(data->dok), ret); in max8903_probe()
375 if (data->uok) { in max8903_probe()
376 ret = devm_request_threaded_irq(dev, gpiod_to_irq(data->uok), in max8903_probe()
383 gpiod_to_irq(data->uok), ret); in max8903_probe()
388 if (data->flt) { in max8903_probe()
389 ret = devm_request_threaded_irq(dev, gpiod_to_irq(data->flt), in max8903_probe()
396 gpiod_to_irq(data->flt), ret); in max8903_probe()
413 .name = "max8903-charger",
423 MODULE_ALIAS("platform:max8903-charger");