hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/power/supply/smb347-charger.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Summit Microelectronics SMB347 Battery Charger Driver
34 *
....@@ -5,12 +6,9 @@
56 *
67 * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
78 * Mika Westerberg <mika.westerberg@linux.intel.com>
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License version 2 as
11
- * published by the Free Software Foundation.
129 */
1310
11
+#include <linux/delay.h>
1412 #include <linux/err.h>
1513 #include <linux/gpio.h>
1614 #include <linux/kernel.h>
....@@ -18,10 +16,17 @@
1816 #include <linux/init.h>
1917 #include <linux/interrupt.h>
2018 #include <linux/i2c.h>
21
-#include <linux/mutex.h>
2219 #include <linux/power_supply.h>
23
-#include <linux/power/smb347-charger.h>
20
+#include <linux/property.h>
2421 #include <linux/regmap.h>
22
+
23
+#include <dt-bindings/power/summit,smb347-charger.h>
24
+
25
+/* Use the default compensation method */
26
+#define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
27
+
28
+/* Use default factory programmed value for hard/soft temperature limit */
29
+#define SMB3XX_TEMP_USE_DEFAULT -273
2530
2631 /*
2732 * Configuration registers. These are mirrored to volatile RAM and can be
....@@ -51,6 +56,7 @@
5156 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
5257 #define CFG_PIN_EN_APSD_IRQ BIT(1)
5358 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
59
+#define CFG_PIN_EN_CTRL BIT(4)
5460 #define CFG_THERM 0x07
5561 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
5662 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
....@@ -124,82 +130,142 @@
124130
125131 /**
126132 * struct smb347_charger - smb347 charger instance
127
- * @lock: protects concurrent access to online variables
128133 * @dev: pointer to device
129134 * @regmap: pointer to driver regmap
130135 * @mains: power_supply instance for AC/DC power
131136 * @usb: power_supply instance for USB power
132
- * @battery: power_supply instance for battery
137
+ * @id: SMB charger ID
133138 * @mains_online: is AC/DC input connected
134139 * @usb_online: is USB input connected
135140 * @charging_enabled: is charging enabled
136
- * @pdata: pointer to platform data
141
+ * @irq_unsupported: is interrupt unsupported by SMB hardware
142
+ * @max_charge_current: maximum current (in uA) the battery can be charged
143
+ * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
144
+ * @pre_charge_current: current (in uA) to use in pre-charging phase
145
+ * @termination_current: current (in uA) used to determine when the
146
+ * charging cycle terminates
147
+ * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to
148
+ * pre-charge to fast charge mode
149
+ * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
150
+ * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
151
+ * input
152
+ * @chip_temp_threshold: die temperature where device starts limiting charge
153
+ * current [%100 - %130] (in degree C)
154
+ * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
155
+ * granularity is 5 deg C.
156
+ * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C),
157
+ * granularity is 5 deg C.
158
+ * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
159
+ * granularity is 5 deg C.
160
+ * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
161
+ * granularity is 5 deg C.
162
+ * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
163
+ * @soft_temp_limit_compensation: compensation method when soft temperature
164
+ * limit is hit
165
+ * @charge_current_compensation: current (in uA) for charging compensation
166
+ * current when temperature hits soft limits
167
+ * @use_mains: AC/DC input can be used
168
+ * @use_usb: USB input can be used
169
+ * @use_usb_otg: USB OTG output can be used (not implemented yet)
170
+ * @enable_control: how charging enable/disable is controlled
171
+ * (driver/pin controls)
172
+ *
173
+ * @use_main, @use_usb, and @use_usb_otg are means to enable/disable
174
+ * hardware support for these. This is useful when we want to have for
175
+ * example OTG charging controlled via OTG transceiver driver and not by
176
+ * the SMB347 hardware.
177
+ *
178
+ * Hard and soft temperature limit values are given as described in the
179
+ * device data sheet and assuming NTC beta value is %3750. Even if this is
180
+ * not the case, these values should be used. They can be mapped to the
181
+ * corresponding NTC beta values with the help of table %2 in the data
182
+ * sheet. So for example if NTC beta is %3375 and we want to program hard
183
+ * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
184
+ *
185
+ * If zero value is given in any of the current and voltage values, the
186
+ * factory programmed default will be used. For soft/hard temperature
187
+ * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead.
137188 */
138189 struct smb347_charger {
139
- struct mutex lock;
140190 struct device *dev;
141191 struct regmap *regmap;
142192 struct power_supply *mains;
143193 struct power_supply *usb;
144
- struct power_supply *battery;
194
+ unsigned int id;
145195 bool mains_online;
146196 bool usb_online;
147197 bool charging_enabled;
148
- const struct smb347_charger_platform_data *pdata;
198
+ bool irq_unsupported;
199
+
200
+ unsigned int max_charge_current;
201
+ unsigned int max_charge_voltage;
202
+ unsigned int pre_charge_current;
203
+ unsigned int termination_current;
204
+ unsigned int pre_to_fast_voltage;
205
+ unsigned int mains_current_limit;
206
+ unsigned int usb_hc_current_limit;
207
+ unsigned int chip_temp_threshold;
208
+ int soft_cold_temp_limit;
209
+ int soft_hot_temp_limit;
210
+ int hard_cold_temp_limit;
211
+ int hard_hot_temp_limit;
212
+ bool suspend_on_hard_temp_limit;
213
+ unsigned int soft_temp_limit_compensation;
214
+ unsigned int charge_current_compensation;
215
+ bool use_mains;
216
+ bool use_usb;
217
+ bool use_usb_otg;
218
+ unsigned int enable_control;
219
+};
220
+
221
+enum smb_charger_chipid {
222
+ SMB345,
223
+ SMB347,
224
+ SMB358,
225
+ NUM_CHIP_TYPES,
149226 };
150227
151228 /* Fast charge current in uA */
152
-static const unsigned int fcc_tbl[] = {
153
- 700000,
154
- 900000,
155
- 1200000,
156
- 1500000,
157
- 1800000,
158
- 2000000,
159
- 2200000,
160
- 2500000,
229
+static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = {
230
+ [SMB345] = { 200000, 450000, 600000, 900000,
231
+ 1300000, 1500000, 1800000, 2000000 },
232
+ [SMB347] = { 700000, 900000, 1200000, 1500000,
233
+ 1800000, 2000000, 2200000, 2500000 },
234
+ [SMB358] = { 200000, 450000, 600000, 900000,
235
+ 1300000, 1500000, 1800000, 2000000 },
161236 };
162
-
163237 /* Pre-charge current in uA */
164
-static const unsigned int pcc_tbl[] = {
165
- 100000,
166
- 150000,
167
- 200000,
168
- 250000,
238
+static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = {
239
+ [SMB345] = { 150000, 250000, 350000, 450000 },
240
+ [SMB347] = { 100000, 150000, 200000, 250000 },
241
+ [SMB358] = { 150000, 250000, 350000, 450000 },
169242 };
170243
171244 /* Termination current in uA */
172
-static const unsigned int tc_tbl[] = {
173
- 37500,
174
- 50000,
175
- 100000,
176
- 150000,
177
- 200000,
178
- 250000,
179
- 500000,
180
- 600000,
245
+static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = {
246
+ [SMB345] = { 30000, 40000, 60000, 80000,
247
+ 100000, 125000, 150000, 200000 },
248
+ [SMB347] = { 37500, 50000, 100000, 150000,
249
+ 200000, 250000, 500000, 600000 },
250
+ [SMB358] = { 30000, 40000, 60000, 80000,
251
+ 100000, 125000, 150000, 200000 },
181252 };
182253
183254 /* Input current limit in uA */
184
-static const unsigned int icl_tbl[] = {
185
- 300000,
186
- 500000,
187
- 700000,
188
- 900000,
189
- 1200000,
190
- 1500000,
191
- 1800000,
192
- 2000000,
193
- 2200000,
194
- 2500000,
255
+static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = {
256
+ [SMB345] = { 300000, 500000, 700000, 1000000, 1500000,
257
+ 1800000, 2000000, 2000000, 2000000, 2000000 },
258
+ [SMB347] = { 300000, 500000, 700000, 900000, 1200000,
259
+ 1500000, 1800000, 2000000, 2200000, 2500000 },
260
+ [SMB358] = { 300000, 500000, 700000, 1000000, 1500000,
261
+ 1800000, 2000000, 2000000, 2000000, 2000000 },
195262 };
196263
197264 /* Charge current compensation in uA */
198
-static const unsigned int ccc_tbl[] = {
199
- 250000,
200
- 700000,
201
- 900000,
202
- 1200000,
265
+static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
266
+ [SMB345] = { 200000, 450000, 600000, 900000 },
267
+ [SMB347] = { 250000, 700000, 900000, 1200000 },
268
+ [SMB358] = { 200000, 450000, 600000, 900000 },
203269 };
204270
205271 /* Convert register value to current using lookup table */
....@@ -244,16 +310,14 @@
244310 * Dc and usb are set depending on whether they are enabled in
245311 * platform data _and_ whether corresponding undervoltage is set.
246312 */
247
- if (smb->pdata->use_mains)
313
+ if (smb->use_mains)
248314 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
249
- if (smb->pdata->use_usb)
315
+ if (smb->use_usb)
250316 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
251317
252
- mutex_lock(&smb->lock);
253318 ret = smb->mains_online != dc || smb->usb_online != usb;
254319 smb->mains_online = dc;
255320 smb->usb_online = usb;
256
- mutex_unlock(&smb->lock);
257321
258322 return ret;
259323 }
....@@ -269,13 +333,7 @@
269333 */
270334 static bool smb347_is_ps_online(struct smb347_charger *smb)
271335 {
272
- bool ret;
273
-
274
- mutex_lock(&smb->lock);
275
- ret = smb->usb_online || smb->mains_online;
276
- mutex_unlock(&smb->lock);
277
-
278
- return ret;
336
+ return smb->usb_online || smb->mains_online;
279337 }
280338
281339 /**
....@@ -304,19 +362,18 @@
304362 {
305363 int ret = 0;
306364
307
- if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
365
+ if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) {
308366 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
309367 return 0;
310368 }
311369
312
- mutex_lock(&smb->lock);
313370 if (smb->charging_enabled != enable) {
314371 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
315372 enable ? CMD_A_CHG_ENABLED : 0);
316373 if (!ret)
317374 smb->charging_enabled = enable;
318375 }
319
- mutex_unlock(&smb->lock);
376
+
320377 return ret;
321378 }
322379
....@@ -354,11 +411,12 @@
354411
355412 static int smb347_set_charge_current(struct smb347_charger *smb)
356413 {
414
+ unsigned int id = smb->id;
357415 int ret;
358416
359
- if (smb->pdata->max_charge_current) {
360
- ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
361
- smb->pdata->max_charge_current);
417
+ if (smb->max_charge_current) {
418
+ ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]),
419
+ smb->max_charge_current);
362420 if (ret < 0)
363421 return ret;
364422
....@@ -369,9 +427,9 @@
369427 return ret;
370428 }
371429
372
- if (smb->pdata->pre_charge_current) {
373
- ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
374
- smb->pdata->pre_charge_current);
430
+ if (smb->pre_charge_current) {
431
+ ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]),
432
+ smb->pre_charge_current);
375433 if (ret < 0)
376434 return ret;
377435
....@@ -382,9 +440,9 @@
382440 return ret;
383441 }
384442
385
- if (smb->pdata->termination_current) {
386
- ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
387
- smb->pdata->termination_current);
443
+ if (smb->termination_current) {
444
+ ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]),
445
+ smb->termination_current);
388446 if (ret < 0)
389447 return ret;
390448
....@@ -399,11 +457,12 @@
399457
400458 static int smb347_set_current_limits(struct smb347_charger *smb)
401459 {
460
+ unsigned int id = smb->id;
402461 int ret;
403462
404
- if (smb->pdata->mains_current_limit) {
405
- ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
406
- smb->pdata->mains_current_limit);
463
+ if (smb->mains_current_limit) {
464
+ ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
465
+ smb->mains_current_limit);
407466 if (ret < 0)
408467 return ret;
409468
....@@ -414,9 +473,9 @@
414473 return ret;
415474 }
416475
417
- if (smb->pdata->usb_hc_current_limit) {
418
- ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
419
- smb->pdata->usb_hc_current_limit);
476
+ if (smb->usb_hc_current_limit) {
477
+ ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
478
+ smb->usb_hc_current_limit);
420479 if (ret < 0)
421480 return ret;
422481
....@@ -433,8 +492,8 @@
433492 {
434493 int ret;
435494
436
- if (smb->pdata->pre_to_fast_voltage) {
437
- ret = smb->pdata->pre_to_fast_voltage;
495
+ if (smb->pre_to_fast_voltage) {
496
+ ret = smb->pre_to_fast_voltage;
438497
439498 /* uV */
440499 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
....@@ -447,8 +506,8 @@
447506 return ret;
448507 }
449508
450
- if (smb->pdata->max_charge_voltage) {
451
- ret = smb->pdata->max_charge_voltage;
509
+ if (smb->max_charge_voltage) {
510
+ ret = smb->max_charge_voltage;
452511
453512 /* uV */
454513 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
....@@ -465,12 +524,13 @@
465524
466525 static int smb347_set_temp_limits(struct smb347_charger *smb)
467526 {
527
+ unsigned int id = smb->id;
468528 bool enable_therm_monitor = false;
469529 int ret = 0;
470530 int val;
471531
472
- if (smb->pdata->chip_temp_threshold) {
473
- val = smb->pdata->chip_temp_threshold;
532
+ if (smb->chip_temp_threshold) {
533
+ val = smb->chip_temp_threshold;
474534
475535 /* degree C */
476536 val = clamp_val(val, 100, 130) - 100;
....@@ -483,8 +543,8 @@
483543 return ret;
484544 }
485545
486
- if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
487
- val = smb->pdata->soft_cold_temp_limit;
546
+ if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
547
+ val = smb->soft_cold_temp_limit;
488548
489549 val = clamp_val(val, 0, 15);
490550 val /= 5;
....@@ -500,8 +560,8 @@
500560 enable_therm_monitor = true;
501561 }
502562
503
- if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
504
- val = smb->pdata->soft_hot_temp_limit;
563
+ if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
564
+ val = smb->soft_hot_temp_limit;
505565
506566 val = clamp_val(val, 40, 55) - 40;
507567 val /= 5;
....@@ -515,8 +575,8 @@
515575 enable_therm_monitor = true;
516576 }
517577
518
- if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
519
- val = smb->pdata->hard_cold_temp_limit;
578
+ if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
579
+ val = smb->hard_cold_temp_limit;
520580
521581 val = clamp_val(val, -5, 10) + 5;
522582 val /= 5;
....@@ -532,8 +592,8 @@
532592 enable_therm_monitor = true;
533593 }
534594
535
- if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
536
- val = smb->pdata->hard_hot_temp_limit;
595
+ if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
596
+ val = smb->hard_hot_temp_limit;
537597
538598 val = clamp_val(val, 50, 65) - 50;
539599 val /= 5;
....@@ -564,16 +624,16 @@
564624 return ret;
565625 }
566626
567
- if (smb->pdata->suspend_on_hard_temp_limit) {
627
+ if (smb->suspend_on_hard_temp_limit) {
568628 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
569629 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
570630 if (ret < 0)
571631 return ret;
572632 }
573633
574
- if (smb->pdata->soft_temp_limit_compensation !=
575
- SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
576
- val = smb->pdata->soft_temp_limit_compensation & 0x3;
634
+ if (smb->soft_temp_limit_compensation !=
635
+ SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) {
636
+ val = smb->soft_temp_limit_compensation & 0x3;
577637
578638 ret = regmap_update_bits(smb->regmap, CFG_THERM,
579639 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
....@@ -588,9 +648,9 @@
588648 return ret;
589649 }
590650
591
- if (smb->pdata->charge_current_compensation) {
592
- val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
593
- smb->pdata->charge_current_compensation);
651
+ if (smb->charge_current_compensation) {
652
+ val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]),
653
+ smb->charge_current_compensation);
594654 if (val < 0)
595655 return val;
596656
....@@ -649,7 +709,7 @@
649709 goto fail;
650710
651711 /* If USB charging is disabled we put the USB in suspend mode */
652
- if (!smb->pdata->use_usb) {
712
+ if (!smb->use_usb) {
653713 ret = regmap_update_bits(smb->regmap, CMD_A,
654714 CMD_A_SUSPEND_ENABLED,
655715 CMD_A_SUSPEND_ENABLED);
....@@ -662,20 +722,29 @@
662722 * support for driving VBUS. Otherwise we disable it.
663723 */
664724 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
665
- smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
725
+ smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
666726 if (ret < 0)
667727 goto fail;
728
+
729
+ /* Activate pin control, making it writable. */
730
+ switch (smb->enable_control) {
731
+ case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
732
+ case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
733
+ ret = regmap_set_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL);
734
+ if (ret < 0)
735
+ goto fail;
736
+ }
668737
669738 /*
670739 * Make the charging functionality controllable by a write to the
671740 * command register unless pin control is specified in the platform
672741 * data.
673742 */
674
- switch (smb->pdata->enable_control) {
675
- case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
743
+ switch (smb->enable_control) {
744
+ case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
676745 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
677746 break;
678
- case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
747
+ case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
679748 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
680749 break;
681750 default:
....@@ -711,6 +780,9 @@
711780 bool handled = false;
712781 int ret;
713782
783
+ /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
784
+ usleep_range(25000, 35000);
785
+
714786 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
715787 if (ret < 0) {
716788 dev_warn(smb->dev, "reading STAT_C failed\n");
....@@ -741,7 +813,10 @@
741813 */
742814 if (stat_c & STAT_C_CHARGER_ERROR) {
743815 dev_err(smb->dev, "charging stopped due to charger error\n");
744
- power_supply_changed(smb->battery);
816
+ if (smb->use_mains)
817
+ power_supply_changed(smb->mains);
818
+ if (smb->use_usb)
819
+ power_supply_changed(smb->usb);
745820 handled = true;
746821 }
747822
....@@ -751,8 +826,12 @@
751826 * disabled by the hardware.
752827 */
753828 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
754
- if (irqstat_c & IRQSTAT_C_TERMINATION_STAT)
755
- power_supply_changed(smb->battery);
829
+ if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) {
830
+ if (smb->use_mains)
831
+ power_supply_changed(smb->mains);
832
+ if (smb->use_usb)
833
+ power_supply_changed(smb->usb);
834
+ }
756835 dev_dbg(smb->dev, "going to HW maintenance mode\n");
757836 handled = true;
758837 }
....@@ -766,7 +845,10 @@
766845
767846 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
768847 dev_warn(smb->dev, "charging stopped due to timeout\n");
769
- power_supply_changed(smb->battery);
848
+ if (smb->use_mains)
849
+ power_supply_changed(smb->mains);
850
+ if (smb->use_usb)
851
+ power_supply_changed(smb->usb);
770852 handled = true;
771853 }
772854
....@@ -777,9 +859,9 @@
777859 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
778860 if (smb347_update_ps_status(smb) > 0) {
779861 smb347_start_stop_charging(smb);
780
- if (smb->pdata->use_mains)
862
+ if (smb->use_mains)
781863 power_supply_changed(smb->mains);
782
- if (smb->pdata->use_usb)
864
+ if (smb->use_usb)
783865 power_supply_changed(smb->usb);
784866 }
785867 handled = true;
....@@ -791,6 +873,9 @@
791873 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
792874 {
793875 int ret;
876
+
877
+ if (smb->irq_unsupported)
878
+ return 0;
794879
795880 ret = smb347_set_writable(smb, true);
796881 if (ret < 0)
....@@ -834,22 +919,17 @@
834919 static int smb347_irq_init(struct smb347_charger *smb,
835920 struct i2c_client *client)
836921 {
837
- const struct smb347_charger_platform_data *pdata = smb->pdata;
838
- int ret, irq = gpio_to_irq(pdata->irq_gpio);
922
+ int ret;
839923
840
- ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
924
+ ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
925
+ smb347_interrupt, IRQF_ONESHOT,
926
+ client->name, smb);
841927 if (ret < 0)
842
- goto fail;
843
-
844
- ret = request_threaded_irq(irq, NULL, smb347_interrupt,
845
- IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
846
- client->name, smb);
847
- if (ret < 0)
848
- goto fail_gpio;
928
+ return ret;
849929
850930 ret = smb347_set_writable(smb, true);
851931 if (ret < 0)
852
- goto fail_irq;
932
+ return ret;
853933
854934 /*
855935 * Configure the STAT output to be suitable for interrupts: disable
....@@ -858,21 +938,9 @@
858938 ret = regmap_update_bits(smb->regmap, CFG_STAT,
859939 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
860940 CFG_STAT_DISABLED);
861
- if (ret < 0)
862
- goto fail_readonly;
863941
864942 smb347_set_writable(smb, false);
865
- client->irq = irq;
866
- return 0;
867943
868
-fail_readonly:
869
- smb347_set_writable(smb, false);
870
-fail_irq:
871
- free_irq(irq, smb);
872
-fail_gpio:
873
- gpio_free(pdata->irq_gpio);
874
-fail:
875
- client->irq = 0;
876944 return ret;
877945 }
878946
....@@ -882,6 +950,7 @@
882950 */
883951 static int get_const_charge_current(struct smb347_charger *smb)
884952 {
953
+ unsigned int id = smb->id;
885954 int ret, intval;
886955 unsigned int v;
887956
....@@ -897,10 +966,12 @@
897966 * and we can detect which table to use from bit 5.
898967 */
899968 if (v & 0x20) {
900
- intval = hw_to_current(fcc_tbl, ARRAY_SIZE(fcc_tbl), v & 7);
969
+ intval = hw_to_current(fcc_tbl[id],
970
+ ARRAY_SIZE(fcc_tbl[id]), v & 7);
901971 } else {
902972 v >>= 3;
903
- intval = hw_to_current(pcc_tbl, ARRAY_SIZE(pcc_tbl), v & 7);
973
+ intval = hw_to_current(pcc_tbl[id],
974
+ ARRAY_SIZE(pcc_tbl[id]), v & 7);
904975 }
905976
906977 return intval;
....@@ -931,95 +1002,19 @@
9311002 return intval;
9321003 }
9331004
934
-static int smb347_mains_get_property(struct power_supply *psy,
935
- enum power_supply_property prop,
936
- union power_supply_propval *val)
937
-{
938
- struct smb347_charger *smb = power_supply_get_drvdata(psy);
939
- int ret;
940
-
941
- switch (prop) {
942
- case POWER_SUPPLY_PROP_ONLINE:
943
- val->intval = smb->mains_online;
944
- break;
945
-
946
- case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
947
- ret = get_const_charge_voltage(smb);
948
- if (ret < 0)
949
- return ret;
950
- else
951
- val->intval = ret;
952
- break;
953
-
954
- case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
955
- ret = get_const_charge_current(smb);
956
- if (ret < 0)
957
- return ret;
958
- else
959
- val->intval = ret;
960
- break;
961
-
962
- default:
963
- return -EINVAL;
964
- }
965
-
966
- return 0;
967
-}
968
-
969
-static enum power_supply_property smb347_mains_properties[] = {
970
- POWER_SUPPLY_PROP_ONLINE,
971
- POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
972
- POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
973
-};
974
-
975
-static int smb347_usb_get_property(struct power_supply *psy,
976
- enum power_supply_property prop,
977
- union power_supply_propval *val)
978
-{
979
- struct smb347_charger *smb = power_supply_get_drvdata(psy);
980
- int ret;
981
-
982
- switch (prop) {
983
- case POWER_SUPPLY_PROP_ONLINE:
984
- val->intval = smb->usb_online;
985
- break;
986
-
987
- case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
988
- ret = get_const_charge_voltage(smb);
989
- if (ret < 0)
990
- return ret;
991
- else
992
- val->intval = ret;
993
- break;
994
-
995
- case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
996
- ret = get_const_charge_current(smb);
997
- if (ret < 0)
998
- return ret;
999
- else
1000
- val->intval = ret;
1001
- break;
1002
-
1003
- default:
1004
- return -EINVAL;
1005
- }
1006
-
1007
- return 0;
1008
-}
1009
-
1010
-static enum power_supply_property smb347_usb_properties[] = {
1011
- POWER_SUPPLY_PROP_ONLINE,
1012
- POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1013
- POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1014
-};
1015
-
1016
-static int smb347_get_charging_status(struct smb347_charger *smb)
1005
+static int smb347_get_charging_status(struct smb347_charger *smb,
1006
+ struct power_supply *psy)
10171007 {
10181008 int ret, status;
10191009 unsigned int val;
10201010
1021
- if (!smb347_is_ps_online(smb))
1022
- return POWER_SUPPLY_STATUS_DISCHARGING;
1011
+ if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1012
+ if (!smb->usb_online)
1013
+ return POWER_SUPPLY_STATUS_DISCHARGING;
1014
+ } else {
1015
+ if (!smb->mains_online)
1016
+ return POWER_SUPPLY_STATUS_DISCHARGING;
1017
+ }
10231018
10241019 ret = regmap_read(smb->regmap, STAT_C, &val);
10251020 if (ret < 0)
....@@ -1058,29 +1053,29 @@
10581053 return status;
10591054 }
10601055
1061
-static int smb347_battery_get_property(struct power_supply *psy,
1062
- enum power_supply_property prop,
1063
- union power_supply_propval *val)
1056
+static int smb347_get_property_locked(struct power_supply *psy,
1057
+ enum power_supply_property prop,
1058
+ union power_supply_propval *val)
10641059 {
10651060 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1066
- const struct smb347_charger_platform_data *pdata = smb->pdata;
10671061 int ret;
1068
-
1069
- ret = smb347_update_ps_status(smb);
1070
- if (ret < 0)
1071
- return ret;
10721062
10731063 switch (prop) {
10741064 case POWER_SUPPLY_PROP_STATUS:
1075
- ret = smb347_get_charging_status(smb);
1065
+ ret = smb347_get_charging_status(smb, psy);
10761066 if (ret < 0)
10771067 return ret;
10781068 val->intval = ret;
10791069 break;
10801070
10811071 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1082
- if (!smb347_is_ps_online(smb))
1083
- return -ENODATA;
1072
+ if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1073
+ if (!smb->usb_online)
1074
+ return -ENODATA;
1075
+ } else {
1076
+ if (!smb->mains_online)
1077
+ return -ENODATA;
1078
+ }
10841079
10851080 /*
10861081 * We handle trickle and pre-charging the same, and taper
....@@ -1099,24 +1094,25 @@
10991094 }
11001095 break;
11011096
1102
- case POWER_SUPPLY_PROP_TECHNOLOGY:
1103
- val->intval = pdata->battery_info.technology;
1097
+ case POWER_SUPPLY_PROP_ONLINE:
1098
+ if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1099
+ val->intval = smb->usb_online;
1100
+ else
1101
+ val->intval = smb->mains_online;
11041102 break;
11051103
1106
- case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1107
- val->intval = pdata->battery_info.voltage_min_design;
1104
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1105
+ ret = get_const_charge_voltage(smb);
1106
+ if (ret < 0)
1107
+ return ret;
1108
+ val->intval = ret;
11081109 break;
11091110
1110
- case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1111
- val->intval = pdata->battery_info.voltage_max_design;
1112
- break;
1113
-
1114
- case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1115
- val->intval = pdata->battery_info.charge_full_design;
1116
- break;
1117
-
1118
- case POWER_SUPPLY_PROP_MODEL_NAME:
1119
- val->strval = pdata->battery_info.name;
1111
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1112
+ ret = get_const_charge_current(smb);
1113
+ if (ret < 0)
1114
+ return ret;
1115
+ val->intval = ret;
11201116 break;
11211117
11221118 default:
....@@ -1126,14 +1122,27 @@
11261122 return 0;
11271123 }
11281124
1129
-static enum power_supply_property smb347_battery_properties[] = {
1125
+static int smb347_get_property(struct power_supply *psy,
1126
+ enum power_supply_property prop,
1127
+ union power_supply_propval *val)
1128
+{
1129
+ struct smb347_charger *smb = power_supply_get_drvdata(psy);
1130
+ struct i2c_client *client = to_i2c_client(smb->dev);
1131
+ int ret;
1132
+
1133
+ disable_irq(client->irq);
1134
+ ret = smb347_get_property_locked(psy, prop, val);
1135
+ enable_irq(client->irq);
1136
+
1137
+ return ret;
1138
+}
1139
+
1140
+static enum power_supply_property smb347_properties[] = {
11301141 POWER_SUPPLY_PROP_STATUS,
11311142 POWER_SUPPLY_PROP_CHARGE_TYPE,
1132
- POWER_SUPPLY_PROP_TECHNOLOGY,
1133
- POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1134
- POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1135
- POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
1136
- POWER_SUPPLY_PROP_MODEL_NAME,
1143
+ POWER_SUPPLY_PROP_ONLINE,
1144
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1145
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
11371146 };
11381147
11391148 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
....@@ -1179,6 +1188,96 @@
11791188 return smb347_volatile_reg(dev, reg);
11801189 }
11811190
1191
+static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1192
+{
1193
+ struct device *dev = smb->dev;
1194
+
1195
+ smb->soft_temp_limit_compensation =
1196
+ SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1197
+ /*
1198
+ * These properties come from the battery info, still we need to
1199
+ * pre-initialize the values. See smb347_get_battery_info() below.
1200
+ */
1201
+ smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1202
+ smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1203
+ smb->soft_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1204
+ smb->hard_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1205
+
1206
+ /* Charging constraints */
1207
+ device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1208
+ &smb->pre_to_fast_voltage);
1209
+ device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1210
+ &smb->mains_current_limit);
1211
+ device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1212
+ &smb->usb_hc_current_limit);
1213
+
1214
+ /* For thermometer monitoring */
1215
+ device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1216
+ &smb->chip_temp_threshold);
1217
+ device_property_read_u32(dev, "summit,soft-compensation-method",
1218
+ &smb->soft_temp_limit_compensation);
1219
+ device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1220
+ &smb->charge_current_compensation);
1221
+
1222
+ /* Supported charging mode */
1223
+ smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1224
+ smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1225
+ smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1226
+
1227
+ /* Select charging control */
1228
+ device_property_read_u32(dev, "summit,enable-charge-control",
1229
+ &smb->enable_control);
1230
+}
1231
+
1232
+static int smb347_get_battery_info(struct smb347_charger *smb)
1233
+{
1234
+ struct power_supply_battery_info info = {};
1235
+ struct power_supply *supply;
1236
+ int err;
1237
+
1238
+ if (smb->mains)
1239
+ supply = smb->mains;
1240
+ else
1241
+ supply = smb->usb;
1242
+
1243
+ err = power_supply_get_battery_info(supply, &info);
1244
+ if (err == -ENXIO || err == -ENODEV)
1245
+ return 0;
1246
+ if (err)
1247
+ return err;
1248
+
1249
+ if (info.constant_charge_current_max_ua != -EINVAL)
1250
+ smb->max_charge_current = info.constant_charge_current_max_ua;
1251
+
1252
+ if (info.constant_charge_voltage_max_uv != -EINVAL)
1253
+ smb->max_charge_voltage = info.constant_charge_voltage_max_uv;
1254
+
1255
+ if (info.precharge_current_ua != -EINVAL)
1256
+ smb->pre_charge_current = info.precharge_current_ua;
1257
+
1258
+ if (info.charge_term_current_ua != -EINVAL)
1259
+ smb->termination_current = info.charge_term_current_ua;
1260
+
1261
+ if (info.temp_alert_min != INT_MIN)
1262
+ smb->soft_cold_temp_limit = info.temp_alert_min;
1263
+
1264
+ if (info.temp_alert_max != INT_MAX)
1265
+ smb->soft_hot_temp_limit = info.temp_alert_max;
1266
+
1267
+ if (info.temp_min != INT_MIN)
1268
+ smb->hard_cold_temp_limit = info.temp_min;
1269
+
1270
+ if (info.temp_max != INT_MAX)
1271
+ smb->hard_hot_temp_limit = info.temp_max;
1272
+
1273
+ /* Suspend when battery temperature is outside hard limits */
1274
+ if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1275
+ smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1276
+ smb->suspend_on_hard_temp_limit = true;
1277
+
1278
+ return 0;
1279
+}
1280
+
11821281 static const struct regmap_config smb347_regmap = {
11831282 .reg_bits = 8,
11841283 .val_bits = 8,
....@@ -1190,102 +1289,76 @@
11901289 static const struct power_supply_desc smb347_mains_desc = {
11911290 .name = "smb347-mains",
11921291 .type = POWER_SUPPLY_TYPE_MAINS,
1193
- .get_property = smb347_mains_get_property,
1194
- .properties = smb347_mains_properties,
1195
- .num_properties = ARRAY_SIZE(smb347_mains_properties),
1292
+ .get_property = smb347_get_property,
1293
+ .properties = smb347_properties,
1294
+ .num_properties = ARRAY_SIZE(smb347_properties),
11961295 };
11971296
11981297 static const struct power_supply_desc smb347_usb_desc = {
11991298 .name = "smb347-usb",
12001299 .type = POWER_SUPPLY_TYPE_USB,
1201
- .get_property = smb347_usb_get_property,
1202
- .properties = smb347_usb_properties,
1203
- .num_properties = ARRAY_SIZE(smb347_usb_properties),
1204
-};
1205
-
1206
-static const struct power_supply_desc smb347_battery_desc = {
1207
- .name = "smb347-battery",
1208
- .type = POWER_SUPPLY_TYPE_BATTERY,
1209
- .get_property = smb347_battery_get_property,
1210
- .properties = smb347_battery_properties,
1211
- .num_properties = ARRAY_SIZE(smb347_battery_properties),
1300
+ .get_property = smb347_get_property,
1301
+ .properties = smb347_properties,
1302
+ .num_properties = ARRAY_SIZE(smb347_properties),
12121303 };
12131304
12141305 static int smb347_probe(struct i2c_client *client,
12151306 const struct i2c_device_id *id)
12161307 {
1217
- static char *battery[] = { "smb347-battery" };
1218
- const struct smb347_charger_platform_data *pdata;
1219
- struct power_supply_config mains_usb_cfg = {}, battery_cfg = {};
1308
+ struct power_supply_config mains_usb_cfg = {};
12201309 struct device *dev = &client->dev;
12211310 struct smb347_charger *smb;
12221311 int ret;
12231312
1224
- pdata = dev->platform_data;
1225
- if (!pdata)
1226
- return -EINVAL;
1227
-
1228
- if (!pdata->use_mains && !pdata->use_usb)
1229
- return -EINVAL;
1230
-
12311313 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
12321314 if (!smb)
12331315 return -ENOMEM;
1234
-
1316
+ smb->dev = &client->dev;
1317
+ smb->id = id->driver_data;
12351318 i2c_set_clientdata(client, smb);
12361319
1237
- mutex_init(&smb->lock);
1238
- smb->dev = &client->dev;
1239
- smb->pdata = pdata;
1320
+ smb347_dt_parse_dev_info(smb);
1321
+ if (!smb->use_mains && !smb->use_usb)
1322
+ return -EINVAL;
12401323
12411324 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
12421325 if (IS_ERR(smb->regmap))
12431326 return PTR_ERR(smb->regmap);
12441327
1245
- ret = smb347_hw_init(smb);
1246
- if (ret < 0)
1247
- return ret;
1248
-
1249
- mains_usb_cfg.supplied_to = battery;
1250
- mains_usb_cfg.num_supplicants = ARRAY_SIZE(battery);
12511328 mains_usb_cfg.drv_data = smb;
1252
- if (smb->pdata->use_mains) {
1253
- smb->mains = power_supply_register(dev, &smb347_mains_desc,
1254
- &mains_usb_cfg);
1329
+ mains_usb_cfg.of_node = dev->of_node;
1330
+ if (smb->use_mains) {
1331
+ smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1332
+ &mains_usb_cfg);
12551333 if (IS_ERR(smb->mains))
12561334 return PTR_ERR(smb->mains);
12571335 }
12581336
1259
- if (smb->pdata->use_usb) {
1260
- smb->usb = power_supply_register(dev, &smb347_usb_desc,
1261
- &mains_usb_cfg);
1262
- if (IS_ERR(smb->usb)) {
1263
- if (smb->pdata->use_mains)
1264
- power_supply_unregister(smb->mains);
1337
+ if (smb->use_usb) {
1338
+ smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1339
+ &mains_usb_cfg);
1340
+ if (IS_ERR(smb->usb))
12651341 return PTR_ERR(smb->usb);
1266
- }
12671342 }
12681343
1269
- battery_cfg.drv_data = smb;
1270
- smb->battery = power_supply_register(dev, &smb347_battery_desc,
1271
- &battery_cfg);
1272
- if (IS_ERR(smb->battery)) {
1273
- if (smb->pdata->use_usb)
1274
- power_supply_unregister(smb->usb);
1275
- if (smb->pdata->use_mains)
1276
- power_supply_unregister(smb->mains);
1277
- return PTR_ERR(smb->battery);
1278
- }
1344
+ ret = smb347_get_battery_info(smb);
1345
+ if (ret)
1346
+ return ret;
1347
+
1348
+ ret = smb347_hw_init(smb);
1349
+ if (ret < 0)
1350
+ return ret;
12791351
12801352 /*
12811353 * Interrupt pin is optional. If it is connected, we setup the
12821354 * interrupt support here.
12831355 */
1284
- if (pdata->irq_gpio >= 0) {
1356
+ if (client->irq) {
12851357 ret = smb347_irq_init(smb, client);
12861358 if (ret < 0) {
12871359 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
12881360 dev_warn(dev, "disabling IRQ support\n");
1361
+ smb->irq_unsupported = true;
12891362 } else {
12901363 smb347_irq_enable(smb);
12911364 }
....@@ -1298,29 +1371,31 @@
12981371 {
12991372 struct smb347_charger *smb = i2c_get_clientdata(client);
13001373
1301
- if (client->irq) {
1302
- smb347_irq_disable(smb);
1303
- free_irq(client->irq, smb);
1304
- gpio_free(smb->pdata->irq_gpio);
1305
- }
1374
+ smb347_irq_disable(smb);
13061375
1307
- power_supply_unregister(smb->battery);
1308
- if (smb->pdata->use_usb)
1309
- power_supply_unregister(smb->usb);
1310
- if (smb->pdata->use_mains)
1311
- power_supply_unregister(smb->mains);
13121376 return 0;
13131377 }
13141378
13151379 static const struct i2c_device_id smb347_id[] = {
1316
- { "smb347", 0 },
1317
- { }
1380
+ { "smb345", SMB345 },
1381
+ { "smb347", SMB347 },
1382
+ { "smb358", SMB358 },
1383
+ { },
13181384 };
13191385 MODULE_DEVICE_TABLE(i2c, smb347_id);
1386
+
1387
+static const struct of_device_id smb3xx_of_match[] = {
1388
+ { .compatible = "summit,smb345" },
1389
+ { .compatible = "summit,smb347" },
1390
+ { .compatible = "summit,smb358" },
1391
+ { },
1392
+};
1393
+MODULE_DEVICE_TABLE(of, smb3xx_of_match);
13201394
13211395 static struct i2c_driver smb347_driver = {
13221396 .driver = {
13231397 .name = "smb347",
1398
+ .of_match_table = smb3xx_of_match,
13241399 },
13251400 .probe = smb347_probe,
13261401 .remove = smb347_remove,