Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / power / supply / smb347-charger.c
blobc8392933ee285255438a78f5bbd9429f9f1c6764
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Summit Microelectronics SMB347 Battery Charger Driver
5 * Copyright (C) 2011, Intel Corporation
7 * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
8 * Mika Westerberg <mika.westerberg@linux.intel.com>
9 */
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 #include <linux/regulator/driver.h>
23 #include <dt-bindings/power/summit,smb347-charger.h>
25 /* Use the default compensation method */
26 #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
28 /* Use default factory programmed value for hard/soft temperature limit */
29 #define SMB3XX_TEMP_USE_DEFAULT -273
32 * Configuration registers. These are mirrored to volatile RAM and can be
33 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
34 * reloaded from non-volatile registers after POR.
36 #define CFG_CHARGE_CURRENT 0x00
37 #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
38 #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
39 #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
40 #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
41 #define CFG_CHARGE_CURRENT_TC_MASK 0x07
42 #define CFG_CURRENT_LIMIT 0x01
43 #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
44 #define CFG_CURRENT_LIMIT_DC_SHIFT 4
45 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
46 #define CFG_FLOAT_VOLTAGE 0x03
47 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
48 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
49 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
50 #define CFG_STAT 0x05
51 #define CFG_STAT_DISABLED BIT(5)
52 #define CFG_STAT_ACTIVE_HIGH BIT(7)
53 #define CFG_PIN 0x06
54 #define CFG_PIN_EN_CTRL_MASK 0x60
55 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
56 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
57 #define CFG_PIN_EN_APSD_IRQ BIT(1)
58 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
59 #define CFG_PIN_EN_CTRL BIT(4)
60 #define CFG_THERM 0x07
61 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
62 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
63 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
64 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
65 #define CFG_THERM_MONITOR_DISABLED BIT(4)
66 #define CFG_SYSOK 0x08
67 #define CFG_SYSOK_INOK_ACTIVE_HIGH BIT(0)
68 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
69 #define CFG_OTHER 0x09
70 #define CFG_OTHER_RID_MASK 0xc0
71 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
72 #define CFG_OTG 0x0a
73 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
74 #define CFG_OTG_CURRENT_LIMIT_250mA BIT(2)
75 #define CFG_OTG_CURRENT_LIMIT_750mA BIT(3)
76 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
77 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
78 #define CFG_OTG_CC_COMPENSATION_SHIFT 6
79 #define CFG_TEMP_LIMIT 0x0b
80 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
81 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
82 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
83 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
84 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
85 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
86 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
87 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
88 #define CFG_FAULT_IRQ 0x0c
89 #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
90 #define CFG_STATUS_IRQ 0x0d
91 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
92 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7)
93 #define CFG_ADDRESS 0x0e
95 /* Command registers */
96 #define CMD_A 0x30
97 #define CMD_A_CHG_ENABLED BIT(1)
98 #define CMD_A_SUSPEND_ENABLED BIT(2)
99 #define CMD_A_OTG_ENABLED BIT(4)
100 #define CMD_A_ALLOW_WRITE BIT(7)
101 #define CMD_B 0x31
102 #define CMD_C 0x33
104 /* Interrupt Status registers */
105 #define IRQSTAT_A 0x35
106 #define IRQSTAT_C 0x37
107 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
108 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
109 #define IRQSTAT_C_TAPER_IRQ BIT(3)
110 #define IRQSTAT_D 0x38
111 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2)
112 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3)
113 #define IRQSTAT_E 0x39
114 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
115 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
116 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
117 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
118 #define IRQSTAT_F 0x3a
120 /* Status registers */
121 #define STAT_A 0x3b
122 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
123 #define STAT_B 0x3c
124 #define STAT_C 0x3d
125 #define STAT_C_CHG_ENABLED BIT(0)
126 #define STAT_C_HOLDOFF_STAT BIT(3)
127 #define STAT_C_CHG_MASK 0x06
128 #define STAT_C_CHG_SHIFT 1
129 #define STAT_C_CHG_TERM BIT(5)
130 #define STAT_C_CHARGER_ERROR BIT(6)
131 #define STAT_E 0x3f
133 #define SMB347_MAX_REGISTER 0x3f
136 * struct smb347_charger - smb347 charger instance
137 * @dev: pointer to device
138 * @regmap: pointer to driver regmap
139 * @mains: power_supply instance for AC/DC power
140 * @usb: power_supply instance for USB power
141 * @usb_rdev: USB VBUS regulator device
142 * @id: SMB charger ID
143 * @mains_online: is AC/DC input connected
144 * @usb_online: is USB input connected
145 * @irq_unsupported: is interrupt unsupported by SMB hardware
146 * @usb_vbus_enabled: is USB VBUS powered by SMB charger
147 * @max_charge_current: maximum current (in uA) the battery can be charged
148 * @max_charge_voltage: maximum voltage (in uV) the battery can be charged
149 * @pre_charge_current: current (in uA) to use in pre-charging phase
150 * @termination_current: current (in uA) used to determine when the
151 * charging cycle terminates
152 * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to
153 * pre-charge to fast charge mode
154 * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
155 * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
156 * input
157 * @chip_temp_threshold: die temperature where device starts limiting charge
158 * current [%100 - %130] (in degree C)
159 * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
160 * granularity is 5 deg C.
161 * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C),
162 * granularity is 5 deg C.
163 * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
164 * granularity is 5 deg C.
165 * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
166 * granularity is 5 deg C.
167 * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
168 * @soft_temp_limit_compensation: compensation method when soft temperature
169 * limit is hit
170 * @charge_current_compensation: current (in uA) for charging compensation
171 * current when temperature hits soft limits
172 * @use_mains: AC/DC input can be used
173 * @use_usb: USB input can be used
174 * @use_usb_otg: USB OTG output can be used (not implemented yet)
175 * @enable_control: how charging enable/disable is controlled
176 * (driver/pin controls)
177 * @inok_polarity: polarity of INOK signal which denotes presence of external
178 * power supply
180 * @use_main, @use_usb, and @use_usb_otg are means to enable/disable
181 * hardware support for these. This is useful when we want to have for
182 * example OTG charging controlled via OTG transceiver driver and not by
183 * the SMB347 hardware.
185 * Hard and soft temperature limit values are given as described in the
186 * device data sheet and assuming NTC beta value is %3750. Even if this is
187 * not the case, these values should be used. They can be mapped to the
188 * corresponding NTC beta values with the help of table %2 in the data
189 * sheet. So for example if NTC beta is %3375 and we want to program hard
190 * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
192 * If zero value is given in any of the current and voltage values, the
193 * factory programmed default will be used. For soft/hard temperature
194 * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead.
196 struct smb347_charger {
197 struct device *dev;
198 struct regmap *regmap;
199 struct power_supply *mains;
200 struct power_supply *usb;
201 struct regulator_dev *usb_rdev;
202 unsigned int id;
203 bool mains_online;
204 bool usb_online;
205 bool irq_unsupported;
206 bool usb_vbus_enabled;
208 unsigned int max_charge_current;
209 unsigned int max_charge_voltage;
210 unsigned int pre_charge_current;
211 unsigned int termination_current;
212 unsigned int pre_to_fast_voltage;
213 unsigned int mains_current_limit;
214 unsigned int usb_hc_current_limit;
215 unsigned int chip_temp_threshold;
216 int soft_cold_temp_limit;
217 int soft_hot_temp_limit;
218 int hard_cold_temp_limit;
219 int hard_hot_temp_limit;
220 bool suspend_on_hard_temp_limit;
221 unsigned int soft_temp_limit_compensation;
222 unsigned int charge_current_compensation;
223 bool use_mains;
224 bool use_usb;
225 bool use_usb_otg;
226 unsigned int enable_control;
227 unsigned int inok_polarity;
230 enum smb_charger_chipid {
231 SMB345,
232 SMB347,
233 SMB358,
234 NUM_CHIP_TYPES,
237 /* Fast charge current in uA */
238 static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = {
239 [SMB345] = { 200000, 450000, 600000, 900000,
240 1300000, 1500000, 1800000, 2000000 },
241 [SMB347] = { 700000, 900000, 1200000, 1500000,
242 1800000, 2000000, 2200000, 2500000 },
243 [SMB358] = { 200000, 450000, 600000, 900000,
244 1300000, 1500000, 1800000, 2000000 },
246 /* Pre-charge current in uA */
247 static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = {
248 [SMB345] = { 150000, 250000, 350000, 450000 },
249 [SMB347] = { 100000, 150000, 200000, 250000 },
250 [SMB358] = { 150000, 250000, 350000, 450000 },
253 /* Termination current in uA */
254 static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = {
255 [SMB345] = { 30000, 40000, 60000, 80000,
256 100000, 125000, 150000, 200000 },
257 [SMB347] = { 37500, 50000, 100000, 150000,
258 200000, 250000, 500000, 600000 },
259 [SMB358] = { 30000, 40000, 60000, 80000,
260 100000, 125000, 150000, 200000 },
263 /* Input current limit in uA */
264 static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = {
265 [SMB345] = { 300000, 500000, 700000, 1000000, 1500000,
266 1800000, 2000000, 2000000, 2000000, 2000000 },
267 [SMB347] = { 300000, 500000, 700000, 900000, 1200000,
268 1500000, 1800000, 2000000, 2200000, 2500000 },
269 [SMB358] = { 300000, 500000, 700000, 1000000, 1500000,
270 1800000, 2000000, 2000000, 2000000, 2000000 },
273 /* Charge current compensation in uA */
274 static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = {
275 [SMB345] = { 200000, 450000, 600000, 900000 },
276 [SMB347] = { 250000, 700000, 900000, 1200000 },
277 [SMB358] = { 200000, 450000, 600000, 900000 },
280 /* Convert register value to current using lookup table */
281 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
283 if (val >= size)
284 return -EINVAL;
285 return tbl[val];
288 /* Convert current to register value using lookup table */
289 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
291 size_t i;
293 for (i = 0; i < size; i++)
294 if (val < tbl[i])
295 break;
296 return i > 0 ? i - 1 : -EINVAL;
300 * smb347_update_ps_status - refreshes the power source status
301 * @smb: pointer to smb347 charger instance
303 * Function checks whether any power source is connected to the charger and
304 * updates internal state accordingly. If there is a change to previous state
305 * function returns %1, otherwise %0 and negative errno in case of errror.
307 static int smb347_update_ps_status(struct smb347_charger *smb)
309 bool usb = false;
310 bool dc = false;
311 unsigned int val;
312 int ret;
314 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
315 if (ret < 0)
316 return ret;
319 * Dc and usb are set depending on whether they are enabled in
320 * platform data _and_ whether corresponding undervoltage is set.
322 if (smb->use_mains)
323 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
324 if (smb->use_usb)
325 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
327 ret = smb->mains_online != dc || smb->usb_online != usb;
328 smb->mains_online = dc;
329 smb->usb_online = usb;
331 return ret;
335 * smb347_is_ps_online - returns whether input power source is connected
336 * @smb: pointer to smb347 charger instance
338 * Returns %true if input power source is connected. Note that this is
339 * dependent on what platform has configured for usable power sources. For
340 * example if USB is disabled, this will return %false even if the USB cable
341 * is connected.
343 static bool smb347_is_ps_online(struct smb347_charger *smb)
345 return smb->usb_online || smb->mains_online;
349 * smb347_charging_status - returns status of charging
350 * @smb: pointer to smb347 charger instance
352 * Function returns charging status. %0 means no charging is in progress,
353 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
355 static int smb347_charging_status(struct smb347_charger *smb)
357 unsigned int val;
358 int ret;
360 if (!smb347_is_ps_online(smb))
361 return 0;
363 ret = regmap_read(smb->regmap, STAT_C, &val);
364 if (ret < 0)
365 return 0;
367 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
370 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
372 if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) {
373 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
374 return 0;
377 if (enable && smb->usb_vbus_enabled) {
378 dev_dbg(smb->dev, "charging not enabled because USB is in host mode\n");
379 return 0;
382 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
383 enable ? CMD_A_CHG_ENABLED : 0);
386 static inline int smb347_charging_enable(struct smb347_charger *smb)
388 return smb347_charging_set(smb, true);
391 static inline int smb347_charging_disable(struct smb347_charger *smb)
393 return smb347_charging_set(smb, false);
396 static int smb347_start_stop_charging(struct smb347_charger *smb)
398 int ret;
401 * Depending on whether valid power source is connected or not, we
402 * disable or enable the charging. We do it manually because it
403 * depends on how the platform has configured the valid inputs.
405 if (smb347_is_ps_online(smb)) {
406 ret = smb347_charging_enable(smb);
407 if (ret < 0)
408 dev_err(smb->dev, "failed to enable charging\n");
409 } else {
410 ret = smb347_charging_disable(smb);
411 if (ret < 0)
412 dev_err(smb->dev, "failed to disable charging\n");
415 return ret;
418 static int smb347_set_charge_current(struct smb347_charger *smb)
420 unsigned int id = smb->id;
421 int ret;
423 if (smb->max_charge_current) {
424 ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]),
425 smb->max_charge_current);
426 if (ret < 0)
427 return ret;
429 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
430 CFG_CHARGE_CURRENT_FCC_MASK,
431 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
432 if (ret < 0)
433 return ret;
436 if (smb->pre_charge_current) {
437 ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]),
438 smb->pre_charge_current);
439 if (ret < 0)
440 return ret;
442 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
443 CFG_CHARGE_CURRENT_PCC_MASK,
444 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
445 if (ret < 0)
446 return ret;
449 if (smb->termination_current) {
450 ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]),
451 smb->termination_current);
452 if (ret < 0)
453 return ret;
455 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
456 CFG_CHARGE_CURRENT_TC_MASK, ret);
457 if (ret < 0)
458 return ret;
461 return 0;
464 static int smb347_set_current_limits(struct smb347_charger *smb)
466 unsigned int id = smb->id;
467 int ret;
469 if (smb->mains_current_limit) {
470 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
471 smb->mains_current_limit);
472 if (ret < 0)
473 return ret;
475 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
476 CFG_CURRENT_LIMIT_DC_MASK,
477 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
478 if (ret < 0)
479 return ret;
482 if (smb->usb_hc_current_limit) {
483 ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]),
484 smb->usb_hc_current_limit);
485 if (ret < 0)
486 return ret;
488 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
489 CFG_CURRENT_LIMIT_USB_MASK, ret);
490 if (ret < 0)
491 return ret;
494 return 0;
497 static int smb347_set_voltage_limits(struct smb347_charger *smb)
499 int ret;
501 if (smb->pre_to_fast_voltage) {
502 ret = smb->pre_to_fast_voltage;
504 /* uV */
505 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
506 ret /= 200000;
508 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
509 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
510 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
511 if (ret < 0)
512 return ret;
515 if (smb->max_charge_voltage) {
516 ret = smb->max_charge_voltage;
518 /* uV */
519 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
520 ret /= 20000;
522 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
523 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
524 if (ret < 0)
525 return ret;
528 return 0;
531 static int smb347_set_temp_limits(struct smb347_charger *smb)
533 unsigned int id = smb->id;
534 bool enable_therm_monitor = false;
535 int ret = 0;
536 int val;
538 if (smb->chip_temp_threshold) {
539 val = smb->chip_temp_threshold;
541 /* degree C */
542 val = clamp_val(val, 100, 130) - 100;
543 val /= 10;
545 ret = regmap_update_bits(smb->regmap, CFG_OTG,
546 CFG_OTG_TEMP_THRESHOLD_MASK,
547 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
548 if (ret < 0)
549 return ret;
552 if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
553 val = smb->soft_cold_temp_limit;
555 val = clamp_val(val, 0, 15);
556 val /= 5;
557 /* this goes from higher to lower so invert the value */
558 val = ~val & 0x3;
560 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
561 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
562 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
563 if (ret < 0)
564 return ret;
566 enable_therm_monitor = true;
569 if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
570 val = smb->soft_hot_temp_limit;
572 val = clamp_val(val, 40, 55) - 40;
573 val /= 5;
575 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
576 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
577 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
578 if (ret < 0)
579 return ret;
581 enable_therm_monitor = true;
584 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
585 val = smb->hard_cold_temp_limit;
587 val = clamp_val(val, -5, 10) + 5;
588 val /= 5;
589 /* this goes from higher to lower so invert the value */
590 val = ~val & 0x3;
592 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
593 CFG_TEMP_LIMIT_HARD_COLD_MASK,
594 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
595 if (ret < 0)
596 return ret;
598 enable_therm_monitor = true;
601 if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) {
602 val = smb->hard_hot_temp_limit;
604 val = clamp_val(val, 50, 65) - 50;
605 val /= 5;
607 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
608 CFG_TEMP_LIMIT_HARD_HOT_MASK,
609 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
610 if (ret < 0)
611 return ret;
613 enable_therm_monitor = true;
617 * If any of the temperature limits are set, we also enable the
618 * thermistor monitoring.
620 * When soft limits are hit, the device will start to compensate
621 * current and/or voltage depending on the configuration.
623 * When hard limit is hit, the device will suspend charging
624 * depending on the configuration.
626 if (enable_therm_monitor) {
627 ret = regmap_update_bits(smb->regmap, CFG_THERM,
628 CFG_THERM_MONITOR_DISABLED, 0);
629 if (ret < 0)
630 return ret;
633 if (smb->suspend_on_hard_temp_limit) {
634 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
635 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
636 if (ret < 0)
637 return ret;
640 if (smb->soft_temp_limit_compensation !=
641 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) {
642 val = smb->soft_temp_limit_compensation & 0x3;
644 ret = regmap_update_bits(smb->regmap, CFG_THERM,
645 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
646 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
647 if (ret < 0)
648 return ret;
650 ret = regmap_update_bits(smb->regmap, CFG_THERM,
651 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
652 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
653 if (ret < 0)
654 return ret;
657 if (smb->charge_current_compensation) {
658 val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]),
659 smb->charge_current_compensation);
660 if (val < 0)
661 return val;
663 ret = regmap_update_bits(smb->regmap, CFG_OTG,
664 CFG_OTG_CC_COMPENSATION_MASK,
665 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
666 if (ret < 0)
667 return ret;
670 return ret;
674 * smb347_set_writable - enables/disables writing to non-volatile registers
675 * @smb: pointer to smb347 charger instance
677 * You can enable/disable writing to the non-volatile configuration
678 * registers by calling this function.
680 * Returns %0 on success and negative errno in case of failure.
682 static int smb347_set_writable(struct smb347_charger *smb, bool writable,
683 bool irq_toggle)
685 struct i2c_client *client = to_i2c_client(smb->dev);
686 int ret;
688 if (writable && irq_toggle && !smb->irq_unsupported)
689 disable_irq(client->irq);
691 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
692 writable ? CMD_A_ALLOW_WRITE : 0);
694 if ((!writable || ret) && irq_toggle && !smb->irq_unsupported)
695 enable_irq(client->irq);
697 return ret;
700 static int smb347_hw_init(struct smb347_charger *smb)
702 unsigned int val;
703 int ret;
705 ret = smb347_set_writable(smb, true, false);
706 if (ret < 0)
707 return ret;
710 * Program the platform specific configuration values to the device
711 * first.
713 ret = smb347_set_charge_current(smb);
714 if (ret < 0)
715 goto fail;
717 ret = smb347_set_current_limits(smb);
718 if (ret < 0)
719 goto fail;
721 ret = smb347_set_voltage_limits(smb);
722 if (ret < 0)
723 goto fail;
725 ret = smb347_set_temp_limits(smb);
726 if (ret < 0)
727 goto fail;
729 /* If USB charging is disabled we put the USB in suspend mode */
730 if (!smb->use_usb) {
731 ret = regmap_update_bits(smb->regmap, CMD_A,
732 CMD_A_SUSPEND_ENABLED,
733 CMD_A_SUSPEND_ENABLED);
734 if (ret < 0)
735 goto fail;
739 * If configured by platform data, we enable hardware Auto-OTG
740 * support for driving VBUS. Otherwise we disable it.
742 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
743 smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
744 if (ret < 0)
745 goto fail;
747 /* Activate pin control, making it writable. */
748 switch (smb->enable_control) {
749 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
750 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
751 ret = regmap_set_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL);
752 if (ret < 0)
753 goto fail;
757 * Make the charging functionality controllable by a write to the
758 * command register unless pin control is specified in the platform
759 * data.
761 switch (smb->enable_control) {
762 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW:
763 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
764 break;
765 case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH:
766 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
767 break;
768 default:
769 val = 0;
770 break;
773 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
774 val);
775 if (ret < 0)
776 goto fail;
778 /* Disable Automatic Power Source Detection (APSD) interrupt. */
779 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
780 if (ret < 0)
781 goto fail;
783 ret = smb347_update_ps_status(smb);
784 if (ret < 0)
785 goto fail;
787 ret = smb347_start_stop_charging(smb);
789 fail:
790 smb347_set_writable(smb, false, false);
791 return ret;
794 static irqreturn_t smb347_interrupt(int irq, void *data)
796 struct smb347_charger *smb = data;
797 unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
798 bool handled = false;
799 int ret;
801 /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */
802 usleep_range(25000, 35000);
804 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
805 if (ret < 0) {
806 dev_warn(smb->dev, "reading STAT_C failed\n");
807 return IRQ_NONE;
810 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
811 if (ret < 0) {
812 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
813 return IRQ_NONE;
816 ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
817 if (ret < 0) {
818 dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
819 return IRQ_NONE;
822 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
823 if (ret < 0) {
824 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
825 return IRQ_NONE;
829 * If we get charger error we report the error back to user.
830 * If the error is recovered charging will resume again.
832 if (stat_c & STAT_C_CHARGER_ERROR) {
833 dev_err(smb->dev, "charging stopped due to charger error\n");
834 if (smb->use_mains)
835 power_supply_changed(smb->mains);
836 if (smb->use_usb)
837 power_supply_changed(smb->usb);
838 handled = true;
842 * If we reached the termination current the battery is charged and
843 * we can update the status now. Charging is automatically
844 * disabled by the hardware.
846 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
847 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) {
848 if (smb->use_mains)
849 power_supply_changed(smb->mains);
850 if (smb->use_usb)
851 power_supply_changed(smb->usb);
853 dev_dbg(smb->dev, "going to HW maintenance mode\n");
854 handled = true;
858 * If we got a charger timeout INT that means the charge
859 * full is not detected with in charge timeout value.
861 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
862 dev_dbg(smb->dev, "total Charge Timeout INT received\n");
864 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
865 dev_warn(smb->dev, "charging stopped due to timeout\n");
866 if (smb->use_mains)
867 power_supply_changed(smb->mains);
868 if (smb->use_usb)
869 power_supply_changed(smb->usb);
870 handled = true;
874 * If we got an under voltage interrupt it means that AC/USB input
875 * was connected or disconnected.
877 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
878 if (smb347_update_ps_status(smb) > 0) {
879 smb347_start_stop_charging(smb);
880 if (smb->use_mains)
881 power_supply_changed(smb->mains);
882 if (smb->use_usb)
883 power_supply_changed(smb->usb);
885 handled = true;
888 return handled ? IRQ_HANDLED : IRQ_NONE;
891 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
893 int ret;
895 if (smb->irq_unsupported)
896 return 0;
898 ret = smb347_set_writable(smb, true, true);
899 if (ret < 0)
900 return ret;
903 * Enable/disable interrupts for:
904 * - under voltage
905 * - termination current reached
906 * - charger timeout
907 * - charger error
909 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
910 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
911 if (ret < 0)
912 goto fail;
914 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
915 enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
916 CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
917 if (ret < 0)
918 goto fail;
920 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
921 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
922 fail:
923 smb347_set_writable(smb, false, true);
924 return ret;
927 static inline int smb347_irq_enable(struct smb347_charger *smb)
929 return smb347_irq_set(smb, true);
932 static inline int smb347_irq_disable(struct smb347_charger *smb)
934 return smb347_irq_set(smb, false);
937 static int smb347_irq_init(struct smb347_charger *smb,
938 struct i2c_client *client)
940 int ret;
942 smb->irq_unsupported = true;
945 * Interrupt pin is optional. If it is connected, we setup the
946 * interrupt support here.
948 if (!client->irq)
949 return 0;
951 ret = smb347_set_writable(smb, true, false);
952 if (ret < 0)
953 return ret;
956 * Configure the STAT output to be suitable for interrupts: disable
957 * all other output (except interrupts) and make it active low.
959 ret = regmap_update_bits(smb->regmap, CFG_STAT,
960 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
961 CFG_STAT_DISABLED);
963 smb347_set_writable(smb, false, false);
965 if (ret < 0) {
966 dev_warn(smb->dev, "failed to initialize IRQ: %d\n", ret);
967 dev_warn(smb->dev, "disabling IRQ support\n");
968 return 0;
971 ret = devm_request_threaded_irq(smb->dev, client->irq, NULL,
972 smb347_interrupt, IRQF_ONESHOT,
973 client->name, smb);
974 if (ret)
975 return ret;
977 smb->irq_unsupported = false;
979 ret = smb347_irq_enable(smb);
980 if (ret < 0)
981 return ret;
983 return 0;
987 * Returns the constant charge current programmed
988 * into the charger in uA.
990 static int get_const_charge_current(struct smb347_charger *smb)
992 unsigned int id = smb->id;
993 int ret, intval;
994 unsigned int v;
996 if (!smb347_is_ps_online(smb))
997 return -ENODATA;
999 ret = regmap_read(smb->regmap, STAT_B, &v);
1000 if (ret < 0)
1001 return ret;
1004 * The current value is composition of FCC and PCC values
1005 * and we can detect which table to use from bit 5.
1007 if (v & 0x20) {
1008 intval = hw_to_current(fcc_tbl[id],
1009 ARRAY_SIZE(fcc_tbl[id]), v & 7);
1010 } else {
1011 v >>= 3;
1012 intval = hw_to_current(pcc_tbl[id],
1013 ARRAY_SIZE(pcc_tbl[id]), v & 7);
1016 return intval;
1020 * Returns the constant charge voltage programmed
1021 * into the charger in uV.
1023 static int get_const_charge_voltage(struct smb347_charger *smb)
1025 int ret, intval;
1026 unsigned int v;
1028 if (!smb347_is_ps_online(smb))
1029 return -ENODATA;
1031 ret = regmap_read(smb->regmap, STAT_A, &v);
1032 if (ret < 0)
1033 return ret;
1035 v &= STAT_A_FLOAT_VOLTAGE_MASK;
1036 if (v > 0x3d)
1037 v = 0x3d;
1039 intval = 3500000 + v * 20000;
1041 return intval;
1044 static int smb347_get_charging_status(struct smb347_charger *smb,
1045 struct power_supply *psy)
1047 int ret, status;
1048 unsigned int val;
1050 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1051 if (!smb->usb_online)
1052 return POWER_SUPPLY_STATUS_DISCHARGING;
1053 } else {
1054 if (!smb->mains_online)
1055 return POWER_SUPPLY_STATUS_DISCHARGING;
1058 ret = regmap_read(smb->regmap, STAT_C, &val);
1059 if (ret < 0)
1060 return ret;
1062 if ((val & STAT_C_CHARGER_ERROR) ||
1063 (val & STAT_C_HOLDOFF_STAT)) {
1065 * set to NOT CHARGING upon charger error
1066 * or charging has stopped.
1068 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1069 } else {
1070 if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1072 * set to charging if battery is in pre-charge,
1073 * fast charge or taper charging mode.
1075 status = POWER_SUPPLY_STATUS_CHARGING;
1076 } else if (val & STAT_C_CHG_TERM) {
1078 * set the status to FULL if battery is not in pre
1079 * charge, fast charge or taper charging mode AND
1080 * charging is terminated at least once.
1082 status = POWER_SUPPLY_STATUS_FULL;
1083 } else {
1085 * in this case no charger error or termination
1086 * occured but charging is not in progress!!!
1088 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1092 return status;
1095 static int smb347_get_property_locked(struct power_supply *psy,
1096 enum power_supply_property prop,
1097 union power_supply_propval *val)
1099 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1100 int ret;
1102 switch (prop) {
1103 case POWER_SUPPLY_PROP_STATUS:
1104 ret = smb347_get_charging_status(smb, psy);
1105 if (ret < 0)
1106 return ret;
1107 val->intval = ret;
1108 break;
1110 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1111 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
1112 if (!smb->usb_online)
1113 return -ENODATA;
1114 } else {
1115 if (!smb->mains_online)
1116 return -ENODATA;
1120 * We handle trickle and pre-charging the same, and taper
1121 * and none the same.
1123 switch (smb347_charging_status(smb)) {
1124 case 1:
1125 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1126 break;
1127 case 2:
1128 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1129 break;
1130 default:
1131 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1132 break;
1134 break;
1136 case POWER_SUPPLY_PROP_ONLINE:
1137 if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
1138 val->intval = smb->usb_online;
1139 else
1140 val->intval = smb->mains_online;
1141 break;
1143 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1144 ret = get_const_charge_voltage(smb);
1145 if (ret < 0)
1146 return ret;
1147 val->intval = ret;
1148 break;
1150 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1151 ret = get_const_charge_current(smb);
1152 if (ret < 0)
1153 return ret;
1154 val->intval = ret;
1155 break;
1157 default:
1158 return -EINVAL;
1161 return 0;
1164 static int smb347_get_property(struct power_supply *psy,
1165 enum power_supply_property prop,
1166 union power_supply_propval *val)
1168 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1169 struct i2c_client *client = to_i2c_client(smb->dev);
1170 int ret;
1172 if (!smb->irq_unsupported)
1173 disable_irq(client->irq);
1175 ret = smb347_get_property_locked(psy, prop, val);
1177 if (!smb->irq_unsupported)
1178 enable_irq(client->irq);
1180 return ret;
1183 static enum power_supply_property smb347_properties[] = {
1184 POWER_SUPPLY_PROP_STATUS,
1185 POWER_SUPPLY_PROP_CHARGE_TYPE,
1186 POWER_SUPPLY_PROP_ONLINE,
1187 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1188 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1191 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1193 switch (reg) {
1194 case IRQSTAT_A:
1195 case IRQSTAT_C:
1196 case IRQSTAT_D:
1197 case IRQSTAT_E:
1198 case IRQSTAT_F:
1199 case STAT_A:
1200 case STAT_B:
1201 case STAT_C:
1202 case STAT_E:
1203 return true;
1206 return false;
1209 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1211 switch (reg) {
1212 case CFG_CHARGE_CURRENT:
1213 case CFG_CURRENT_LIMIT:
1214 case CFG_FLOAT_VOLTAGE:
1215 case CFG_STAT:
1216 case CFG_PIN:
1217 case CFG_THERM:
1218 case CFG_SYSOK:
1219 case CFG_OTHER:
1220 case CFG_OTG:
1221 case CFG_TEMP_LIMIT:
1222 case CFG_FAULT_IRQ:
1223 case CFG_STATUS_IRQ:
1224 case CFG_ADDRESS:
1225 case CMD_A:
1226 case CMD_B:
1227 case CMD_C:
1228 return true;
1231 return smb347_volatile_reg(dev, reg);
1234 static void smb347_dt_parse_dev_info(struct smb347_charger *smb)
1236 struct device *dev = smb->dev;
1238 smb->soft_temp_limit_compensation =
1239 SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT;
1241 * These properties come from the battery info, still we need to
1242 * pre-initialize the values. See smb347_get_battery_info() below.
1244 smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1245 smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1246 smb->soft_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1247 smb->hard_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT;
1249 /* Charging constraints */
1250 device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt",
1251 &smb->pre_to_fast_voltage);
1252 device_property_read_u32(dev, "summit,mains-current-limit-microamp",
1253 &smb->mains_current_limit);
1254 device_property_read_u32(dev, "summit,usb-current-limit-microamp",
1255 &smb->usb_hc_current_limit);
1257 /* For thermometer monitoring */
1258 device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius",
1259 &smb->chip_temp_threshold);
1260 device_property_read_u32(dev, "summit,soft-compensation-method",
1261 &smb->soft_temp_limit_compensation);
1262 device_property_read_u32(dev, "summit,charge-current-compensation-microamp",
1263 &smb->charge_current_compensation);
1265 /* Supported charging mode */
1266 smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging");
1267 smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging");
1268 smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging");
1270 /* Select charging control */
1271 device_property_read_u32(dev, "summit,enable-charge-control",
1272 &smb->enable_control);
1275 * Polarity of INOK signal indicating presence of external power
1276 * supply connected to the charger.
1278 device_property_read_u32(dev, "summit,inok-polarity",
1279 &smb->inok_polarity);
1282 static int smb347_get_battery_info(struct smb347_charger *smb)
1284 struct power_supply_battery_info *info;
1285 struct power_supply *supply;
1286 int err;
1288 if (smb->mains)
1289 supply = smb->mains;
1290 else
1291 supply = smb->usb;
1293 err = power_supply_get_battery_info(supply, &info);
1294 if (err == -ENXIO || err == -ENODEV)
1295 return 0;
1296 if (err)
1297 return err;
1299 if (info->constant_charge_current_max_ua != -EINVAL)
1300 smb->max_charge_current = info->constant_charge_current_max_ua;
1302 if (info->constant_charge_voltage_max_uv != -EINVAL)
1303 smb->max_charge_voltage = info->constant_charge_voltage_max_uv;
1305 if (info->precharge_current_ua != -EINVAL)
1306 smb->pre_charge_current = info->precharge_current_ua;
1308 if (info->charge_term_current_ua != -EINVAL)
1309 smb->termination_current = info->charge_term_current_ua;
1311 if (info->temp_alert_min != INT_MIN)
1312 smb->soft_cold_temp_limit = info->temp_alert_min;
1314 if (info->temp_alert_max != INT_MAX)
1315 smb->soft_hot_temp_limit = info->temp_alert_max;
1317 if (info->temp_min != INT_MIN)
1318 smb->hard_cold_temp_limit = info->temp_min;
1320 if (info->temp_max != INT_MAX)
1321 smb->hard_hot_temp_limit = info->temp_max;
1323 /* Suspend when battery temperature is outside hard limits */
1324 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT ||
1325 smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT)
1326 smb->suspend_on_hard_temp_limit = true;
1328 return 0;
1331 static int smb347_usb_vbus_get_current_limit(struct regulator_dev *rdev)
1333 struct smb347_charger *smb = rdev_get_drvdata(rdev);
1334 unsigned int val;
1335 int ret;
1337 ret = regmap_read(smb->regmap, CFG_OTG, &val);
1338 if (ret < 0)
1339 return ret;
1342 * It's unknown what happens if this bit is unset due to lack of
1343 * access to the datasheet, assume it's limit-enable.
1345 if (!(val & CFG_OTG_CURRENT_LIMIT_250mA))
1346 return 0;
1348 return val & CFG_OTG_CURRENT_LIMIT_750mA ? 750000 : 250000;
1351 static int smb347_usb_vbus_set_new_current_limit(struct smb347_charger *smb,
1352 int max_uA)
1354 const unsigned int mask = CFG_OTG_CURRENT_LIMIT_750mA |
1355 CFG_OTG_CURRENT_LIMIT_250mA;
1356 unsigned int val = CFG_OTG_CURRENT_LIMIT_250mA;
1357 int ret;
1359 if (max_uA >= 750000)
1360 val |= CFG_OTG_CURRENT_LIMIT_750mA;
1362 ret = regmap_update_bits(smb->regmap, CFG_OTG, mask, val);
1363 if (ret < 0)
1364 dev_err(smb->dev, "failed to change USB current limit\n");
1366 return ret;
1369 static int smb347_usb_vbus_set_current_limit(struct regulator_dev *rdev,
1370 int min_uA, int max_uA)
1372 struct smb347_charger *smb = rdev_get_drvdata(rdev);
1373 int ret;
1375 ret = smb347_set_writable(smb, true, true);
1376 if (ret < 0)
1377 return ret;
1379 ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA);
1380 smb347_set_writable(smb, false, true);
1382 return ret;
1385 static int smb347_usb_vbus_regulator_enable(struct regulator_dev *rdev)
1387 struct smb347_charger *smb = rdev_get_drvdata(rdev);
1388 int ret, max_uA;
1390 ret = smb347_set_writable(smb, true, true);
1391 if (ret < 0)
1392 return ret;
1394 smb347_charging_disable(smb);
1396 if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) {
1397 unsigned int sysok = 0;
1399 if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_LOW)
1400 sysok = CFG_SYSOK_INOK_ACTIVE_HIGH;
1403 * VBUS won't be powered if INOK is active, so we need to
1404 * manually disable INOK on some platforms.
1406 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
1407 CFG_SYSOK_INOK_ACTIVE_HIGH, sysok);
1408 if (ret < 0) {
1409 dev_err(smb->dev, "failed to disable INOK\n");
1410 goto done;
1414 ret = smb347_usb_vbus_get_current_limit(rdev);
1415 if (ret < 0) {
1416 dev_err(smb->dev, "failed to get USB VBUS current limit\n");
1417 goto done;
1420 max_uA = ret;
1422 ret = smb347_usb_vbus_set_new_current_limit(smb, 250000);
1423 if (ret < 0) {
1424 dev_err(smb->dev, "failed to preset USB VBUS current limit\n");
1425 goto done;
1428 ret = regmap_set_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED);
1429 if (ret < 0) {
1430 dev_err(smb->dev, "failed to enable USB VBUS\n");
1431 goto done;
1434 smb->usb_vbus_enabled = true;
1436 ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA);
1437 if (ret < 0) {
1438 dev_err(smb->dev, "failed to restore USB VBUS current limit\n");
1439 goto done;
1441 done:
1442 smb347_set_writable(smb, false, true);
1444 return ret;
1447 static int smb347_usb_vbus_regulator_disable(struct regulator_dev *rdev)
1449 struct smb347_charger *smb = rdev_get_drvdata(rdev);
1450 int ret;
1452 ret = smb347_set_writable(smb, true, true);
1453 if (ret < 0)
1454 return ret;
1456 ret = regmap_clear_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED);
1457 if (ret < 0) {
1458 dev_err(smb->dev, "failed to disable USB VBUS\n");
1459 goto done;
1462 smb->usb_vbus_enabled = false;
1464 if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) {
1465 unsigned int sysok = 0;
1467 if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_HIGH)
1468 sysok = CFG_SYSOK_INOK_ACTIVE_HIGH;
1470 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
1471 CFG_SYSOK_INOK_ACTIVE_HIGH, sysok);
1472 if (ret < 0) {
1473 dev_err(smb->dev, "failed to enable INOK\n");
1474 goto done;
1478 smb347_start_stop_charging(smb);
1479 done:
1480 smb347_set_writable(smb, false, true);
1482 return ret;
1485 static const struct regmap_config smb347_regmap = {
1486 .reg_bits = 8,
1487 .val_bits = 8,
1488 .max_register = SMB347_MAX_REGISTER,
1489 .volatile_reg = smb347_volatile_reg,
1490 .readable_reg = smb347_readable_reg,
1491 .cache_type = REGCACHE_RBTREE,
1494 static const struct regulator_ops smb347_usb_vbus_regulator_ops = {
1495 .is_enabled = regulator_is_enabled_regmap,
1496 .enable = smb347_usb_vbus_regulator_enable,
1497 .disable = smb347_usb_vbus_regulator_disable,
1498 .get_current_limit = smb347_usb_vbus_get_current_limit,
1499 .set_current_limit = smb347_usb_vbus_set_current_limit,
1502 static const struct power_supply_desc smb347_mains_desc = {
1503 .name = "smb347-mains",
1504 .type = POWER_SUPPLY_TYPE_MAINS,
1505 .get_property = smb347_get_property,
1506 .properties = smb347_properties,
1507 .num_properties = ARRAY_SIZE(smb347_properties),
1510 static const struct power_supply_desc smb347_usb_desc = {
1511 .name = "smb347-usb",
1512 .type = POWER_SUPPLY_TYPE_USB,
1513 .get_property = smb347_get_property,
1514 .properties = smb347_properties,
1515 .num_properties = ARRAY_SIZE(smb347_properties),
1518 static const struct regulator_desc smb347_usb_vbus_regulator_desc = {
1519 .name = "smb347-usb-vbus",
1520 .of_match = of_match_ptr("usb-vbus"),
1521 .ops = &smb347_usb_vbus_regulator_ops,
1522 .type = REGULATOR_VOLTAGE,
1523 .owner = THIS_MODULE,
1524 .enable_reg = CMD_A,
1525 .enable_mask = CMD_A_OTG_ENABLED,
1526 .enable_val = CMD_A_OTG_ENABLED,
1527 .fixed_uV = 5000000,
1528 .n_voltages = 1,
1531 static int smb347_probe(struct i2c_client *client)
1533 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1534 struct power_supply_config mains_usb_cfg = {};
1535 struct regulator_config usb_rdev_cfg = {};
1536 struct device *dev = &client->dev;
1537 struct smb347_charger *smb;
1538 int ret;
1540 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1541 if (!smb)
1542 return -ENOMEM;
1543 smb->dev = &client->dev;
1544 smb->id = id->driver_data;
1545 i2c_set_clientdata(client, smb);
1547 smb347_dt_parse_dev_info(smb);
1548 if (!smb->use_mains && !smb->use_usb)
1549 return -EINVAL;
1551 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1552 if (IS_ERR(smb->regmap))
1553 return PTR_ERR(smb->regmap);
1555 mains_usb_cfg.drv_data = smb;
1556 mains_usb_cfg.of_node = dev->of_node;
1557 if (smb->use_mains) {
1558 smb->mains = devm_power_supply_register(dev, &smb347_mains_desc,
1559 &mains_usb_cfg);
1560 if (IS_ERR(smb->mains))
1561 return PTR_ERR(smb->mains);
1564 if (smb->use_usb) {
1565 smb->usb = devm_power_supply_register(dev, &smb347_usb_desc,
1566 &mains_usb_cfg);
1567 if (IS_ERR(smb->usb))
1568 return PTR_ERR(smb->usb);
1571 ret = smb347_get_battery_info(smb);
1572 if (ret)
1573 return ret;
1575 ret = smb347_hw_init(smb);
1576 if (ret < 0)
1577 return ret;
1579 ret = smb347_irq_init(smb, client);
1580 if (ret)
1581 return ret;
1583 usb_rdev_cfg.dev = dev;
1584 usb_rdev_cfg.driver_data = smb;
1585 usb_rdev_cfg.regmap = smb->regmap;
1587 smb->usb_rdev = devm_regulator_register(dev,
1588 &smb347_usb_vbus_regulator_desc,
1589 &usb_rdev_cfg);
1590 if (IS_ERR(smb->usb_rdev)) {
1591 smb347_irq_disable(smb);
1592 return PTR_ERR(smb->usb_rdev);
1595 return 0;
1598 static void smb347_remove(struct i2c_client *client)
1600 struct smb347_charger *smb = i2c_get_clientdata(client);
1602 smb347_usb_vbus_regulator_disable(smb->usb_rdev);
1603 smb347_irq_disable(smb);
1606 static void smb347_shutdown(struct i2c_client *client)
1608 smb347_remove(client);
1611 static const struct i2c_device_id smb347_id[] = {
1612 { "smb345", SMB345 },
1613 { "smb347", SMB347 },
1614 { "smb358", SMB358 },
1615 { },
1617 MODULE_DEVICE_TABLE(i2c, smb347_id);
1619 static const struct of_device_id smb3xx_of_match[] = {
1620 { .compatible = "summit,smb345" },
1621 { .compatible = "summit,smb347" },
1622 { .compatible = "summit,smb358" },
1623 { },
1625 MODULE_DEVICE_TABLE(of, smb3xx_of_match);
1627 static struct i2c_driver smb347_driver = {
1628 .driver = {
1629 .name = "smb347",
1630 .of_match_table = smb3xx_of_match,
1632 .probe = smb347_probe,
1633 .remove = smb347_remove,
1634 .shutdown = smb347_shutdown,
1635 .id_table = smb347_id,
1637 module_i2c_driver(smb347_driver);
1639 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1640 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1641 MODULE_DESCRIPTION("SMB347 battery charger driver");
1642 MODULE_LICENSE("GPL");