Lines Matching +full:gpio +full:- +full:charger

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
9 #include <linux/gpio.h>
31 POWER_SUPPLY_PROP_STATUS, /* Charger status output */
44 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; in max8903_get_property()
45 if (gpio_is_valid(data->pdata->chg)) { in max8903_get_property()
46 if (gpio_get_value(data->pdata->chg) == 0) in max8903_get_property()
47 val->intval = POWER_SUPPLY_STATUS_CHARGING; in max8903_get_property()
48 else if (data->usb_in || data->ta_in) in max8903_get_property()
49 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in max8903_get_property()
51 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in max8903_get_property()
55 val->intval = 0; in max8903_get_property()
56 if (data->usb_in || data->ta_in) in max8903_get_property()
57 val->intval = 1; in max8903_get_property()
60 val->intval = POWER_SUPPLY_HEALTH_GOOD; in max8903_get_property()
61 if (data->fault) in max8903_get_property()
62 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; in max8903_get_property()
65 return -EINVAL; in max8903_get_property()
74 struct max8903_pdata *pdata = data->pdata; in max8903_dcin()
78 ta_in = gpio_get_value(pdata->dok) ? false : true; in max8903_dcin()
80 if (ta_in == data->ta_in) in max8903_dcin()
83 data->ta_in = ta_in; in max8903_dcin()
85 /* Set Current-Limit-Mode 1:DC 0:USB */ in max8903_dcin()
86 if (gpio_is_valid(pdata->dcm)) in max8903_dcin()
87 gpio_set_value(pdata->dcm, ta_in ? 1 : 0); in max8903_dcin()
89 /* Charger Enable / Disable (cen is negated) */ in max8903_dcin()
90 if (gpio_is_valid(pdata->cen)) in max8903_dcin()
91 gpio_set_value(pdata->cen, ta_in ? 0 : in max8903_dcin()
92 (data->usb_in ? 0 : 1)); in max8903_dcin()
94 dev_dbg(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ? in max8903_dcin()
97 old_type = data->psy_desc.type; in max8903_dcin()
99 if (data->ta_in) in max8903_dcin()
100 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; in max8903_dcin()
101 else if (data->usb_in) in max8903_dcin()
102 data->psy_desc.type = POWER_SUPPLY_TYPE_USB; in max8903_dcin()
104 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY; in max8903_dcin()
106 if (old_type != data->psy_desc.type) in max8903_dcin()
107 power_supply_changed(data->psy); in max8903_dcin()
115 struct max8903_pdata *pdata = data->pdata; in max8903_usbin()
119 usb_in = gpio_get_value(pdata->uok) ? false : true; in max8903_usbin()
121 if (usb_in == data->usb_in) in max8903_usbin()
124 data->usb_in = usb_in; in max8903_usbin()
126 /* Do not touch Current-Limit-Mode */ in max8903_usbin()
128 /* Charger Enable / Disable (cen is negated) */ in max8903_usbin()
129 if (gpio_is_valid(pdata->cen)) in max8903_usbin()
130 gpio_set_value(pdata->cen, usb_in ? 0 : in max8903_usbin()
131 (data->ta_in ? 0 : 1)); in max8903_usbin()
133 dev_dbg(data->dev, "USB Charger %s.\n", usb_in ? in max8903_usbin()
136 old_type = data->psy_desc.type; in max8903_usbin()
138 if (data->ta_in) in max8903_usbin()
139 data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS; in max8903_usbin()
140 else if (data->usb_in) in max8903_usbin()
141 data->psy_desc.type = POWER_SUPPLY_TYPE_USB; in max8903_usbin()
143 data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY; in max8903_usbin()
145 if (old_type != data->psy_desc.type) in max8903_usbin()
146 power_supply_changed(data->psy); in max8903_usbin()
154 struct max8903_pdata *pdata = data->pdata; in max8903_fault()
157 fault = gpio_get_value(pdata->flt) ? false : true; in max8903_fault()
159 if (fault == data->fault) in max8903_fault()
162 data->fault = fault; in max8903_fault()
165 dev_err(data->dev, "Charger suffers a fault and stops.\n"); in max8903_fault()
167 dev_err(data->dev, "Charger recovered from a fault.\n"); in max8903_fault()
174 struct device_node *np = dev->of_node; in max8903_parse_dt_data()
184 pdata->dc_valid = false; in max8903_parse_dt_data()
185 pdata->usb_valid = false; in max8903_parse_dt_data()
187 pdata->cen = of_get_named_gpio(np, "cen-gpios", 0); in max8903_parse_dt_data()
188 if (!gpio_is_valid(pdata->cen)) in max8903_parse_dt_data()
189 pdata->cen = -EINVAL; in max8903_parse_dt_data()
191 pdata->chg = of_get_named_gpio(np, "chg-gpios", 0); in max8903_parse_dt_data()
192 if (!gpio_is_valid(pdata->chg)) in max8903_parse_dt_data()
193 pdata->chg = -EINVAL; in max8903_parse_dt_data()
195 pdata->flt = of_get_named_gpio(np, "flt-gpios", 0); in max8903_parse_dt_data()
196 if (!gpio_is_valid(pdata->flt)) in max8903_parse_dt_data()
197 pdata->flt = -EINVAL; in max8903_parse_dt_data()
199 pdata->usus = of_get_named_gpio(np, "usus-gpios", 0); in max8903_parse_dt_data()
200 if (!gpio_is_valid(pdata->usus)) in max8903_parse_dt_data()
201 pdata->usus = -EINVAL; in max8903_parse_dt_data()
203 pdata->dcm = of_get_named_gpio(np, "dcm-gpios", 0); in max8903_parse_dt_data()
204 if (!gpio_is_valid(pdata->dcm)) in max8903_parse_dt_data()
205 pdata->dcm = -EINVAL; in max8903_parse_dt_data()
207 pdata->dok = of_get_named_gpio(np, "dok-gpios", 0); in max8903_parse_dt_data()
208 if (!gpio_is_valid(pdata->dok)) in max8903_parse_dt_data()
209 pdata->dok = -EINVAL; in max8903_parse_dt_data()
211 pdata->dc_valid = true; in max8903_parse_dt_data()
213 pdata->uok = of_get_named_gpio(np, "uok-gpios", 0); in max8903_parse_dt_data()
214 if (!gpio_is_valid(pdata->uok)) in max8903_parse_dt_data()
215 pdata->uok = -EINVAL; in max8903_parse_dt_data()
217 pdata->usb_valid = true; in max8903_parse_dt_data()
225 struct device *dev = &pdev->dev; in max8903_setup_gpios()
226 struct max8903_pdata *pdata = pdev->dev.platform_data; in max8903_setup_gpios()
228 int gpio; in max8903_setup_gpios() local
232 if (pdata->dc_valid) { in max8903_setup_gpios()
233 if (gpio_is_valid(pdata->dok)) { in max8903_setup_gpios()
234 ret = devm_gpio_request(dev, pdata->dok, in max8903_setup_gpios()
235 data->psy_desc.name); in max8903_setup_gpios()
238 "Failed GPIO request for dok: %d err %d\n", in max8903_setup_gpios()
239 pdata->dok, ret); in max8903_setup_gpios()
243 gpio = pdata->dok; /* PULL_UPed Interrupt */ in max8903_setup_gpios()
244 ta_in = gpio_get_value(gpio) ? 0 : 1; in max8903_setup_gpios()
247 return -EINVAL; in max8903_setup_gpios()
251 if (gpio_is_valid(pdata->dcm)) { in max8903_setup_gpios()
252 ret = devm_gpio_request(dev, pdata->dcm, data->psy_desc.name); in max8903_setup_gpios()
255 "Failed GPIO request for dcm: %d err %d\n", in max8903_setup_gpios()
256 pdata->dcm, ret); in max8903_setup_gpios()
260 gpio = pdata->dcm; /* Output */ in max8903_setup_gpios()
261 gpio_set_value(gpio, ta_in); in max8903_setup_gpios()
264 if (pdata->usb_valid) { in max8903_setup_gpios()
265 if (gpio_is_valid(pdata->uok)) { in max8903_setup_gpios()
266 ret = devm_gpio_request(dev, pdata->uok, in max8903_setup_gpios()
267 data->psy_desc.name); in max8903_setup_gpios()
270 "Failed GPIO request for uok: %d err %d\n", in max8903_setup_gpios()
271 pdata->uok, ret); in max8903_setup_gpios()
275 gpio = pdata->uok; in max8903_setup_gpios()
276 usb_in = gpio_get_value(gpio) ? 0 : 1; in max8903_setup_gpios()
280 return -EINVAL; in max8903_setup_gpios()
284 if (gpio_is_valid(pdata->cen)) { in max8903_setup_gpios()
285 ret = devm_gpio_request(dev, pdata->cen, data->psy_desc.name); in max8903_setup_gpios()
288 "Failed GPIO request for cen: %d err %d\n", in max8903_setup_gpios()
289 pdata->cen, ret); in max8903_setup_gpios()
293 gpio_set_value(pdata->cen, (ta_in || usb_in) ? 0 : 1); in max8903_setup_gpios()
296 if (gpio_is_valid(pdata->chg)) { in max8903_setup_gpios()
297 ret = devm_gpio_request(dev, pdata->chg, data->psy_desc.name); in max8903_setup_gpios()
300 "Failed GPIO request for chg: %d err %d\n", in max8903_setup_gpios()
301 pdata->chg, ret); in max8903_setup_gpios()
306 if (gpio_is_valid(pdata->flt)) { in max8903_setup_gpios()
307 ret = devm_gpio_request(dev, pdata->flt, data->psy_desc.name); in max8903_setup_gpios()
310 "Failed GPIO request for flt: %d err %d\n", in max8903_setup_gpios()
311 pdata->flt, ret); in max8903_setup_gpios()
316 if (gpio_is_valid(pdata->usus)) { in max8903_setup_gpios()
317 ret = devm_gpio_request(dev, pdata->usus, data->psy_desc.name); in max8903_setup_gpios()
320 "Failed GPIO request for usus: %d err %d\n", in max8903_setup_gpios()
321 pdata->usus, ret); in max8903_setup_gpios()
326 data->fault = false; in max8903_setup_gpios()
327 data->ta_in = ta_in; in max8903_setup_gpios()
328 data->usb_in = usb_in; in max8903_setup_gpios()
336 struct device *dev = &pdev->dev; in max8903_probe()
337 struct max8903_pdata *pdata = pdev->dev.platform_data; in max8903_probe()
343 return -ENOMEM; in max8903_probe()
345 if (IS_ENABLED(CONFIG_OF) && !pdata && dev->of_node) in max8903_probe()
350 return -EINVAL; in max8903_probe()
353 pdev->dev.platform_data = pdata; in max8903_probe()
354 data->pdata = pdata; in max8903_probe()
355 data->dev = dev; in max8903_probe()
358 if (pdata->dc_valid == false && pdata->usb_valid == false) { in max8903_probe()
360 return -EINVAL; in max8903_probe()
367 data->psy_desc.name = "max8903_charger"; in max8903_probe()
368 data->psy_desc.type = (data->ta_in) ? POWER_SUPPLY_TYPE_MAINS : in max8903_probe()
369 ((data->usb_in) ? POWER_SUPPLY_TYPE_USB : in max8903_probe()
371 data->psy_desc.get_property = max8903_get_property; in max8903_probe()
372 data->psy_desc.properties = max8903_charger_props; in max8903_probe()
373 data->psy_desc.num_properties = ARRAY_SIZE(max8903_charger_props); in max8903_probe()
375 psy_cfg.of_node = dev->of_node; in max8903_probe()
378 data->psy = devm_power_supply_register(dev, &data->psy_desc, &psy_cfg); in max8903_probe()
379 if (IS_ERR(data->psy)) { in max8903_probe()
381 return PTR_ERR(data->psy); in max8903_probe()
384 if (pdata->dc_valid) { in max8903_probe()
385 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->dok), in max8903_probe()
392 gpio_to_irq(pdata->dok), ret); in max8903_probe()
397 if (pdata->usb_valid) { in max8903_probe()
398 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->uok), in max8903_probe()
405 gpio_to_irq(pdata->uok), ret); in max8903_probe()
410 if (gpio_is_valid(pdata->flt)) { in max8903_probe()
411 ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->flt), in max8903_probe()
418 gpio_to_irq(pdata->flt), ret); in max8903_probe()
435 .name = "max8903-charger",
443 MODULE_DESCRIPTION("MAX8903 Charger Driver");
445 MODULE_ALIAS("platform:max8903-charger");