Lines Matching +full:usb +full:- +full:charger
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
27 struct gpio_desc *cen; /* Charger Enable input */
29 struct gpio_desc *uok; /* USB Power OK output */
30 struct gpio_desc *chg; /* Charger status output */
32 struct gpio_desc *dcm; /* Current-Limit Mode input (1: DC, 2: USB) */
33 struct gpio_desc *usus; /* USB Suspend Input (1: suspended) */
40 POWER_SUPPLY_PROP_STATUS, /* Charger status output */
53 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; in max8903_get_property()
54 if (data->chg) { in max8903_get_property()
55 if (gpiod_get_value(data->chg)) in max8903_get_property()
57 val->intval = POWER_SUPPLY_STATUS_CHARGING; in max8903_get_property()
58 else if (data->usb_in || data->ta_in) in max8903_get_property()
59 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in max8903_get_property()
61 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in max8903_get_property()
65 val->intval = 0; in max8903_get_property()
66 if (data->usb_in || data->ta_in) in max8903_get_property()
67 val->intval = 1; in max8903_get_property()
70 val->intval = POWER_SUPPLY_HEALTH_GOOD; in max8903_get_property()
71 if (data->fault) in max8903_get_property()
72 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; in max8903_get_property()
75 return -EINVAL; in max8903_get_property()
94 ta_in = gpiod_get_value(data->dok); in max8903_dcin()
96 if (ta_in == data->ta_in) in max8903_dcin()
99 data->ta_in = ta_in; in max8903_dcin()
101 /* Set Current-Limit-Mode 1:DC 0:USB */ in max8903_dcin()
102 if (data->dcm) in max8903_dcin()
103 gpiod_set_value(data->dcm, ta_in); in max8903_dcin()
105 /* Charger Enable / Disable */ in max8903_dcin()
106 if (data->cen) { in max8903_dcin()
112 else if (data->usb_in) in max8903_dcin()
113 /* Enable if the USB charger is enabled */ in max8903_dcin()
116 /* Else default-disable */ in max8903_dcin()
119 gpiod_set_value(data->cen, val); in max8903_dcin()
122 dev_dbg(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ? in max8903_dcin()
125 old_type = data->psy_desc.type; in max8903_dcin()
127 if (data->ta_in) in max8903_dcin()
128 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; in max8903_dcin()
129 else if (data->usb_in) in max8903_dcin()
130 data->psy_desc.type = POWER_SUPPLY_TYPE_USB; in max8903_dcin()
132 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY; in max8903_dcin()
134 if (old_type != data->psy_desc.type) in max8903_dcin()
135 power_supply_changed(data->psy); in max8903_dcin()
153 usb_in = gpiod_get_value(data->uok); in max8903_usbin()
155 if (usb_in == data->usb_in) in max8903_usbin()
158 data->usb_in = usb_in; in max8903_usbin()
160 /* Do not touch Current-Limit-Mode */ in max8903_usbin()
162 /* Charger Enable / Disable */ in max8903_usbin()
163 if (data->cen) { in max8903_usbin()
169 else if (data->ta_in) in max8903_usbin()
170 /* Enable if the DC charger is enabled */ in max8903_usbin()
173 /* Else default-disable */ in max8903_usbin()
176 gpiod_set_value(data->cen, val); in max8903_usbin()
179 dev_dbg(data->dev, "USB Charger %s.\n", usb_in ? in max8903_usbin()
182 old_type = data->psy_desc.type; in max8903_usbin()
184 if (data->ta_in) in max8903_usbin()
185 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; in max8903_usbin()
186 else if (data->usb_in) in max8903_usbin()
187 data->psy_desc.type = POWER_SUPPLY_TYPE_USB; in max8903_usbin()
189 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY; in max8903_usbin()
191 if (old_type != data->psy_desc.type) in max8903_usbin()
192 power_supply_changed(data->psy); in max8903_usbin()
209 fault = gpiod_get_value(data->flt); in max8903_fault()
211 if (fault == data->fault) in max8903_fault()
214 data->fault = fault; in max8903_fault()
217 dev_err(data->dev, "Charger suffers a fault and stops.\n"); in max8903_fault()
219 dev_err(data->dev, "Charger recovered from a fault.\n"); in max8903_fault()
227 struct device *dev = &pdev->dev; in max8903_setup_gpios()
232 data->dok = devm_gpiod_get_optional(dev, "dok", GPIOD_IN); in max8903_setup_gpios()
233 if (IS_ERR(data->dok)) in max8903_setup_gpios()
234 return dev_err_probe(dev, PTR_ERR(data->dok), in max8903_setup_gpios()
236 if (data->dok) { in max8903_setup_gpios()
237 gpiod_set_consumer_name(data->dok, data->psy_desc.name); in max8903_setup_gpios()
239 * The DC OK is pulled up to 1 and goes low when a charger in max8903_setup_gpios()
242 * here if the DC charger is plugged in. in max8903_setup_gpios()
244 ta_in = gpiod_get_value(data->dok); in max8903_setup_gpios()
247 data->uok = devm_gpiod_get_optional(dev, "uok", GPIOD_IN); in max8903_setup_gpios()
248 if (IS_ERR(data->uok)) in max8903_setup_gpios()
249 return dev_err_probe(dev, PTR_ERR(data->uok), in max8903_setup_gpios()
251 if (data->uok) { in max8903_setup_gpios()
252 gpiod_set_consumer_name(data->uok, data->psy_desc.name); in max8903_setup_gpios()
254 * The USB OK is pulled up to 1 and goes low when a USB charger in max8903_setup_gpios()
257 * here if the USB charger is plugged in. in max8903_setup_gpios()
259 usb_in = gpiod_get_value(data->uok); in max8903_setup_gpios()
262 /* Either DC OK or USB OK must be provided */ in max8903_setup_gpios()
263 if (!data->dok && !data->uok) { in max8903_setup_gpios()
265 return -EINVAL; in max8903_setup_gpios()
269 * If either charger is already connected at this point, in max8903_setup_gpios()
278 * If DC OK is provided, Charger Enable CEN is compulsory in max8903_setup_gpios()
281 data->cen = devm_gpiod_get(dev, "cen", flags); in max8903_setup_gpios()
282 if (IS_ERR(data->cen)) in max8903_setup_gpios()
283 return dev_err_probe(dev, PTR_ERR(data->cen), in max8903_setup_gpios()
285 gpiod_set_consumer_name(data->cen, data->psy_desc.name); in max8903_setup_gpios()
288 * If the DC charger is connected, then select it. in max8903_setup_gpios()
291 * device tree. Driving it high will enable the DC charger in max8903_setup_gpios()
292 * input over the USB charger input. in max8903_setup_gpios()
295 data->dcm = devm_gpiod_get_optional(dev, "dcm", flags); in max8903_setup_gpios()
296 if (IS_ERR(data->dcm)) in max8903_setup_gpios()
297 return dev_err_probe(dev, PTR_ERR(data->dcm), in max8903_setup_gpios()
299 gpiod_set_consumer_name(data->dcm, data->psy_desc.name); in max8903_setup_gpios()
301 data->chg = devm_gpiod_get_optional(dev, "chg", GPIOD_IN); in max8903_setup_gpios()
302 if (IS_ERR(data->chg)) in max8903_setup_gpios()
303 return dev_err_probe(dev, PTR_ERR(data->chg), in max8903_setup_gpios()
305 gpiod_set_consumer_name(data->chg, data->psy_desc.name); in max8903_setup_gpios()
307 data->flt = devm_gpiod_get_optional(dev, "flt", GPIOD_IN); in max8903_setup_gpios()
308 if (IS_ERR(data->flt)) in max8903_setup_gpios()
309 return dev_err_probe(dev, PTR_ERR(data->flt), in max8903_setup_gpios()
311 gpiod_set_consumer_name(data->flt, data->psy_desc.name); in max8903_setup_gpios()
313 data->usus = devm_gpiod_get_optional(dev, "usus", GPIOD_IN); in max8903_setup_gpios()
314 if (IS_ERR(data->usus)) in max8903_setup_gpios()
315 return dev_err_probe(dev, PTR_ERR(data->usus), in max8903_setup_gpios()
317 gpiod_set_consumer_name(data->usus, data->psy_desc.name); in max8903_setup_gpios()
319 data->fault = false; in max8903_setup_gpios()
320 data->ta_in = ta_in; in max8903_setup_gpios()
321 data->usb_in = usb_in; in max8903_setup_gpios()
329 struct device *dev = &pdev->dev; in max8903_probe()
335 return -ENOMEM; in max8903_probe()
337 data->dev = dev; in max8903_probe()
344 data->psy_desc.name = "max8903_charger"; in max8903_probe()
345 data->psy_desc.type = (data->ta_in) ? POWER_SUPPLY_TYPE_MAINS : in max8903_probe()
346 ((data->usb_in) ? POWER_SUPPLY_TYPE_USB : in max8903_probe()
348 data->psy_desc.get_property = max8903_get_property; in max8903_probe()
349 data->psy_desc.properties = max8903_charger_props; in max8903_probe()
350 data->psy_desc.num_properties = ARRAY_SIZE(max8903_charger_props); in max8903_probe()
352 psy_cfg.of_node = dev->of_node; in max8903_probe()
355 data->psy = devm_power_supply_register(dev, &data->psy_desc, &psy_cfg); in max8903_probe()
356 if (IS_ERR(data->psy)) { in max8903_probe()
358 return PTR_ERR(data->psy); in max8903_probe()
361 if (data->dok) { in max8903_probe()
362 ret = devm_request_threaded_irq(dev, gpiod_to_irq(data->dok), in max8903_probe()
369 gpiod_to_irq(data->dok), ret); in max8903_probe()
374 if (data->uok) { in max8903_probe()
375 ret = devm_request_threaded_irq(dev, gpiod_to_irq(data->uok), in max8903_probe()
379 "MAX8903 USB IN", data); in max8903_probe()
381 dev_err(dev, "Cannot request irq %d for USB (%d)\n", in max8903_probe()
382 gpiod_to_irq(data->uok), ret); in max8903_probe()
387 if (data->flt) { in max8903_probe()
388 ret = devm_request_threaded_irq(dev, gpiod_to_irq(data->flt), in max8903_probe()
395 gpiod_to_irq(data->flt), ret); in max8903_probe()
412 .name = "max8903-charger",
420 MODULE_DESCRIPTION("MAX8903 Charger Driver");
422 MODULE_ALIAS("platform:max8903-charger");