hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/power/supply/ab8500_charger.c
....@@ -1,9 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) ST-Ericsson SA 2012
34 *
45 * Charger driver for AB8500
56 *
6
- * License Terms: GNU General Public License v2
77 * Author:
88 * Johan Palsson <johan.palsson@stericsson.com>
99 * Karl Komierowski <karl.komierowski@stericsson.com>
....@@ -29,10 +29,10 @@
2929 #include <linux/mfd/abx500/ab8500.h>
3030 #include <linux/mfd/abx500.h>
3131 #include <linux/mfd/abx500/ab8500-bm.h>
32
-#include <linux/mfd/abx500/ab8500-gpadc.h>
3332 #include <linux/mfd/abx500/ux500_chargalg.h>
3433 #include <linux/usb/otg.h>
3534 #include <linux/mutex.h>
35
+#include <linux/iio/consumer.h>
3636
3737 /* Charger constants */
3838 #define NO_PW_CONN 0
....@@ -233,7 +233,10 @@
233233 * @current_stepping_sessions:
234234 * Counter for current stepping sessions
235235 * @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
237240 * @bm: Platform specific battery management information
238241 * @flags: Structure for information about events triggered
239242 * @usb_state: Structure for usb stack information
....@@ -283,7 +286,10 @@
283286 int is_aca_rid;
284287 atomic_t current_stepping_sessions;
285288 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;
287293 struct abx500_bm_data *bm;
288294 struct ab8500_charger_event_flags flags;
289295 struct ab8500_charger_usb_state usb_state;
....@@ -398,7 +404,7 @@
398404 }
399405
400406 /**
401
- * ab8500_power_supply_changed - a wrapper with local extentions for
407
+ * ab8500_power_supply_changed - a wrapper with local extensions for
402408 * power_supply_changed
403409 * @di: pointer to the ab8500_charger structure
404410 * @psy: pointer to power_supply_that have changed.
....@@ -475,13 +481,13 @@
475481 */
476482 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
477483 {
478
- int vch;
484
+ int vch, ret;
479485
480486 /* Only measure voltage if the charger is connected */
481487 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__);
485491 } else {
486492 vch = 0;
487493 }
....@@ -526,13 +532,13 @@
526532 */
527533 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
528534 {
529
- int vch;
535
+ int vch, ret;
530536
531537 /* Only measure voltage if the charger is connected */
532538 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__);
536542 } else {
537543 vch = 0;
538544 }
....@@ -548,13 +554,13 @@
548554 */
549555 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
550556 {
551
- int ich;
557
+ int ich, ret;
552558
553559 /* Only measure current if the charger is online */
554560 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__);
558564 } else {
559565 ich = 0;
560566 }
....@@ -570,13 +576,13 @@
570576 */
571577 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
572578 {
573
- int ich;
579
+ int ich, ret;
574580
575581 /* Only measure current if the charger is online */
576582 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__);
580586 } else {
581587 ich = 0;
582588 }
....@@ -693,7 +699,7 @@
693699 /*
694700 * Platform only supports USB 2.0.
695701 * 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_*
697703 * should set USB_CH_IP_CUR_LVL_0P5.
698704 */
699705
....@@ -758,6 +764,7 @@
758764 USB_CH_IP_CUR_LVL_1P5;
759765 break;
760766 }
767
+ fallthrough;
761768 case USB_STAT_HM_IDGND:
762769 dev_err(di->dev, "USB Type - Charging not allowed\n");
763770 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
....@@ -798,7 +805,7 @@
798805 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
799806 ret = -ENXIO;
800807 break;
801
- };
808
+ }
802809
803810 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
804811 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
....@@ -1088,7 +1095,7 @@
10881095 di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
10891096 ret = -EPERM;
10901097 break;
1091
- };
1098
+ }
10921099 di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
10931100 return ret;
10941101 }
....@@ -1388,13 +1395,13 @@
13881395
13891396 /*
13901397 * 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.
13921399 * This will turn off charging for a short while.
13931400 * It can be avoided by having the supply on when
13941401 * 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
13981405 */
13991406 if (!di->vddadc_en_ac) {
14001407 ret = regulator_enable(di->regu);
....@@ -1464,7 +1471,7 @@
14641471 if (is_ab8500_1p1_or_earlier(di->parent)) {
14651472 /*
14661473 * 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
14681475 * kick the charger watchdog even when no charger is
14691476 * connected. This is only valid once the AC charger
14701477 * has been enabled. This is a bug that is not handled
....@@ -1561,13 +1568,13 @@
15611568
15621569 /*
15631570 * 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.
15651572 * This will turn off charging for a short while.
15661573 * It can be avoided by having the supply on when
15671574 * 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
15711578 */
15721579 if (!di->vddadc_en_usb) {
15731580 ret = regulator_enable(di->regu);
....@@ -1591,7 +1598,10 @@
15911598 return -ENXIO;
15921599 }
15931600
1594
- /* ChVoltLevel: max voltage upto which battery can be charged */
1601
+ /*
1602
+ * ChVoltLevel: max voltage up to which battery can be
1603
+ * charged
1604
+ */
15951605 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
15961606 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
15971607 if (ret) {
....@@ -2023,7 +2033,7 @@
20232033 * Work queue function for kicking the charger watchdog.
20242034 *
20252035 * 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
20272037 * watchdog even when no charger is connected. This is only
20282038 * valid once the AC charger has been enabled. This is
20292039 * a bug that is not handled by the algorithm and the
....@@ -2271,7 +2281,7 @@
22712281 * Some chargers that breaks the USB spec is
22722282 * identified as invalid by AB8500 and it refuse
22732283 * 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.
22752285 */
22762286 if (is_ab8500(di->parent))
22772287 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
....@@ -2416,7 +2426,7 @@
24162426 * of 1sec for enabling charging
24172427 */
24182428 msleep(1000);
2419
- /* Intentional fall through */
2429
+ fallthrough;
24202430 case AB8500_BM_USB_STATE_CONFIGURED:
24212431 /*
24222432 * USB is configured, enable charging with the charging
....@@ -2436,7 +2446,7 @@
24362446
24372447 default:
24382448 break;
2439
- };
2449
+ }
24402450 }
24412451
24422452 /**
....@@ -3026,7 +3036,6 @@
30263036 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
30273037 {
30283038 int ret = 0;
3029
- u8 bup_vch_range = 0, vbup33_vrtcn = 0;
30303039
30313040 /* Setup maximum charger current and voltage for ABB cut2.0 */
30323041 if (!is_ab8500_1p1_or_earlier(di->parent)) {
....@@ -3127,12 +3136,6 @@
31273136 goto out;
31283137 }
31293138
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
-
31363139 ret = abx500_set_register_interruptible(di->dev,
31373140 AB8500_RTC,
31383141 AB8500_RTC_BACKUP_CHG_REG,
....@@ -3230,7 +3233,7 @@
32303233
32313234 /*
32323235 * 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
32343237 * watchdog even when no charger is connected. This is only
32353238 * valid once the AC charger has been enabled. This is
32363239 * a bug that is not handled by the algorithm and the
....@@ -3393,7 +3396,39 @@
33933396 /* get parent data */
33943397 di->dev = &pdev->dev;
33953398 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
+ }
33973432
33983433 /* initialize lock */
33993434 spin_lock_init(&di->usb_state.usb_lock);
....@@ -3467,7 +3502,7 @@
34673502
34683503 /*
34693504 * 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
34713506 * watchdog even when no charger is connected. This is only
34723507 * valid once the AC charger has been enabled. This is
34733508 * a bug that is not handled by the algorithm and the
....@@ -3578,6 +3613,11 @@
35783613 /* Register interrupts */
35793614 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
35803615 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3616
+ if (irq < 0) {
3617
+ ret = irq;
3618
+ goto free_irq;
3619
+ }
3620
+
35813621 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
35823622 IRQF_SHARED | IRQF_NO_SUSPEND,
35833623 ab8500_charger_irq[i].name, di);