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

2  * Battery charger driver for TI BQ24735
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <linux/gpio.h>
28 #include <linux/gpio/consumer.h>
32 #include <linux/power/bq24735-charger.h>
47 struct power_supply *charger; member
108 static int bq24735_config_charger(struct bq24735 *charger) in bq24735_config_charger() argument
110 struct bq24735_platform *pdata = charger->pdata; in bq24735_config_charger()
114 if (pdata->ext_control) in bq24735_config_charger()
117 if (pdata->charge_current) { in bq24735_config_charger()
118 value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK; in bq24735_config_charger()
120 ret = bq24735_write_word(charger->client, in bq24735_config_charger()
123 dev_err(&charger->client->dev, in bq24735_config_charger()
124 "Failed to write charger current : %d\n", in bq24735_config_charger()
130 if (pdata->charge_voltage) { in bq24735_config_charger()
131 value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK; in bq24735_config_charger()
133 ret = bq24735_write_word(charger->client, in bq24735_config_charger()
136 dev_err(&charger->client->dev, in bq24735_config_charger()
137 "Failed to write charger voltage : %d\n", in bq24735_config_charger()
143 if (pdata->input_current) { in bq24735_config_charger()
144 value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK; in bq24735_config_charger()
146 ret = bq24735_write_word(charger->client, in bq24735_config_charger()
149 dev_err(&charger->client->dev, in bq24735_config_charger()
159 static inline int bq24735_enable_charging(struct bq24735 *charger) in bq24735_enable_charging() argument
163 if (charger->pdata->ext_control) in bq24735_enable_charging()
166 ret = bq24735_config_charger(charger); in bq24735_enable_charging()
170 return bq24735_update_word(charger->client, BQ24735_CHG_OPT, in bq24735_enable_charging()
174 static inline int bq24735_disable_charging(struct bq24735 *charger) in bq24735_disable_charging() argument
176 if (charger->pdata->ext_control) in bq24735_disable_charging()
179 return bq24735_update_word(charger->client, BQ24735_CHG_OPT, in bq24735_disable_charging()
184 static bool bq24735_charger_is_present(struct bq24735 *charger) in bq24735_charger_is_present() argument
186 if (charger->status_gpio) { in bq24735_charger_is_present()
187 return !gpiod_get_value_cansleep(charger->status_gpio); in bq24735_charger_is_present()
191 ac = bq24735_read_word(charger->client, BQ24735_CHG_OPT); in bq24735_charger_is_present()
193 dev_dbg(&charger->client->dev, in bq24735_charger_is_present()
194 "Failed to read charger options : %d\n", in bq24735_charger_is_present()
204 static int bq24735_charger_is_charging(struct bq24735 *charger) in bq24735_charger_is_charging() argument
208 if (!bq24735_charger_is_present(charger)) in bq24735_charger_is_charging()
211 ret = bq24735_read_word(charger->client, BQ24735_CHG_OPT); in bq24735_charger_is_charging()
218 static void bq24735_update(struct bq24735 *charger) in bq24735_update() argument
220 mutex_lock(&charger->lock); in bq24735_update()
222 if (charger->charging && bq24735_charger_is_present(charger)) in bq24735_update()
223 bq24735_enable_charging(charger); in bq24735_update()
225 bq24735_disable_charging(charger); in bq24735_update()
227 mutex_unlock(&charger->lock); in bq24735_update()
229 power_supply_changed(charger->charger); in bq24735_update()
235 struct bq24735 *charger = to_bq24735(psy); in bq24735_charger_isr() local
237 bq24735_update(charger); in bq24735_charger_isr()
244 struct bq24735 *charger = container_of(work, struct bq24735, poll.work); in bq24735_poll() local
246 bq24735_update(charger); in bq24735_poll()
248 schedule_delayed_work(&charger->poll, in bq24735_poll()
249 msecs_to_jiffies(charger->poll_interval)); in bq24735_poll()
256 struct bq24735 *charger = to_bq24735(psy); in bq24735_charger_get_property() local
260 val->intval = bq24735_charger_is_present(charger) ? 1 : 0; in bq24735_charger_get_property()
263 switch (bq24735_charger_is_charging(charger)) { in bq24735_charger_get_property()
265 val->intval = POWER_SUPPLY_STATUS_CHARGING; in bq24735_charger_get_property()
268 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in bq24735_charger_get_property()
271 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; in bq24735_charger_get_property()
276 return -EINVAL; in bq24735_charger_get_property()
286 struct bq24735 *charger = to_bq24735(psy); in bq24735_charger_set_property() local
291 switch (val->intval) { in bq24735_charger_set_property()
293 mutex_lock(&charger->lock); in bq24735_charger_set_property()
294 charger->charging = true; in bq24735_charger_set_property()
295 ret = bq24735_enable_charging(charger); in bq24735_charger_set_property()
296 mutex_unlock(&charger->lock); in bq24735_charger_set_property()
302 mutex_lock(&charger->lock); in bq24735_charger_set_property()
303 charger->charging = false; in bq24735_charger_set_property()
304 ret = bq24735_disable_charging(charger); in bq24735_charger_set_property()
305 mutex_unlock(&charger->lock); in bq24735_charger_set_property()
310 return -EINVAL; in bq24735_charger_set_property()
315 return -EPERM; in bq24735_charger_set_property()
324 struct device_node *np = client->dev.of_node; in bq24735_parse_dt_data()
328 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); in bq24735_parse_dt_data()
330 dev_err(&client->dev, in bq24735_parse_dt_data()
335 ret = of_property_read_u32(np, "ti,charge-current", &val); in bq24735_parse_dt_data()
337 pdata->charge_current = val; in bq24735_parse_dt_data()
339 ret = of_property_read_u32(np, "ti,charge-voltage", &val); in bq24735_parse_dt_data()
341 pdata->charge_voltage = val; in bq24735_parse_dt_data()
343 ret = of_property_read_u32(np, "ti,input-current", &val); in bq24735_parse_dt_data()
345 pdata->input_current = val; in bq24735_parse_dt_data()
347 pdata->ext_control = of_property_read_bool(np, "ti,external-control"); in bq24735_parse_dt_data()
356 struct bq24735 *charger; in bq24735_charger_probe() local
361 charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL); in bq24735_charger_probe()
362 if (!charger) in bq24735_charger_probe()
363 return -ENOMEM; in bq24735_charger_probe()
365 mutex_init(&charger->lock); in bq24735_charger_probe()
366 charger->charging = true; in bq24735_charger_probe()
367 charger->pdata = client->dev.platform_data; in bq24735_charger_probe()
369 if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node) in bq24735_charger_probe()
370 charger->pdata = bq24735_parse_dt_data(client); in bq24735_charger_probe()
372 if (!charger->pdata) { in bq24735_charger_probe()
373 dev_err(&client->dev, "no platform data provided\n"); in bq24735_charger_probe()
374 return -EINVAL; in bq24735_charger_probe()
377 name = (char *)charger->pdata->name; in bq24735_charger_probe()
379 name = devm_kasprintf(&client->dev, GFP_KERNEL, in bq24735_charger_probe()
381 dev_name(&client->dev)); in bq24735_charger_probe()
383 dev_err(&client->dev, "Failed to alloc device name\n"); in bq24735_charger_probe()
384 return -ENOMEM; in bq24735_charger_probe()
388 charger->client = client; in bq24735_charger_probe()
390 supply_desc = &charger->charger_desc; in bq24735_charger_probe()
392 supply_desc->name = name; in bq24735_charger_probe()
393 supply_desc->type = POWER_SUPPLY_TYPE_MAINS; in bq24735_charger_probe()
394 supply_desc->properties = bq24735_charger_properties; in bq24735_charger_probe()
395 supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties); in bq24735_charger_probe()
396 supply_desc->get_property = bq24735_charger_get_property; in bq24735_charger_probe()
397 supply_desc->set_property = bq24735_charger_set_property; in bq24735_charger_probe()
398 supply_desc->property_is_writeable = in bq24735_charger_probe()
401 psy_cfg.supplied_to = charger->pdata->supplied_to; in bq24735_charger_probe()
402 psy_cfg.num_supplicants = charger->pdata->num_supplicants; in bq24735_charger_probe()
403 psy_cfg.of_node = client->dev.of_node; in bq24735_charger_probe()
404 psy_cfg.drv_data = charger; in bq24735_charger_probe()
406 i2c_set_clientdata(client, charger); in bq24735_charger_probe()
408 charger->status_gpio = devm_gpiod_get_optional(&client->dev, in bq24735_charger_probe()
409 "ti,ac-detect", in bq24735_charger_probe()
411 if (IS_ERR(charger->status_gpio)) { in bq24735_charger_probe()
412 ret = PTR_ERR(charger->status_gpio); in bq24735_charger_probe()
413 dev_err(&client->dev, "Getting gpio failed: %d\n", ret); in bq24735_charger_probe()
417 if (bq24735_charger_is_present(charger)) { in bq24735_charger_probe()
420 dev_err(&client->dev, "Failed to read manufacturer id : %d\n", in bq24735_charger_probe()
424 dev_err(&client->dev, in bq24735_charger_probe()
426 return -ENODEV; in bq24735_charger_probe()
431 dev_err(&client->dev, "Failed to read device id : %d\n", ret); in bq24735_charger_probe()
434 dev_err(&client->dev, in bq24735_charger_probe()
436 return -ENODEV; in bq24735_charger_probe()
439 ret = bq24735_enable_charging(charger); in bq24735_charger_probe()
441 dev_err(&client->dev, "Failed to enable charging\n"); in bq24735_charger_probe()
446 charger->charger = devm_power_supply_register(&client->dev, supply_desc, in bq24735_charger_probe()
448 if (IS_ERR(charger->charger)) { in bq24735_charger_probe()
449 ret = PTR_ERR(charger->charger); in bq24735_charger_probe()
450 dev_err(&client->dev, "Failed to register power supply: %d\n", in bq24735_charger_probe()
455 if (client->irq) { in bq24735_charger_probe()
456 ret = devm_request_threaded_irq(&client->dev, client->irq, in bq24735_charger_probe()
461 supply_desc->name, in bq24735_charger_probe()
462 charger->charger); in bq24735_charger_probe()
464 dev_err(&client->dev, in bq24735_charger_probe()
466 client->irq, ret); in bq24735_charger_probe()
470 ret = device_property_read_u32(&client->dev, "poll-interval", in bq24735_charger_probe()
471 &charger->poll_interval); in bq24735_charger_probe()
474 if (!charger->poll_interval) in bq24735_charger_probe()
477 INIT_DELAYED_WORK(&charger->poll, bq24735_poll); in bq24735_charger_probe()
478 schedule_delayed_work(&charger->poll, in bq24735_charger_probe()
479 msecs_to_jiffies(charger->poll_interval)); in bq24735_charger_probe()
487 struct bq24735 *charger = i2c_get_clientdata(client); in bq24735_charger_remove() local
489 if (charger->poll_interval) in bq24735_charger_remove()
490 cancel_delayed_work_sync(&charger->poll); in bq24735_charger_remove()
496 { "bq24735-charger", 0 },
509 .name = "bq24735-charger",