Merge tag 'libata-5.14-2021-07-30' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / drivers / power / supply / smb347-charger.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Summit Microelectronics SMB347 Battery Charger Driver
4  *
5  * Copyright (C) 2011, Intel Corporation
6  *
7  * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
8  *          Mika Westerberg <mika.westerberg@linux.intel.com>
9  */
10
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/power_supply.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21
22 #include <dt-bindings/power/summit,smb347-charger.h>
23
24 /* Use the default compensation method */
25 #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
26
27 /* Use default factory programmed value for hard/soft temperature limit */
28 #define SMB3XX_TEMP_USE_DEFAULT         -273
29
30 /*
31  * Configuration registers. These are mirrored to volatile RAM and can be
32  * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
33  * reloaded from non-volatile registers after POR.
34  */
35 #define CFG_CHARGE_CURRENT                      0x00
36 #define CFG_CHARGE_CURRENT_FCC_MASK             0xe0
37 #define CFG_CHARGE_CURRENT_FCC_SHIFT            5
38 #define CFG_CHARGE_CURRENT_PCC_MASK             0x18
39 #define CFG_CHARGE_CURRENT_PCC_SHIFT            3
40 #define CFG_CHARGE_CURRENT_TC_MASK              0x07
41 #define CFG_CURRENT_LIMIT                       0x01
42 #define CFG_CURRENT_LIMIT_DC_MASK               0xf0
43 #define CFG_CURRENT_LIMIT_DC_SHIFT              4
44 #define CFG_CURRENT_LIMIT_USB_MASK              0x0f
45 #define CFG_FLOAT_VOLTAGE                       0x03
46 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK            0x3f
47 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK        0xc0
48 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT       6
49 #define CFG_STAT                                0x05
50 #define CFG_STAT_DISABLED                       BIT(5)
51 #define CFG_STAT_ACTIVE_HIGH                    BIT(7)
52 #define CFG_PIN                                 0x06
53 #define CFG_PIN_EN_CTRL_MASK                    0x60
54 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH             0x40
55 #define CFG_PIN_EN_CTRL_ACTIVE_LOW              0x60
56 #define CFG_PIN_EN_APSD_IRQ                     BIT(1)
57 #define CFG_PIN_EN_CHARGER_ERROR                BIT(2)
58 #define CFG_THERM                               0x07
59 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK    0x03
60 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT   0
61 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK   0x0c
62 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT  2
63 #define CFG_THERM_MONITOR_DISABLED              BIT(4)
64 #define CFG_SYSOK                               0x08
65 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED   BIT(2)
66 #define CFG_OTHER                               0x09
67 #define CFG_OTHER_RID_MASK                      0xc0
68 #define CFG_OTHER_RID_ENABLED_AUTO_OTG          0xc0
69 #define CFG_OTG                                 0x0a
70 #define CFG_OTG_TEMP_THRESHOLD_MASK             0x30
71 #define CFG_OTG_TEMP_THRESHOLD_SHIFT            4
72 #define CFG_OTG_CC_COMPENSATION_MASK            0xc0
73 #define CFG_OTG_CC_COMPENSATION_SHIFT           6
74 #define CFG_TEMP_LIMIT                          0x0b
75 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK            0x03
76 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT           0
77 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK           0x0c
78 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT          2
79 #define CFG_TEMP_LIMIT_HARD_HOT_MASK            0x30
80 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT           4
81 #define CFG_TEMP_LIMIT_HARD_COLD_MASK           0xc0
82 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT          6
83 #define CFG_FAULT_IRQ                           0x0c
84 #define CFG_FAULT_IRQ_DCIN_UV                   BIT(2)
85 #define CFG_STATUS_IRQ                          0x0d
86 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER     BIT(4)
87 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT           BIT(7)
88 #define CFG_ADDRESS                             0x0e
89
90 /* Command registers */
91 #define CMD_A                                   0x30
92 #define CMD_A_CHG_ENABLED                       BIT(1)
93 #define CMD_A_SUSPEND_ENABLED                   BIT(2)
94 #define CMD_A_ALLOW_WRITE                       BIT(7)
95 #define CMD_B                                   0x31
96 #define CMD_C                                   0x33
97
98 /* Interrupt Status registers */
99 #define IRQSTAT_A                               0x35
100 #define IRQSTAT_C                               0x37
101 #define IRQSTAT_C_TERMINATION_STAT              BIT(0)
102 #define IRQSTAT_C_TERMINATION_IRQ               BIT(1)
103 #define IRQSTAT_C_TAPER_IRQ                     BIT(3)
104 #define IRQSTAT_D                               0x38
105 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT           BIT(2)
106 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ            BIT(3)
107 #define IRQSTAT_E                               0x39
108 #define IRQSTAT_E_USBIN_UV_STAT                 BIT(0)
109 #define IRQSTAT_E_USBIN_UV_IRQ                  BIT(1)
110 #define IRQSTAT_E_DCIN_UV_STAT                  BIT(4)
111 #define IRQSTAT_E_DCIN_UV_IRQ                   BIT(5)
112 #define IRQSTAT_F                               0x3a
113
114 /* Status registers */
115 #define STAT_A                                  0x3b
116 #define STAT_A_FLOAT_VOLTAGE_MASK               0x3f
117 #define STAT_B                                  0x3c
118 #define STAT_C                                  0x3d
119 #define STAT_C_CHG_ENABLED                      BIT(0)
120 #define STAT_C_HOLDOFF_STAT                     BIT(3)
121 #define STAT_C_CHG_MASK                         0x06
122 #define STAT_C_CHG_SHIFT                        1
123 #define STAT_C_CHG_TERM                         BIT(5)
124 #define STAT_C_CHARGER_ERROR                    BIT(6)
125 #define STAT_E                                  0x3f
126
127 #define SMB347_MAX_REGISTER                     0x3f
128
129 /**
130  * struct smb347_charger - smb347 charger instance
131  * @dev: pointer to device
132  * @regmap: pointer to driver regmap
133  * @mains: power_supply instance for AC/DC power
134  * @usb: power_supply instance for USB power
135  * @id: SMB charger ID
136  * @mains_online: is AC/DC input connected
137  * @usb_online: is USB input connected
138  * @charging_enabled: is charging enabled
139  * @irq_unsupported: is interrupt unsupported by SMB hardware
140  * @max_charge_current: maximum current (in uA) the battery can be charged
141  * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
142  * @pre_charge_current: current (in uA) to use in pre-charging phase
143  * @termination_current: current (in uA) used to determine when the
144  *                       charging cycle terminates
145  * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to
146  *                       pre-charge to fast charge mode
147  * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
148  * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
149  *                        input
150  * @chip_temp_threshold: die temperature where device starts limiting charge
151  *                       current [%100 - %130] (in degree C)
152  * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
153  *                        granularity is 5 deg C.
154  * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree  C),
155  *                       granularity is 5 deg C.
156  * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
157  *                        granularity is 5 deg C.
158  * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
159  *                       granularity is 5 deg C.
160  * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
161  * @soft_temp_limit_compensation: compensation method when soft temperature
162  *                                limit is hit
163  * @charge_current_compensation: current (in uA) for charging compensation
164  *                               current when temperature hits soft limits
165  * @use_mains: AC/DC input can be used
166  * @use_usb: USB input can be used
167  * @use_usb_otg: USB OTG output can be used (not implemented yet)
168  * @enable_control: how charging enable/disable is controlled
169  *                  (driver/pin controls)
170  *
171  * @use_main, @use_usb, and @use_usb_otg are means to enable/disable
172  * hardware support for these. This is useful when we want to have for
173  * example OTG charging controlled via OTG transceiver driver and not by
174  * the SMB347 hardware.
175  *
176  * Hard and soft temperature limit values are given as described in the
177  * device data sheet and assuming NTC beta value is %3750. Even if this is
178  * not the case, these values should be used. They can be mapped to the
179  * corresponding NTC beta values with the help of table %2 in the data
180  * sheet. So for example if NTC beta is %3375 and we want to program hard
181  * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
182  *
183  * If zero value is given in any of the current and voltage values, the
184  * factory programmed default will be used. For soft/hard temperature
185  * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead.
186  */
187 struct smb347_charger {
188         struct device           *dev;
189         struct regmap           *regmap;
190         struct power_supply     *mains;
191         struct power_supply     *usb;
192         unsigned int            id;
193         bool                    mains_online;
194         bool                    usb_online;
195         bool                    charging_enabled;
196         bool                    irq_unsupported;
197
198         unsigned int            max_charge_current;
199         unsigned int            max_charge_voltage;
200         unsigned int            pre_charge_current;
201         unsigned int            termination_current;
202         unsigned int            pre_to_fast_voltage;
203         unsigned int            mains_current_limit;
204         unsigned int            usb_hc_current_limit;
205         unsigned int            chip_temp_threshold;
206         int                     soft_cold_temp_limit;
207         int                     soft_hot_temp_limit;
208         int                     hard_cold_temp_limit;
209         int                     hard_hot_temp_limit;
210         bool                    suspend_on_hard_temp_limit;
211         unsigned int            soft_temp_limit_compensation;
212         unsigned int            charge_current_compensation;
213         bool                    use_mains;
214         bool                    use_usb;
215         bool                    use_usb_otg;
216         unsigned int            enable_control;
217 };
218
219 enum smb_charger_chipid {
220         SMB345,
221         SMB347,
222         SMB358,
223         NUM_CHIP_TYPES,
224 };
225
226 /* Fast charge current in uA */
227 static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = {
228         [SMB345] = {  200000,  450000,  600000,  900000,
229                      1300000, 1500000, 1800000, 2000000 },
230         [SMB347] = {  700000,  900000, 1200000, 1500000,
231                      1800000, 2000000, 2200000, 2500000 },
232         [SMB358] = {  200000,  450000,  600000,  900000,
233                      1300000, 1500000, 1800000, 2000000 },
234 };
235 /* Pre-charge current in uA */
236 static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = {
237         [SMB345] = { 150000, 250000, 350000, 450000 },
238         [SMB347] = { 100000, 150000, 200000, 250000 },
239         [SMB358] = { 150000, 250000, 350000, 450000 },
240 };
241
242 /* Termination current in uA */
243 static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = {
244         [SMB345] = {  30000,  40000,  60000,  80000,
245                      100000, 125000, 150000, 200000 },
246         [SMB347] = {  37500,  50000, 100000, 150000,
247                      200000, 250000, 500000, 600000 },
248         [SMB358] = {  30000,  40000,  60000,  80000,
249                      100000, 125000, 150000, 200000 },
250 };
251
252 /* Input current limit in uA */
253 static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = {
254         [SMB345] = {  300000,  500000,  700000, 1000000, 1500000,
255                      1800000, 2000000, 2000000, 2000000, 2000000 },
256         [SMB347] = {  300000,  500000,  700000,  900000, 1200000,
257                      1500000, 1800000, 2000000, 2200000, 2500000 },
258         [SMB358] = {  300000,  500000,  700000, 1000000, 1500000,
259                      1800000, 2000000, 2000000, 2000000, 2000000 },
260 };
261
262 /* Charge current compensation in uA */
263 static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
264         [SMB345] = {  200000,  450000,  600000,  900000 },
265         [SMB347] = {  250000,  700000,  900000, 1200000 },
266         [SMB358] = {  200000,  450000,  600000,  900000 },
267 };
268
269 /* Convert register value to current using lookup table */
270 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
271 {
272         if (val >= size)
273                 return -EINVAL;
274         return tbl[val];
275 }
276
277 /* Convert current to register value using lookup table */
278 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
279 {
280         size_t i;
281
282         for (i = 0; i < size; i++)
283                 if (val < tbl[i])
284                         break;
285         return i > 0 ? i - 1 : -EINVAL;
286 }
287
288 /**
289  * smb347_update_ps_status - refreshes the power source status
290  * @smb: pointer to smb347 charger instance
291  *
292  * Function checks whether any power source is connected to the charger and
293  * updates internal state accordingly. If there is a change to previous state
294  * function returns %1, otherwise %0 and negative errno in case of errror.
295  */
296 static int smb347_update_ps_status(struct smb347_charger *smb)
297 {
298         bool usb = false;
299         bool dc = false;
300         unsigned int val;
301         int ret;
302
303         ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
304         if (ret < 0)
305                 return ret;
306
307         /*
308          * Dc and usb are set depending on whether they are enabled in
309          * platform data _and_ whether corresponding undervoltage is set.
310          */
311         if (smb->use_mains)
312                 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
313         if (smb->use_usb)
314                 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
315
316         ret = smb->mains_online != dc || smb->usb_online != usb;
317         smb->mains_online = dc;
318         smb->usb_online = usb;
319
320         return ret;
321 }
322
323 /*
324  * smb347_is_ps_online - returns whether input power source is connected
325  * @smb: pointer to smb347 charger instance
326  *
327  * Returns %true if input power source is connected. Note that this is
328  * dependent on what platform has configured for usable power sources. For
329  * example if USB is disabled, this will return %false even if the USB cable
330  * is connected.
331  */
332 static bool smb347_is_ps_online(struct smb347_charger *smb)
333 {
334         return smb->usb_online || smb->mains_online;
335 }
336
337 /**
338  * smb347_charging_status - returns status of charging
339  * @smb: pointer to smb347 charger instance
340  *
341  * Function returns charging status. %0 means no charging is in progress,
342  * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
343  */
344 static int smb347_charging_status(struct smb347_charger *smb)
345 {
346         unsigned int val;
347         int ret;
348
349         if (!smb347_is_ps_online(smb))
350                 return 0;
351
352         ret = regmap_read(smb->regmap, STAT_C, &val);
353         if (ret < 0)
354                 return 0;
355
356         return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
357 }
358
359 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
360 {
361         int ret = 0;
362
363         if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) {
364                 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
365                 return 0;
366         }
367
368         if (smb->charging_enabled != enable) {
369                 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
370                                          enable ? CMD_A_CHG_ENABLED : 0);
371                 if (!ret)
372                         smb->charging_enabled = enable;
373         }
374
375         return ret;
376 }
377
378 static inline int smb347_charging_enable(struct smb347_charger *smb)
379 {
380         return smb347_charging_set(smb, true);
381 }
382
383 static inline int smb347_charging_disable(struct smb347_charger *smb)
384 {
385         return smb347_charging_set(smb, false);
386 }
387
388 static int smb347_start_stop_charging(struct smb347_charger *smb)
389 {
390         int ret;
391
392         /*
393          * Depending on whether valid power source is connected or not, we
394          * disable or enable the charging. We do it manually because it
395          * depends on how the platform has configured the valid inputs.
396          */
397         if (smb347_is_ps_online(smb)) {
398                 ret = smb347_charging_enable(smb);
399                 if (ret < 0)
400                         dev_err(smb->dev, "failed to enable charging\n");
401         } else {
402                 ret = smb347_charging_disable(smb);
403                 if (ret < 0)
404                         dev_err(smb->dev, "failed to disable charging\n");
405         }
406
407         return ret;
408 }
409
410 static int smb347_set_charge_current(struct smb347_charger *smb)
411 {
412         unsigned int id = smb->id;
413         int ret;
414
415         if (smb->max_charge_current) {
416                 ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]),
417                                     smb->max_charge_current);
418                 if (ret < 0)
419                         return ret;
420
421                 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
422                                          CFG_CHARGE_CURRENT_FCC_MASK,
423                                          ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
424                 if (ret < 0)
425                         return ret;
426         }
427
428         if (smb->pre_charge_current) {
429                 ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]),
430                                     smb->pre_charge_current);
431                 if (ret < 0)
432                         return ret;
433
434                 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
435                                          CFG_CHARGE_CURRENT_PCC_MASK,
436                                          ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
437                 if (ret < 0)
438                         return ret;
439         }
440
441         if (smb->termination_current) {
442                 ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]),
443                                     smb->termination_current);
444                 if (ret < 0)
445                         return ret;
446
447                 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
448                                          CFG_CHARGE_CURRENT_TC_MASK, ret);
449                 if (ret < 0)
450                         return ret;
451         }
452
453         return 0;
454 }
455
456 static int smb347_set_current_limits(struct smb347_charger *smb)
457 {
458         unsigned int id = smb->id;
459         int ret;
460
461         if (smb->mains_current_limit) {
462                 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
463                                     smb->mains_current_limit);
464                 if (ret < 0)
465                         return ret;
466
467                 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
468                                          CFG_CURRENT_LIMIT_DC_MASK,
469                                          ret << CFG_CURRENT_LIMIT_DC_SHIFT);
470                 if (ret < 0)
471                         return ret;
472         }
473
474         if (smb->usb_hc_current_limit) {
475                 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
476                                     smb->usb_hc_current_limit);
477                 if (ret < 0)
478                         return ret;
479
480                 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
481                                          CFG_CURRENT_LIMIT_USB_MASK, ret);
482                 if (ret < 0)
483                         return ret;
484         }
485
486         return 0;
487 }
488
489 static int smb347_set_voltage_limits(struct smb347_charger *smb)
490 {
491         int ret;
492
493         if (smb->pre_to_fast_voltage) {
494                 ret = smb->pre_to_fast_voltage;
495
496                 /* uV */
497                 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
498                 ret /= 200000;
499
500                 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
501                                 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
502                                 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
503                 if (ret < 0)
504                         return ret;
505         }
506
507         if (smb->max_charge_voltage) {
508                 ret = smb->max_charge_voltage;
509
510                 /* uV */
511                 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
512                 ret /= 20000;
513
514                 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
515                                          CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
516                 if (ret < 0)
517                         return ret;
518         }
519
520         return 0;
521 }
522
523 static int smb347_set_temp_limits(struct smb347_charger *smb)
524 {
525         unsigned int id = smb->id;
526         bool enable_therm_monitor = false;
527         int ret = 0;
528         int val;
529
530         if (smb->chip_temp_threshold) {
531                 val = smb->chip_temp_threshold;
532
533                 /* degree C */
534                 val = clamp_val(val, 100, 130) - 100;
535                 val /= 10;
536
537                 ret = regmap_update_bits(smb->regmap, CFG_OTG,
538                                          CFG_OTG_TEMP_THRESHOLD_MASK,
539                                          val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
540                 if (ret < 0)
541                         return ret;
542         }
543
544         if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
545                 val = smb->soft_cold_temp_limit;
546
547                 val = clamp_val(val, 0, 15);
548                 val /= 5;
549                 /* this goes from higher to lower so invert the value */
550                 val = ~val & 0x3;
551
552                 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
553                                          CFG_TEMP_LIMIT_SOFT_COLD_MASK,
554                                          val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
555                 if (ret < 0)
556                         return ret;
557
558                 enable_therm_monitor = true;
559         }
560
561         if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
562                 val = smb->soft_hot_temp_limit;
563
564                 val = clamp_val(val, 40, 55) - 40;
565                 val /= 5;
566
567                 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
568                                          CFG_TEMP_LIMIT_SOFT_HOT_MASK,
569                                          val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
570                 if (ret < 0)
571                         return ret;
572
573                 enable_therm_monitor = true;
574         }
575
576         if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
577                 val = smb->hard_cold_temp_limit;
578
579                 val = clamp_val(val, -5, 10) + 5;
580                 val /= 5;
581                 /* this goes from higher to lower so invert the value */
582                 val = ~val & 0x3;
583
584                 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
585                                          CFG_TEMP_LIMIT_HARD_COLD_MASK,
586                                          val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
587                 if (ret < 0)
588                         return ret;
589
590                 enable_therm_monitor = true;
591         }
592
593         if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
594                 val = smb->hard_hot_temp_limit;
595
596                 val = clamp_val(val, 50, 65) - 50;
597                 val /= 5;
598
599                 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
600                                          CFG_TEMP_LIMIT_HARD_HOT_MASK,
601                                          val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
602                 if (ret < 0)
603                         return ret;
604
605                 enable_therm_monitor = true;
606         }
607
608         /*
609          * If any of the temperature limits are set, we also enable the
610          * thermistor monitoring.
611          *
612          * When soft limits are hit, the device will start to compensate
613          * current and/or voltage depending on the configuration.
614          *
615          * When hard limit is hit, the device will suspend charging
616          * depending on the configuration.
617          */
618         if (enable_therm_monitor) {
619                 ret = regmap_update_bits(smb->regmap, CFG_THERM,
620                                          CFG_THERM_MONITOR_DISABLED, 0);
621                 if (ret < 0)
622                         return ret;
623         }
624
625         if (smb->suspend_on_hard_temp_limit) {
626                 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
627                                  CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
628                 if (ret < 0)
629                         return ret;
630         }
631
632         if (smb->soft_temp_limit_compensation !=
633             SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) {
634                 val = smb->soft_temp_limit_compensation & 0x3;
635
636                 ret = regmap_update_bits(smb->regmap, CFG_THERM,
637                                  CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
638                                  val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
639                 if (ret < 0)
640                         return ret;
641
642                 ret = regmap_update_bits(smb->regmap, CFG_THERM,
643                                  CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
644                                  val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
645                 if (ret < 0)
646                         return ret;
647         }
648
649         if (smb->charge_current_compensation) {
650                 val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]),
651                                     smb->charge_current_compensation);
652                 if (val < 0)
653                         return val;
654
655                 ret = regmap_update_bits(smb->regmap, CFG_OTG,
656                                 CFG_OTG_CC_COMPENSATION_MASK,
657                                 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
658                 if (ret < 0)
659                         return ret;
660         }
661
662         return ret;
663 }
664
665 /*
666  * smb347_set_writable - enables/disables writing to non-volatile registers
667  * @smb: pointer to smb347 charger instance
668  *
669  * You can enable/disable writing to the non-volatile configuration
670  * registers by calling this function.
671  *
672  * Returns %0 on success and negative errno in case of failure.
673  */
674 static int smb347_set_writable(struct smb347_charger *smb, bool writable)
675 {
676         return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
677                                   writable ? CMD_A_ALLOW_WRITE : 0);
678 }
679
680 static int smb347_hw_init(struct smb347_charger *smb)
681 {
682         unsigned int val;
683         int ret;
684
685         ret = smb347_set_writable(smb, true);
686         if (ret < 0)
687                 return ret;
688
689         /*
690          * Program the platform specific configuration values to the device
691          * first.
692          */
693         ret = smb347_set_charge_current(smb);
694         if (ret < 0)
695                 goto fail;
696
697         ret = smb347_set_current_limits(smb);
698         if (ret < 0)
699                 goto fail;
700
701         ret = smb347_set_voltage_limits(smb);
702         if (ret < 0)
703                 goto fail;
704
705         ret = smb347_set_temp_limits(smb);
706         if (ret < 0)
707                 goto fail;
708
709         /* If USB charging is disabled we put the USB in suspend mode */
710         if (!smb->use_usb) {
711                 ret = regmap_update_bits(smb->regmap, CMD_A,
712                                          CMD_A_SUSPEND_ENABLED,
713                                          CMD_A_SUSPEND_ENABLED);
714                 if (ret < 0)
715                         goto fail;
716         }
717
718         /*
719          * If configured by platform data, we enable hardware Auto-OTG
720          * support for driving VBUS. Otherwise we disable it.
721          */
722         ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
723                 smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
724         if (ret < 0)
725                 goto fail;
726
727         /*
728          * Make the charging functionality controllable by a write to the
729          * command register unless pin control is specified in the platform
730          * data.
731          */
732         switch (smb->enable_control) {
733         case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
734                 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
735                 break;
736         case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
737                 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
738                 break;
739         default:
740                 val = 0;
741                 break;
742         }
743
744         ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
745                                  val);
746         if (ret < 0)
747                 goto fail;
748
749         /* Disable Automatic Power Source Detection (APSD) interrupt. */
750         ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
751         if (ret < 0)
752                 goto fail;
753
754         ret = smb347_update_ps_status(smb);
755         if (ret < 0)
756                 goto fail;
757
758         ret = smb347_start_stop_charging(smb);
759
760 fail:
761         smb347_set_writable(smb, false);
762         return ret;
763 }
764
765 static irqreturn_t smb347_interrupt(int irq, void *data)
766 {
767         struct smb347_charger *smb = data;
768         unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
769         bool handled = false;
770         int ret;
771
772         /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
773         usleep_range(25000, 35000);
774
775         ret = regmap_read(smb->regmap, STAT_C, &stat_c);
776         if (ret < 0) {
777                 dev_warn(smb->dev, "reading STAT_C failed\n");
778                 return IRQ_NONE;
779         }
780
781         ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
782         if (ret < 0) {
783                 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
784                 return IRQ_NONE;
785         }
786
787         ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
788         if (ret < 0) {
789                 dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
790                 return IRQ_NONE;
791         }
792
793         ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
794         if (ret < 0) {
795                 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
796                 return IRQ_NONE;
797         }
798
799         /*
800          * If we get charger error we report the error back to user.
801          * If the error is recovered charging will resume again.
802          */
803         if (stat_c & STAT_C_CHARGER_ERROR) {
804                 dev_err(smb->dev, "charging stopped due to charger error\n");
805                 if (smb->use_mains)
806                         power_supply_changed(smb->mains);
807                 if (smb->use_usb)
808                         power_supply_changed(smb->usb);
809                 handled = true;
810         }
811
812         /*
813          * If we reached the termination current the battery is charged and
814          * we can update the status now. Charging is automatically
815          * disabled by the hardware.
816          */
817         if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
818                 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) {
819                         if (smb->use_mains)
820                                 power_supply_changed(smb->mains);
821                         if (smb->use_usb)
822                                 power_supply_changed(smb->usb);
823                 }
824                 dev_dbg(smb->dev, "going to HW maintenance mode\n");
825                 handled = true;
826         }
827
828         /*
829          * If we got a charger timeout INT that means the charge
830          * full is not detected with in charge timeout value.
831          */
832         if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
833                 dev_dbg(smb->dev, "total Charge Timeout INT received\n");
834
835                 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
836                         dev_warn(smb->dev, "charging stopped due to timeout\n");
837                 if (smb->use_mains)
838                         power_supply_changed(smb->mains);
839                 if (smb->use_usb)
840                         power_supply_changed(smb->usb);
841                 handled = true;
842         }
843
844         /*
845          * If we got an under voltage interrupt it means that AC/USB input
846          * was connected or disconnected.
847          */
848         if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
849                 if (smb347_update_ps_status(smb) > 0) {
850                         smb347_start_stop_charging(smb);
851                         if (smb->use_mains)
852                                 power_supply_changed(smb->mains);
853                         if (smb->use_usb)
854                                 power_supply_changed(smb->usb);
855                 }
856                 handled = true;
857         }
858
859         return handled ? IRQ_HANDLED : IRQ_NONE;
860 }
861
862 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
863 {
864         int ret;
865
866         if (smb->irq_unsupported)
867                 return 0;
868
869         ret = smb347_set_writable(smb, true);
870         if (ret < 0)
871                 return ret;
872
873         /*
874          * Enable/disable interrupts for:
875          *      - under voltage
876          *      - termination current reached
877          *      - charger timeout
878          *      - charger error
879          */
880         ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
881                                  enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
882         if (ret < 0)
883                 goto fail;
884
885         ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
886                         enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
887                                         CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
888         if (ret < 0)
889                 goto fail;
890
891         ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
892                                  enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
893 fail:
894         smb347_set_writable(smb, false);
895         return ret;
896 }
897
898 static inline int smb347_irq_enable(struct smb347_charger *smb)
899 {
900         return smb347_irq_set(smb, true);
901 }
902
903 static inline int smb347_irq_disable(struct smb347_charger *smb)
904 {
905         return smb347_irq_set(smb, false);
906 }
907
908 static int smb347_irq_init(struct smb347_charger *smb,
909                            struct i2c_client *client)
910 {
911         int ret;
912
913         smb->irq_unsupported = true;
914
915         /*
916          * Interrupt pin is optional. If it is connected, we setup the
917          * interrupt support here.
918          */
919         if (!client->irq)
920                 return 0;
921
922         ret = smb347_set_writable(smb, true);
923         if (ret < 0)
924                 return ret;
925
926         /*
927          * Configure the STAT output to be suitable for interrupts: disable
928          * all other output (except interrupts) and make it active low.
929          */
930         ret = regmap_update_bits(smb->regmap, CFG_STAT,
931                                  CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
932                                  CFG_STAT_DISABLED);
933
934         smb347_set_writable(smb, false);
935
936         if (ret < 0) {
937                 dev_warn(smb->dev, "failed to initialize IRQ: %d\n", ret);
938                 dev_warn(smb->dev, "disabling IRQ support\n");
939                 return 0;
940         }
941
942         ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
943                                         smb347_interrupt, IRQF_ONESHOT,
944                                         client->name, smb);
945         if (ret)
946                 return ret;
947
948         smb->irq_unsupported = false;
949
950         ret = smb347_irq_enable(smb);
951         if (ret < 0)
952                 return ret;
953
954         return 0;
955 }
956
957 /*
958  * Returns the constant charge current programmed
959  * into the charger in uA.
960  */
961 static int get_const_charge_current(struct smb347_charger *smb)
962 {
963         unsigned int id = smb->id;
964         int ret, intval;
965         unsigned int v;
966
967         if (!smb347_is_ps_online(smb))
968                 return -ENODATA;
969
970         ret = regmap_read(smb->regmap, STAT_B, &v);
971         if (ret < 0)
972                 return ret;
973
974         /*
975          * The current value is composition of FCC and PCC values
976          * and we can detect which table to use from bit 5.
977          */
978         if (v & 0x20) {
979                 intval = hw_to_current(fcc_tbl[id],
980                                        ARRAY_SIZE(fcc_tbl[id]), v & 7);
981         } else {
982                 v >>= 3;
983                 intval = hw_to_current(pcc_tbl[id],
984                                        ARRAY_SIZE(pcc_tbl[id]), v & 7);
985         }
986
987         return intval;
988 }
989
990 /*
991  * Returns the constant charge voltage programmed
992  * into the charger in uV.
993  */
994 static int get_const_charge_voltage(struct smb347_charger *smb)
995 {
996         int ret, intval;
997         unsigned int v;
998
999         if (!smb347_is_ps_online(smb))
1000                 return -ENODATA;
1001
1002         ret = regmap_read(smb->regmap, STAT_A, &v);
1003         if (ret < 0)
1004                 return ret;
1005
1006         v &= STAT_A_FLOAT_VOLTAGE_MASK;
1007         if (v > 0x3d)
1008                 v = 0x3d;
1009
1010         intval = 3500000 + v * 20000;
1011
1012         return intval;
1013 }
1014
1015 static int smb347_get_charging_status(struct smb347_charger *smb,
1016                                       struct power_supply *psy)
1017 {
1018         int ret, status;
1019         unsigned int val;
1020
1021         if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1022                 if (!smb->usb_online)
1023                         return POWER_SUPPLY_STATUS_DISCHARGING;
1024         } else {
1025                 if (!smb->mains_online)
1026                         return POWER_SUPPLY_STATUS_DISCHARGING;
1027         }
1028
1029         ret = regmap_read(smb->regmap, STAT_C, &val);
1030         if (ret < 0)
1031                 return ret;
1032
1033         if ((val & STAT_C_CHARGER_ERROR) ||
1034                         (val & STAT_C_HOLDOFF_STAT)) {
1035                 /*
1036                  * set to NOT CHARGING upon charger error
1037                  * or charging has stopped.
1038                  */
1039                 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1040         } else {
1041                 if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1042                         /*
1043                          * set to charging if battery is in pre-charge,
1044                          * fast charge or taper charging mode.
1045                          */
1046                         status = POWER_SUPPLY_STATUS_CHARGING;
1047                 } else if (val & STAT_C_CHG_TERM) {
1048                         /*
1049                          * set the status to FULL if battery is not in pre
1050                          * charge, fast charge or taper charging mode AND
1051                          * charging is terminated at least once.
1052                          */
1053                         status = POWER_SUPPLY_STATUS_FULL;
1054                 } else {
1055                         /*
1056                          * in this case no charger error or termination
1057                          * occured but charging is not in progress!!!
1058                          */
1059                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1060                 }
1061         }
1062
1063         return status;
1064 }
1065
1066 static int smb347_get_property_locked(struct power_supply *psy,
1067                                       enum power_supply_property prop,
1068                                       union power_supply_propval *val)
1069 {
1070         struct smb347_charger *smb = power_supply_get_drvdata(psy);
1071         int ret;
1072
1073         switch (prop) {
1074         case POWER_SUPPLY_PROP_STATUS:
1075                 ret = smb347_get_charging_status(smb, psy);
1076                 if (ret < 0)
1077                         return ret;
1078                 val->intval = ret;
1079                 break;
1080
1081         case POWER_SUPPLY_PROP_CHARGE_TYPE:
1082                 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1083                         if (!smb->usb_online)
1084                                 return -ENODATA;
1085                 } else {
1086                         if (!smb->mains_online)
1087                                 return -ENODATA;
1088                 }
1089
1090                 /*
1091                  * We handle trickle and pre-charging the same, and taper
1092                  * and none the same.
1093                  */
1094                 switch (smb347_charging_status(smb)) {
1095                 case 1:
1096                         val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1097                         break;
1098                 case 2:
1099                         val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1100                         break;
1101                 default:
1102                         val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1103                         break;
1104                 }
1105                 break;
1106
1107         case POWER_SUPPLY_PROP_ONLINE:
1108                 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1109                         val->intval = smb->usb_online;
1110                 else
1111                         val->intval = smb->mains_online;
1112                 break;
1113
1114         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1115                 ret = get_const_charge_voltage(smb);
1116                 if (ret < 0)
1117                         return ret;
1118                 val->intval = ret;
1119                 break;
1120
1121         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1122                 ret = get_const_charge_current(smb);
1123                 if (ret < 0)
1124                         return ret;
1125                 val->intval = ret;
1126                 break;
1127
1128         default:
1129                 return -EINVAL;
1130         }
1131
1132         return 0;
1133 }
1134
1135 static int smb347_get_property(struct power_supply *psy,
1136                                enum power_supply_property prop,
1137                                union power_supply_propval *val)
1138 {
1139         struct smb347_charger *smb = power_supply_get_drvdata(psy);
1140         struct i2c_client *client = to_i2c_client(smb->dev);
1141         int ret;
1142
1143         if (!smb->irq_unsupported)
1144                 disable_irq(client->irq);
1145
1146         ret = smb347_get_property_locked(psy, prop, val);
1147
1148         if (!smb->irq_unsupported)
1149                 enable_irq(client->irq);
1150
1151         return ret;
1152 }
1153
1154 static enum power_supply_property smb347_properties[] = {
1155         POWER_SUPPLY_PROP_STATUS,
1156         POWER_SUPPLY_PROP_CHARGE_TYPE,
1157         POWER_SUPPLY_PROP_ONLINE,
1158         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1159         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1160 };
1161
1162 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1163 {
1164         switch (reg) {
1165         case IRQSTAT_A:
1166         case IRQSTAT_C:
1167         case IRQSTAT_D:
1168         case IRQSTAT_E:
1169         case IRQSTAT_F:
1170         case STAT_A:
1171         case STAT_B:
1172         case STAT_C:
1173         case STAT_E:
1174                 return true;
1175         }
1176
1177         return false;
1178 }
1179
1180 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1181 {
1182         switch (reg) {
1183         case CFG_CHARGE_CURRENT:
1184         case CFG_CURRENT_LIMIT:
1185         case CFG_FLOAT_VOLTAGE:
1186         case CFG_STAT:
1187         case CFG_PIN:
1188         case CFG_THERM:
1189         case CFG_SYSOK:
1190         case CFG_OTHER:
1191         case CFG_OTG:
1192         case CFG_TEMP_LIMIT:
1193         case CFG_FAULT_IRQ:
1194         case CFG_STATUS_IRQ:
1195         case CFG_ADDRESS:
1196         case CMD_A:
1197         case CMD_B:
1198         case CMD_C:
1199                 return true;
1200         }
1201
1202         return smb347_volatile_reg(dev, reg);
1203 }
1204
1205 static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1206 {
1207         struct device *dev = smb->dev;
1208
1209         smb->soft_temp_limit_compensation =
1210                                         SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1211         /*
1212          * These properties come from the battery info, still we need to
1213          * pre-initialize the values. See smb347_get_battery_info() below.
1214          */
1215         smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1216         smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1217         smb->soft_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1218         smb->hard_hot_temp_limit  = SMB3XX_TEMP_USE_DEFAULT;
1219
1220         /* Charging constraints */
1221         device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1222                                  &smb->pre_to_fast_voltage);
1223         device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1224                                  &smb->mains_current_limit);
1225         device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1226                                  &smb->usb_hc_current_limit);
1227
1228         /* For thermometer monitoring */
1229         device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1230                                  &smb->chip_temp_threshold);
1231         device_property_read_u32(dev, "summit,soft-compensation-method",
1232                                  &smb->soft_temp_limit_compensation);
1233         device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1234                                  &smb->charge_current_compensation);
1235
1236         /* Supported charging mode */
1237         smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1238         smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1239         smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1240
1241         /* Select charging control */
1242         device_property_read_u32(dev, "summit,enable-charge-control",
1243                                  &smb->enable_control);
1244 }
1245
1246 static int smb347_get_battery_info(struct smb347_charger *smb)
1247 {
1248         struct power_supply_battery_info info = {};
1249         struct power_supply *supply;
1250         int err;
1251
1252         if (smb->mains)
1253                 supply = smb->mains;
1254         else
1255                 supply = smb->usb;
1256
1257         err = power_supply_get_battery_info(supply, &info);
1258         if (err == -ENXIO || err == -ENODEV)
1259                 return 0;
1260         if (err)
1261                 return err;
1262
1263         if (info.constant_charge_current_max_ua != -EINVAL)
1264                 smb->max_charge_current = info.constant_charge_current_max_ua;
1265
1266         if (info.constant_charge_voltage_max_uv != -EINVAL)
1267                 smb->max_charge_voltage = info.constant_charge_voltage_max_uv;
1268
1269         if (info.precharge_current_ua != -EINVAL)
1270                 smb->pre_charge_current = info.precharge_current_ua;
1271
1272         if (info.charge_term_current_ua != -EINVAL)
1273                 smb->termination_current = info.charge_term_current_ua;
1274
1275         if (info.temp_alert_min != INT_MIN)
1276                 smb->soft_cold_temp_limit = info.temp_alert_min;
1277
1278         if (info.temp_alert_max != INT_MAX)
1279                 smb->soft_hot_temp_limit = info.temp_alert_max;
1280
1281         if (info.temp_min != INT_MIN)
1282                 smb->hard_cold_temp_limit = info.temp_min;
1283
1284         if (info.temp_max != INT_MAX)
1285                 smb->hard_hot_temp_limit = info.temp_max;
1286
1287         /* Suspend when battery temperature is outside hard limits */
1288         if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1289             smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1290                 smb->suspend_on_hard_temp_limit = true;
1291
1292         return 0;
1293 }
1294
1295 static const struct regmap_config smb347_regmap = {
1296         .reg_bits       = 8,
1297         .val_bits       = 8,
1298         .max_register   = SMB347_MAX_REGISTER,
1299         .volatile_reg   = smb347_volatile_reg,
1300         .readable_reg   = smb347_readable_reg,
1301 };
1302
1303 static const struct power_supply_desc smb347_mains_desc = {
1304         .name           = "smb347-mains",
1305         .type           = POWER_SUPPLY_TYPE_MAINS,
1306         .get_property   = smb347_get_property,
1307         .properties     = smb347_properties,
1308         .num_properties = ARRAY_SIZE(smb347_properties),
1309 };
1310
1311 static const struct power_supply_desc smb347_usb_desc = {
1312         .name           = "smb347-usb",
1313         .type           = POWER_SUPPLY_TYPE_USB,
1314         .get_property   = smb347_get_property,
1315         .properties     = smb347_properties,
1316         .num_properties = ARRAY_SIZE(smb347_properties),
1317 };
1318
1319 static int smb347_probe(struct i2c_client *client,
1320                         const struct i2c_device_id *id)
1321 {
1322         struct power_supply_config mains_usb_cfg = {};
1323         struct device *dev = &client->dev;
1324         struct smb347_charger *smb;
1325         int ret;
1326
1327         smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1328         if (!smb)
1329                 return -ENOMEM;
1330         smb->dev = &client->dev;
1331         smb->id = id->driver_data;
1332         i2c_set_clientdata(client, smb);
1333
1334         smb347_dt_parse_dev_info(smb);
1335         if (!smb->use_mains && !smb->use_usb)
1336                 return -EINVAL;
1337
1338         smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1339         if (IS_ERR(smb->regmap))
1340                 return PTR_ERR(smb->regmap);
1341
1342         mains_usb_cfg.drv_data = smb;
1343         mains_usb_cfg.of_node = dev->of_node;
1344         if (smb->use_mains) {
1345                 smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1346                                                         &mains_usb_cfg);
1347                 if (IS_ERR(smb->mains))
1348                         return PTR_ERR(smb->mains);
1349         }
1350
1351         if (smb->use_usb) {
1352                 smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1353                                                       &mains_usb_cfg);
1354                 if (IS_ERR(smb->usb))
1355                         return PTR_ERR(smb->usb);
1356         }
1357
1358         ret = smb347_get_battery_info(smb);
1359         if (ret)
1360                 return ret;
1361
1362         ret = smb347_hw_init(smb);
1363         if (ret < 0)
1364                 return ret;
1365
1366         ret = smb347_irq_init(smb, client);
1367         if (ret)
1368                 return ret;
1369
1370         return 0;
1371 }
1372
1373 static int smb347_remove(struct i2c_client *client)
1374 {
1375         struct smb347_charger *smb = i2c_get_clientdata(client);
1376
1377         smb347_irq_disable(smb);
1378
1379         return 0;
1380 }
1381
1382 static const struct i2c_device_id smb347_id[] = {
1383         { "smb345", SMB345 },
1384         { "smb347", SMB347 },
1385         { "smb358", SMB358 },
1386         { },
1387 };
1388 MODULE_DEVICE_TABLE(i2c, smb347_id);
1389
1390 static const struct of_device_id smb3xx_of_match[] = {
1391         { .compatible = "summit,smb345" },
1392         { .compatible = "summit,smb347" },
1393         { .compatible = "summit,smb358" },
1394         { },
1395 };
1396 MODULE_DEVICE_TABLE(of, smb3xx_of_match);
1397
1398 static struct i2c_driver smb347_driver = {
1399         .driver = {
1400                 .name = "smb347",
1401                 .of_match_table = smb3xx_of_match,
1402         },
1403         .probe = smb347_probe,
1404         .remove = smb347_remove,
1405         .id_table = smb347_id,
1406 };
1407 module_i2c_driver(smb347_driver);
1408
1409 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1410 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1411 MODULE_DESCRIPTION("SMB347 battery charger driver");
1412 MODULE_LICENSE("GPL");