.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) ST-Ericsson SA 2012 |
---|
3 | 4 | * |
---|
4 | 5 | * Charger driver for AB8500 |
---|
5 | 6 | * |
---|
6 | | - * License Terms: GNU General Public License v2 |
---|
7 | 7 | * Author: |
---|
8 | 8 | * Johan Palsson <johan.palsson@stericsson.com> |
---|
9 | 9 | * Karl Komierowski <karl.komierowski@stericsson.com> |
---|
.. | .. |
---|
29 | 29 | #include <linux/mfd/abx500/ab8500.h> |
---|
30 | 30 | #include <linux/mfd/abx500.h> |
---|
31 | 31 | #include <linux/mfd/abx500/ab8500-bm.h> |
---|
32 | | -#include <linux/mfd/abx500/ab8500-gpadc.h> |
---|
33 | 32 | #include <linux/mfd/abx500/ux500_chargalg.h> |
---|
34 | 33 | #include <linux/usb/otg.h> |
---|
35 | 34 | #include <linux/mutex.h> |
---|
| 35 | +#include <linux/iio/consumer.h> |
---|
36 | 36 | |
---|
37 | 37 | /* Charger constants */ |
---|
38 | 38 | #define NO_PW_CONN 0 |
---|
.. | .. |
---|
233 | 233 | * @current_stepping_sessions: |
---|
234 | 234 | * Counter for current stepping sessions |
---|
235 | 235 | * @parent: Pointer to the struct ab8500 |
---|
236 | | - * @gpadc: Pointer to the struct gpadc |
---|
| 236 | + * @adc_main_charger_v ADC channel for main charger voltage |
---|
| 237 | + * @adc_main_charger_c ADC channel for main charger current |
---|
| 238 | + * @adc_vbus_v ADC channel for USB charger voltage |
---|
| 239 | + * @adc_usb_charger_c ADC channel for USB charger current |
---|
237 | 240 | * @bm: Platform specific battery management information |
---|
238 | 241 | * @flags: Structure for information about events triggered |
---|
239 | 242 | * @usb_state: Structure for usb stack information |
---|
.. | .. |
---|
283 | 286 | int is_aca_rid; |
---|
284 | 287 | atomic_t current_stepping_sessions; |
---|
285 | 288 | struct ab8500 *parent; |
---|
286 | | - struct ab8500_gpadc *gpadc; |
---|
| 289 | + struct iio_channel *adc_main_charger_v; |
---|
| 290 | + struct iio_channel *adc_main_charger_c; |
---|
| 291 | + struct iio_channel *adc_vbus_v; |
---|
| 292 | + struct iio_channel *adc_usb_charger_c; |
---|
287 | 293 | struct abx500_bm_data *bm; |
---|
288 | 294 | struct ab8500_charger_event_flags flags; |
---|
289 | 295 | struct ab8500_charger_usb_state usb_state; |
---|
.. | .. |
---|
398 | 404 | } |
---|
399 | 405 | |
---|
400 | 406 | /** |
---|
401 | | - * ab8500_power_supply_changed - a wrapper with local extentions for |
---|
| 407 | + * ab8500_power_supply_changed - a wrapper with local extensions for |
---|
402 | 408 | * power_supply_changed |
---|
403 | 409 | * @di: pointer to the ab8500_charger structure |
---|
404 | 410 | * @psy: pointer to power_supply_that have changed. |
---|
.. | .. |
---|
475 | 481 | */ |
---|
476 | 482 | static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di) |
---|
477 | 483 | { |
---|
478 | | - int vch; |
---|
| 484 | + int vch, ret; |
---|
479 | 485 | |
---|
480 | 486 | /* Only measure voltage if the charger is connected */ |
---|
481 | 487 | if (di->ac.charger_connected) { |
---|
482 | | - vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V); |
---|
483 | | - if (vch < 0) |
---|
484 | | - dev_err(di->dev, "%s gpadc conv failed,\n", __func__); |
---|
| 488 | + ret = iio_read_channel_processed(di->adc_main_charger_v, &vch); |
---|
| 489 | + if (ret < 0) |
---|
| 490 | + dev_err(di->dev, "%s ADC conv failed,\n", __func__); |
---|
485 | 491 | } else { |
---|
486 | 492 | vch = 0; |
---|
487 | 493 | } |
---|
.. | .. |
---|
526 | 532 | */ |
---|
527 | 533 | static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di) |
---|
528 | 534 | { |
---|
529 | | - int vch; |
---|
| 535 | + int vch, ret; |
---|
530 | 536 | |
---|
531 | 537 | /* Only measure voltage if the charger is connected */ |
---|
532 | 538 | if (di->usb.charger_connected) { |
---|
533 | | - vch = ab8500_gpadc_convert(di->gpadc, VBUS_V); |
---|
534 | | - if (vch < 0) |
---|
535 | | - dev_err(di->dev, "%s gpadc conv failed\n", __func__); |
---|
| 539 | + ret = iio_read_channel_processed(di->adc_vbus_v, &vch); |
---|
| 540 | + if (ret < 0) |
---|
| 541 | + dev_err(di->dev, "%s ADC conv failed,\n", __func__); |
---|
536 | 542 | } else { |
---|
537 | 543 | vch = 0; |
---|
538 | 544 | } |
---|
.. | .. |
---|
548 | 554 | */ |
---|
549 | 555 | static int ab8500_charger_get_usb_current(struct ab8500_charger *di) |
---|
550 | 556 | { |
---|
551 | | - int ich; |
---|
| 557 | + int ich, ret; |
---|
552 | 558 | |
---|
553 | 559 | /* Only measure current if the charger is online */ |
---|
554 | 560 | if (di->usb.charger_online) { |
---|
555 | | - ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C); |
---|
556 | | - if (ich < 0) |
---|
557 | | - dev_err(di->dev, "%s gpadc conv failed\n", __func__); |
---|
| 561 | + ret = iio_read_channel_processed(di->adc_usb_charger_c, &ich); |
---|
| 562 | + if (ret < 0) |
---|
| 563 | + dev_err(di->dev, "%s ADC conv failed,\n", __func__); |
---|
558 | 564 | } else { |
---|
559 | 565 | ich = 0; |
---|
560 | 566 | } |
---|
.. | .. |
---|
570 | 576 | */ |
---|
571 | 577 | static int ab8500_charger_get_ac_current(struct ab8500_charger *di) |
---|
572 | 578 | { |
---|
573 | | - int ich; |
---|
| 579 | + int ich, ret; |
---|
574 | 580 | |
---|
575 | 581 | /* Only measure current if the charger is online */ |
---|
576 | 582 | if (di->ac.charger_online) { |
---|
577 | | - ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C); |
---|
578 | | - if (ich < 0) |
---|
579 | | - dev_err(di->dev, "%s gpadc conv failed\n", __func__); |
---|
| 583 | + ret = iio_read_channel_processed(di->adc_main_charger_c, &ich); |
---|
| 584 | + if (ret < 0) |
---|
| 585 | + dev_err(di->dev, "%s ADC conv failed,\n", __func__); |
---|
580 | 586 | } else { |
---|
581 | 587 | ich = 0; |
---|
582 | 588 | } |
---|
.. | .. |
---|
693 | 699 | /* |
---|
694 | 700 | * Platform only supports USB 2.0. |
---|
695 | 701 | * This means that charging current from USB source |
---|
696 | | - * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_* |
---|
| 702 | + * is maximum 500 mA. Every occurrence of USB_STAT_*_HOST_* |
---|
697 | 703 | * should set USB_CH_IP_CUR_LVL_0P5. |
---|
698 | 704 | */ |
---|
699 | 705 | |
---|
.. | .. |
---|
758 | 764 | USB_CH_IP_CUR_LVL_1P5; |
---|
759 | 765 | break; |
---|
760 | 766 | } |
---|
| 767 | + fallthrough; |
---|
761 | 768 | case USB_STAT_HM_IDGND: |
---|
762 | 769 | dev_err(di->dev, "USB Type - Charging not allowed\n"); |
---|
763 | 770 | di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05; |
---|
.. | .. |
---|
798 | 805 | di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05; |
---|
799 | 806 | ret = -ENXIO; |
---|
800 | 807 | break; |
---|
801 | | - }; |
---|
| 808 | + } |
---|
802 | 809 | |
---|
803 | 810 | di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max; |
---|
804 | 811 | dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", |
---|
.. | .. |
---|
1088 | 1095 | di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05; |
---|
1089 | 1096 | ret = -EPERM; |
---|
1090 | 1097 | break; |
---|
1091 | | - }; |
---|
| 1098 | + } |
---|
1092 | 1099 | di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max; |
---|
1093 | 1100 | return ret; |
---|
1094 | 1101 | } |
---|
.. | .. |
---|
1388 | 1395 | |
---|
1389 | 1396 | /* |
---|
1390 | 1397 | * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts |
---|
1391 | | - * will be triggered everytime we enable the VDD ADC supply. |
---|
| 1398 | + * will be triggered every time we enable the VDD ADC supply. |
---|
1392 | 1399 | * This will turn off charging for a short while. |
---|
1393 | 1400 | * It can be avoided by having the supply on when |
---|
1394 | 1401 | * there is a charger enabled. Normally the VDD ADC supply |
---|
1395 | | - * is enabled everytime a GPADC conversion is triggered. We will |
---|
1396 | | - * force it to be enabled from this driver to have |
---|
1397 | | - * the GPADC module independant of the AB8500 chargers |
---|
| 1402 | + * is enabled every time a GPADC conversion is triggered. |
---|
| 1403 | + * We will force it to be enabled from this driver to have |
---|
| 1404 | + * the GPADC module independent of the AB8500 chargers |
---|
1398 | 1405 | */ |
---|
1399 | 1406 | if (!di->vddadc_en_ac) { |
---|
1400 | 1407 | ret = regulator_enable(di->regu); |
---|
.. | .. |
---|
1464 | 1471 | if (is_ab8500_1p1_or_earlier(di->parent)) { |
---|
1465 | 1472 | /* |
---|
1466 | 1473 | * For ABB revision 1.0 and 1.1 there is a bug in the |
---|
1467 | | - * watchdog logic. That means we have to continously |
---|
| 1474 | + * watchdog logic. That means we have to continuously |
---|
1468 | 1475 | * kick the charger watchdog even when no charger is |
---|
1469 | 1476 | * connected. This is only valid once the AC charger |
---|
1470 | 1477 | * has been enabled. This is a bug that is not handled |
---|
.. | .. |
---|
1561 | 1568 | |
---|
1562 | 1569 | /* |
---|
1563 | 1570 | * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts |
---|
1564 | | - * will be triggered everytime we enable the VDD ADC supply. |
---|
| 1571 | + * will be triggered every time we enable the VDD ADC supply. |
---|
1565 | 1572 | * This will turn off charging for a short while. |
---|
1566 | 1573 | * It can be avoided by having the supply on when |
---|
1567 | 1574 | * there is a charger enabled. Normally the VDD ADC supply |
---|
1568 | | - * is enabled everytime a GPADC conversion is triggered. We will |
---|
1569 | | - * force it to be enabled from this driver to have |
---|
1570 | | - * the GPADC module independant of the AB8500 chargers |
---|
| 1575 | + * is enabled every time a GPADC conversion is triggered. |
---|
| 1576 | + * We will force it to be enabled from this driver to have |
---|
| 1577 | + * the GPADC module independent of the AB8500 chargers |
---|
1571 | 1578 | */ |
---|
1572 | 1579 | if (!di->vddadc_en_usb) { |
---|
1573 | 1580 | ret = regulator_enable(di->regu); |
---|
.. | .. |
---|
1591 | 1598 | return -ENXIO; |
---|
1592 | 1599 | } |
---|
1593 | 1600 | |
---|
1594 | | - /* ChVoltLevel: max voltage upto which battery can be charged */ |
---|
| 1601 | + /* |
---|
| 1602 | + * ChVoltLevel: max voltage up to which battery can be |
---|
| 1603 | + * charged |
---|
| 1604 | + */ |
---|
1595 | 1605 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
---|
1596 | 1606 | AB8500_CH_VOLT_LVL_REG, (u8) volt_index); |
---|
1597 | 1607 | if (ret) { |
---|
.. | .. |
---|
2023 | 2033 | * Work queue function for kicking the charger watchdog. |
---|
2024 | 2034 | * |
---|
2025 | 2035 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog |
---|
2026 | | - * logic. That means we have to continously kick the charger |
---|
| 2036 | + * logic. That means we have to continuously kick the charger |
---|
2027 | 2037 | * watchdog even when no charger is connected. This is only |
---|
2028 | 2038 | * valid once the AC charger has been enabled. This is |
---|
2029 | 2039 | * a bug that is not handled by the algorithm and the |
---|
.. | .. |
---|
2271 | 2281 | * Some chargers that breaks the USB spec is |
---|
2272 | 2282 | * identified as invalid by AB8500 and it refuse |
---|
2273 | 2283 | * to start the charging process. but by jumping |
---|
2274 | | - * thru a few hoops it can be forced to start. |
---|
| 2284 | + * through a few hoops it can be forced to start. |
---|
2275 | 2285 | */ |
---|
2276 | 2286 | if (is_ab8500(di->parent)) |
---|
2277 | 2287 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, |
---|
.. | .. |
---|
2416 | 2426 | * of 1sec for enabling charging |
---|
2417 | 2427 | */ |
---|
2418 | 2428 | msleep(1000); |
---|
2419 | | - /* Intentional fall through */ |
---|
| 2429 | + fallthrough; |
---|
2420 | 2430 | case AB8500_BM_USB_STATE_CONFIGURED: |
---|
2421 | 2431 | /* |
---|
2422 | 2432 | * USB is configured, enable charging with the charging |
---|
.. | .. |
---|
2436 | 2446 | |
---|
2437 | 2447 | default: |
---|
2438 | 2448 | break; |
---|
2439 | | - }; |
---|
| 2449 | + } |
---|
2440 | 2450 | } |
---|
2441 | 2451 | |
---|
2442 | 2452 | /** |
---|
.. | .. |
---|
3026 | 3036 | static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) |
---|
3027 | 3037 | { |
---|
3028 | 3038 | int ret = 0; |
---|
3029 | | - u8 bup_vch_range = 0, vbup33_vrtcn = 0; |
---|
3030 | 3039 | |
---|
3031 | 3040 | /* Setup maximum charger current and voltage for ABB cut2.0 */ |
---|
3032 | 3041 | if (!is_ab8500_1p1_or_earlier(di->parent)) { |
---|
.. | .. |
---|
3127 | 3136 | goto out; |
---|
3128 | 3137 | } |
---|
3129 | 3138 | |
---|
3130 | | - /* Backup battery voltage and current */ |
---|
3131 | | - if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V) |
---|
3132 | | - bup_vch_range = BUP_VCH_RANGE; |
---|
3133 | | - if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V) |
---|
3134 | | - vbup33_vrtcn = VBUP33_VRTCN; |
---|
3135 | | - |
---|
3136 | 3139 | ret = abx500_set_register_interruptible(di->dev, |
---|
3137 | 3140 | AB8500_RTC, |
---|
3138 | 3141 | AB8500_RTC_BACKUP_CHG_REG, |
---|
.. | .. |
---|
3230 | 3233 | |
---|
3231 | 3234 | /* |
---|
3232 | 3235 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog |
---|
3233 | | - * logic. That means we have to continously kick the charger |
---|
| 3236 | + * logic. That means we have to continuously kick the charger |
---|
3234 | 3237 | * watchdog even when no charger is connected. This is only |
---|
3235 | 3238 | * valid once the AC charger has been enabled. This is |
---|
3236 | 3239 | * a bug that is not handled by the algorithm and the |
---|
.. | .. |
---|
3393 | 3396 | /* get parent data */ |
---|
3394 | 3397 | di->dev = &pdev->dev; |
---|
3395 | 3398 | di->parent = dev_get_drvdata(pdev->dev.parent); |
---|
3396 | | - di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); |
---|
| 3399 | + |
---|
| 3400 | + /* Get ADC channels */ |
---|
| 3401 | + di->adc_main_charger_v = devm_iio_channel_get(&pdev->dev, |
---|
| 3402 | + "main_charger_v"); |
---|
| 3403 | + if (IS_ERR(di->adc_main_charger_v)) { |
---|
| 3404 | + if (PTR_ERR(di->adc_main_charger_v) == -ENODEV) |
---|
| 3405 | + return -EPROBE_DEFER; |
---|
| 3406 | + dev_err(&pdev->dev, "failed to get ADC main charger voltage\n"); |
---|
| 3407 | + return PTR_ERR(di->adc_main_charger_v); |
---|
| 3408 | + } |
---|
| 3409 | + di->adc_main_charger_c = devm_iio_channel_get(&pdev->dev, |
---|
| 3410 | + "main_charger_c"); |
---|
| 3411 | + if (IS_ERR(di->adc_main_charger_c)) { |
---|
| 3412 | + if (PTR_ERR(di->adc_main_charger_c) == -ENODEV) |
---|
| 3413 | + return -EPROBE_DEFER; |
---|
| 3414 | + dev_err(&pdev->dev, "failed to get ADC main charger current\n"); |
---|
| 3415 | + return PTR_ERR(di->adc_main_charger_c); |
---|
| 3416 | + } |
---|
| 3417 | + di->adc_vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v"); |
---|
| 3418 | + if (IS_ERR(di->adc_vbus_v)) { |
---|
| 3419 | + if (PTR_ERR(di->adc_vbus_v) == -ENODEV) |
---|
| 3420 | + return -EPROBE_DEFER; |
---|
| 3421 | + dev_err(&pdev->dev, "failed to get ADC USB charger voltage\n"); |
---|
| 3422 | + return PTR_ERR(di->adc_vbus_v); |
---|
| 3423 | + } |
---|
| 3424 | + di->adc_usb_charger_c = devm_iio_channel_get(&pdev->dev, |
---|
| 3425 | + "usb_charger_c"); |
---|
| 3426 | + if (IS_ERR(di->adc_usb_charger_c)) { |
---|
| 3427 | + if (PTR_ERR(di->adc_usb_charger_c) == -ENODEV) |
---|
| 3428 | + return -EPROBE_DEFER; |
---|
| 3429 | + dev_err(&pdev->dev, "failed to get ADC USB charger current\n"); |
---|
| 3430 | + return PTR_ERR(di->adc_usb_charger_c); |
---|
| 3431 | + } |
---|
3397 | 3432 | |
---|
3398 | 3433 | /* initialize lock */ |
---|
3399 | 3434 | spin_lock_init(&di->usb_state.usb_lock); |
---|
.. | .. |
---|
3467 | 3502 | |
---|
3468 | 3503 | /* |
---|
3469 | 3504 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog |
---|
3470 | | - * logic. That means we have to continously kick the charger |
---|
| 3505 | + * logic. That means we have to continuously kick the charger |
---|
3471 | 3506 | * watchdog even when no charger is connected. This is only |
---|
3472 | 3507 | * valid once the AC charger has been enabled. This is |
---|
3473 | 3508 | * a bug that is not handled by the algorithm and the |
---|
.. | .. |
---|
3578 | 3613 | /* Register interrupts */ |
---|
3579 | 3614 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { |
---|
3580 | 3615 | irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); |
---|
| 3616 | + if (irq < 0) { |
---|
| 3617 | + ret = irq; |
---|
| 3618 | + goto free_irq; |
---|
| 3619 | + } |
---|
| 3620 | + |
---|
3581 | 3621 | ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr, |
---|
3582 | 3622 | IRQF_SHARED | IRQF_NO_SUSPEND, |
---|
3583 | 3623 | ab8500_charger_irq[i].name, di); |
---|