treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / smb347-charger.c
blobc1d124b8be0c2d3fb7b6c6f067a81e30b1fcb456
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/err.h>
12 #include <linux/gpio.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/mutex.h>
19 #include <linux/power_supply.h>
20 #include <linux/power/smb347-charger.h>
21 #include <linux/regmap.h>
24 * Configuration registers. These are mirrored to volatile RAM and can be
25 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
26 * reloaded from non-volatile registers after POR.
28 #define CFG_CHARGE_CURRENT 0x00
29 #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
30 #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
31 #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
32 #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
33 #define CFG_CHARGE_CURRENT_TC_MASK 0x07
34 #define CFG_CURRENT_LIMIT 0x01
35 #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
36 #define CFG_CURRENT_LIMIT_DC_SHIFT 4
37 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
38 #define CFG_FLOAT_VOLTAGE 0x03
39 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
40 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
41 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
42 #define CFG_STAT 0x05
43 #define CFG_STAT_DISABLED BIT(5)
44 #define CFG_STAT_ACTIVE_HIGH BIT(7)
45 #define CFG_PIN 0x06
46 #define CFG_PIN_EN_CTRL_MASK 0x60
47 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
48 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
49 #define CFG_PIN_EN_APSD_IRQ BIT(1)
50 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
51 #define CFG_THERM 0x07
52 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
53 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
54 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
55 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
56 #define CFG_THERM_MONITOR_DISABLED BIT(4)
57 #define CFG_SYSOK 0x08
58 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
59 #define CFG_OTHER 0x09
60 #define CFG_OTHER_RID_MASK 0xc0
61 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
62 #define CFG_OTG 0x0a
63 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
64 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
65 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
66 #define CFG_OTG_CC_COMPENSATION_SHIFT 6
67 #define CFG_TEMP_LIMIT 0x0b
68 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
69 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
70 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
71 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
72 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
73 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
74 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
75 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
76 #define CFG_FAULT_IRQ 0x0c
77 #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
78 #define CFG_STATUS_IRQ 0x0d
79 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
80 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7)
81 #define CFG_ADDRESS 0x0e
83 /* Command registers */
84 #define CMD_A 0x30
85 #define CMD_A_CHG_ENABLED BIT(1)
86 #define CMD_A_SUSPEND_ENABLED BIT(2)
87 #define CMD_A_ALLOW_WRITE BIT(7)
88 #define CMD_B 0x31
89 #define CMD_C 0x33
91 /* Interrupt Status registers */
92 #define IRQSTAT_A 0x35
93 #define IRQSTAT_C 0x37
94 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
95 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
96 #define IRQSTAT_C_TAPER_IRQ BIT(3)
97 #define IRQSTAT_D 0x38
98 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2)
99 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3)
100 #define IRQSTAT_E 0x39
101 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
102 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
103 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
104 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
105 #define IRQSTAT_F 0x3a
107 /* Status registers */
108 #define STAT_A 0x3b
109 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
110 #define STAT_B 0x3c
111 #define STAT_C 0x3d
112 #define STAT_C_CHG_ENABLED BIT(0)
113 #define STAT_C_HOLDOFF_STAT BIT(3)
114 #define STAT_C_CHG_MASK 0x06
115 #define STAT_C_CHG_SHIFT 1
116 #define STAT_C_CHG_TERM BIT(5)
117 #define STAT_C_CHARGER_ERROR BIT(6)
118 #define STAT_E 0x3f
120 #define SMB347_MAX_REGISTER 0x3f
123 * struct smb347_charger - smb347 charger instance
124 * @lock: protects concurrent access to online variables
125 * @dev: pointer to device
126 * @regmap: pointer to driver regmap
127 * @mains: power_supply instance for AC/DC power
128 * @usb: power_supply instance for USB power
129 * @battery: power_supply instance for battery
130 * @mains_online: is AC/DC input connected
131 * @usb_online: is USB input connected
132 * @charging_enabled: is charging enabled
133 * @pdata: pointer to platform data
135 struct smb347_charger {
136 struct mutex lock;
137 struct device *dev;
138 struct regmap *regmap;
139 struct power_supply *mains;
140 struct power_supply *usb;
141 struct power_supply *battery;
142 bool mains_online;
143 bool usb_online;
144 bool charging_enabled;
145 const struct smb347_charger_platform_data *pdata;
148 /* Fast charge current in uA */
149 static const unsigned int fcc_tbl[] = {
150 700000,
151 900000,
152 1200000,
153 1500000,
154 1800000,
155 2000000,
156 2200000,
157 2500000,
160 /* Pre-charge current in uA */
161 static const unsigned int pcc_tbl[] = {
162 100000,
163 150000,
164 200000,
165 250000,
168 /* Termination current in uA */
169 static const unsigned int tc_tbl[] = {
170 37500,
171 50000,
172 100000,
173 150000,
174 200000,
175 250000,
176 500000,
177 600000,
180 /* Input current limit in uA */
181 static const unsigned int icl_tbl[] = {
182 300000,
183 500000,
184 700000,
185 900000,
186 1200000,
187 1500000,
188 1800000,
189 2000000,
190 2200000,
191 2500000,
194 /* Charge current compensation in uA */
195 static const unsigned int ccc_tbl[] = {
196 250000,
197 700000,
198 900000,
199 1200000,
202 /* Convert register value to current using lookup table */
203 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
205 if (val >= size)
206 return -EINVAL;
207 return tbl[val];
210 /* Convert current to register value using lookup table */
211 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
213 size_t i;
215 for (i = 0; i < size; i++)
216 if (val < tbl[i])
217 break;
218 return i > 0 ? i - 1 : -EINVAL;
222 * smb347_update_ps_status - refreshes the power source status
223 * @smb: pointer to smb347 charger instance
225 * Function checks whether any power source is connected to the charger and
226 * updates internal state accordingly. If there is a change to previous state
227 * function returns %1, otherwise %0 and negative errno in case of errror.
229 static int smb347_update_ps_status(struct smb347_charger *smb)
231 bool usb = false;
232 bool dc = false;
233 unsigned int val;
234 int ret;
236 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
237 if (ret < 0)
238 return ret;
241 * Dc and usb are set depending on whether they are enabled in
242 * platform data _and_ whether corresponding undervoltage is set.
244 if (smb->pdata->use_mains)
245 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
246 if (smb->pdata->use_usb)
247 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
249 mutex_lock(&smb->lock);
250 ret = smb->mains_online != dc || smb->usb_online != usb;
251 smb->mains_online = dc;
252 smb->usb_online = usb;
253 mutex_unlock(&smb->lock);
255 return ret;
259 * smb347_is_ps_online - returns whether input power source is connected
260 * @smb: pointer to smb347 charger instance
262 * Returns %true if input power source is connected. Note that this is
263 * dependent on what platform has configured for usable power sources. For
264 * example if USB is disabled, this will return %false even if the USB cable
265 * is connected.
267 static bool smb347_is_ps_online(struct smb347_charger *smb)
269 bool ret;
271 mutex_lock(&smb->lock);
272 ret = smb->usb_online || smb->mains_online;
273 mutex_unlock(&smb->lock);
275 return ret;
279 * smb347_charging_status - returns status of charging
280 * @smb: pointer to smb347 charger instance
282 * Function returns charging status. %0 means no charging is in progress,
283 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
285 static int smb347_charging_status(struct smb347_charger *smb)
287 unsigned int val;
288 int ret;
290 if (!smb347_is_ps_online(smb))
291 return 0;
293 ret = regmap_read(smb->regmap, STAT_C, &val);
294 if (ret < 0)
295 return 0;
297 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
300 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
302 int ret = 0;
304 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
305 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
306 return 0;
309 mutex_lock(&smb->lock);
310 if (smb->charging_enabled != enable) {
311 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
312 enable ? CMD_A_CHG_ENABLED : 0);
313 if (!ret)
314 smb->charging_enabled = enable;
316 mutex_unlock(&smb->lock);
317 return ret;
320 static inline int smb347_charging_enable(struct smb347_charger *smb)
322 return smb347_charging_set(smb, true);
325 static inline int smb347_charging_disable(struct smb347_charger *smb)
327 return smb347_charging_set(smb, false);
330 static int smb347_start_stop_charging(struct smb347_charger *smb)
332 int ret;
335 * Depending on whether valid power source is connected or not, we
336 * disable or enable the charging. We do it manually because it
337 * depends on how the platform has configured the valid inputs.
339 if (smb347_is_ps_online(smb)) {
340 ret = smb347_charging_enable(smb);
341 if (ret < 0)
342 dev_err(smb->dev, "failed to enable charging\n");
343 } else {
344 ret = smb347_charging_disable(smb);
345 if (ret < 0)
346 dev_err(smb->dev, "failed to disable charging\n");
349 return ret;
352 static int smb347_set_charge_current(struct smb347_charger *smb)
354 int ret;
356 if (smb->pdata->max_charge_current) {
357 ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
358 smb->pdata->max_charge_current);
359 if (ret < 0)
360 return ret;
362 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
363 CFG_CHARGE_CURRENT_FCC_MASK,
364 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
365 if (ret < 0)
366 return ret;
369 if (smb->pdata->pre_charge_current) {
370 ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
371 smb->pdata->pre_charge_current);
372 if (ret < 0)
373 return ret;
375 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
376 CFG_CHARGE_CURRENT_PCC_MASK,
377 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
378 if (ret < 0)
379 return ret;
382 if (smb->pdata->termination_current) {
383 ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
384 smb->pdata->termination_current);
385 if (ret < 0)
386 return ret;
388 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
389 CFG_CHARGE_CURRENT_TC_MASK, ret);
390 if (ret < 0)
391 return ret;
394 return 0;
397 static int smb347_set_current_limits(struct smb347_charger *smb)
399 int ret;
401 if (smb->pdata->mains_current_limit) {
402 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
403 smb->pdata->mains_current_limit);
404 if (ret < 0)
405 return ret;
407 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
408 CFG_CURRENT_LIMIT_DC_MASK,
409 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
410 if (ret < 0)
411 return ret;
414 if (smb->pdata->usb_hc_current_limit) {
415 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
416 smb->pdata->usb_hc_current_limit);
417 if (ret < 0)
418 return ret;
420 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
421 CFG_CURRENT_LIMIT_USB_MASK, ret);
422 if (ret < 0)
423 return ret;
426 return 0;
429 static int smb347_set_voltage_limits(struct smb347_charger *smb)
431 int ret;
433 if (smb->pdata->pre_to_fast_voltage) {
434 ret = smb->pdata->pre_to_fast_voltage;
436 /* uV */
437 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
438 ret /= 200000;
440 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
441 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
442 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
443 if (ret < 0)
444 return ret;
447 if (smb->pdata->max_charge_voltage) {
448 ret = smb->pdata->max_charge_voltage;
450 /* uV */
451 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
452 ret /= 20000;
454 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
455 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
456 if (ret < 0)
457 return ret;
460 return 0;
463 static int smb347_set_temp_limits(struct smb347_charger *smb)
465 bool enable_therm_monitor = false;
466 int ret = 0;
467 int val;
469 if (smb->pdata->chip_temp_threshold) {
470 val = smb->pdata->chip_temp_threshold;
472 /* degree C */
473 val = clamp_val(val, 100, 130) - 100;
474 val /= 10;
476 ret = regmap_update_bits(smb->regmap, CFG_OTG,
477 CFG_OTG_TEMP_THRESHOLD_MASK,
478 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
479 if (ret < 0)
480 return ret;
483 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
484 val = smb->pdata->soft_cold_temp_limit;
486 val = clamp_val(val, 0, 15);
487 val /= 5;
488 /* this goes from higher to lower so invert the value */
489 val = ~val & 0x3;
491 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
492 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
493 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
494 if (ret < 0)
495 return ret;
497 enable_therm_monitor = true;
500 if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
501 val = smb->pdata->soft_hot_temp_limit;
503 val = clamp_val(val, 40, 55) - 40;
504 val /= 5;
506 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
507 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
508 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
509 if (ret < 0)
510 return ret;
512 enable_therm_monitor = true;
515 if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
516 val = smb->pdata->hard_cold_temp_limit;
518 val = clamp_val(val, -5, 10) + 5;
519 val /= 5;
520 /* this goes from higher to lower so invert the value */
521 val = ~val & 0x3;
523 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
524 CFG_TEMP_LIMIT_HARD_COLD_MASK,
525 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
526 if (ret < 0)
527 return ret;
529 enable_therm_monitor = true;
532 if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
533 val = smb->pdata->hard_hot_temp_limit;
535 val = clamp_val(val, 50, 65) - 50;
536 val /= 5;
538 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
539 CFG_TEMP_LIMIT_HARD_HOT_MASK,
540 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
541 if (ret < 0)
542 return ret;
544 enable_therm_monitor = true;
548 * If any of the temperature limits are set, we also enable the
549 * thermistor monitoring.
551 * When soft limits are hit, the device will start to compensate
552 * current and/or voltage depending on the configuration.
554 * When hard limit is hit, the device will suspend charging
555 * depending on the configuration.
557 if (enable_therm_monitor) {
558 ret = regmap_update_bits(smb->regmap, CFG_THERM,
559 CFG_THERM_MONITOR_DISABLED, 0);
560 if (ret < 0)
561 return ret;
564 if (smb->pdata->suspend_on_hard_temp_limit) {
565 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
566 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
567 if (ret < 0)
568 return ret;
571 if (smb->pdata->soft_temp_limit_compensation !=
572 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
573 val = smb->pdata->soft_temp_limit_compensation & 0x3;
575 ret = regmap_update_bits(smb->regmap, CFG_THERM,
576 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
577 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
578 if (ret < 0)
579 return ret;
581 ret = regmap_update_bits(smb->regmap, CFG_THERM,
582 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
583 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
584 if (ret < 0)
585 return ret;
588 if (smb->pdata->charge_current_compensation) {
589 val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
590 smb->pdata->charge_current_compensation);
591 if (val < 0)
592 return val;
594 ret = regmap_update_bits(smb->regmap, CFG_OTG,
595 CFG_OTG_CC_COMPENSATION_MASK,
596 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
597 if (ret < 0)
598 return ret;
601 return ret;
605 * smb347_set_writable - enables/disables writing to non-volatile registers
606 * @smb: pointer to smb347 charger instance
608 * You can enable/disable writing to the non-volatile configuration
609 * registers by calling this function.
611 * Returns %0 on success and negative errno in case of failure.
613 static int smb347_set_writable(struct smb347_charger *smb, bool writable)
615 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
616 writable ? CMD_A_ALLOW_WRITE : 0);
619 static int smb347_hw_init(struct smb347_charger *smb)
621 unsigned int val;
622 int ret;
624 ret = smb347_set_writable(smb, true);
625 if (ret < 0)
626 return ret;
629 * Program the platform specific configuration values to the device
630 * first.
632 ret = smb347_set_charge_current(smb);
633 if (ret < 0)
634 goto fail;
636 ret = smb347_set_current_limits(smb);
637 if (ret < 0)
638 goto fail;
640 ret = smb347_set_voltage_limits(smb);
641 if (ret < 0)
642 goto fail;
644 ret = smb347_set_temp_limits(smb);
645 if (ret < 0)
646 goto fail;
648 /* If USB charging is disabled we put the USB in suspend mode */
649 if (!smb->pdata->use_usb) {
650 ret = regmap_update_bits(smb->regmap, CMD_A,
651 CMD_A_SUSPEND_ENABLED,
652 CMD_A_SUSPEND_ENABLED);
653 if (ret < 0)
654 goto fail;
658 * If configured by platform data, we enable hardware Auto-OTG
659 * support for driving VBUS. Otherwise we disable it.
661 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
662 smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
663 if (ret < 0)
664 goto fail;
667 * Make the charging functionality controllable by a write to the
668 * command register unless pin control is specified in the platform
669 * data.
671 switch (smb->pdata->enable_control) {
672 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
673 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
674 break;
675 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
676 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
677 break;
678 default:
679 val = 0;
680 break;
683 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
684 val);
685 if (ret < 0)
686 goto fail;
688 /* Disable Automatic Power Source Detection (APSD) interrupt. */
689 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
690 if (ret < 0)
691 goto fail;
693 ret = smb347_update_ps_status(smb);
694 if (ret < 0)
695 goto fail;
697 ret = smb347_start_stop_charging(smb);
699 fail:
700 smb347_set_writable(smb, false);
701 return ret;
704 static irqreturn_t smb347_interrupt(int irq, void *data)
706 struct smb347_charger *smb = data;
707 unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
708 bool handled = false;
709 int ret;
711 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
712 if (ret < 0) {
713 dev_warn(smb->dev, "reading STAT_C failed\n");
714 return IRQ_NONE;
717 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
718 if (ret < 0) {
719 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
720 return IRQ_NONE;
723 ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
724 if (ret < 0) {
725 dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
726 return IRQ_NONE;
729 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
730 if (ret < 0) {
731 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
732 return IRQ_NONE;
736 * If we get charger error we report the error back to user.
737 * If the error is recovered charging will resume again.
739 if (stat_c & STAT_C_CHARGER_ERROR) {
740 dev_err(smb->dev, "charging stopped due to charger error\n");
741 power_supply_changed(smb->battery);
742 handled = true;
746 * If we reached the termination current the battery is charged and
747 * we can update the status now. Charging is automatically
748 * disabled by the hardware.
750 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
751 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT)
752 power_supply_changed(smb->battery);
753 dev_dbg(smb->dev, "going to HW maintenance mode\n");
754 handled = true;
758 * If we got a charger timeout INT that means the charge
759 * full is not detected with in charge timeout value.
761 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
762 dev_dbg(smb->dev, "total Charge Timeout INT received\n");
764 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
765 dev_warn(smb->dev, "charging stopped due to timeout\n");
766 power_supply_changed(smb->battery);
767 handled = true;
771 * If we got an under voltage interrupt it means that AC/USB input
772 * was connected or disconnected.
774 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
775 if (smb347_update_ps_status(smb) > 0) {
776 smb347_start_stop_charging(smb);
777 if (smb->pdata->use_mains)
778 power_supply_changed(smb->mains);
779 if (smb->pdata->use_usb)
780 power_supply_changed(smb->usb);
782 handled = true;
785 return handled ? IRQ_HANDLED : IRQ_NONE;
788 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
790 int ret;
792 ret = smb347_set_writable(smb, true);
793 if (ret < 0)
794 return ret;
797 * Enable/disable interrupts for:
798 * - under voltage
799 * - termination current reached
800 * - charger timeout
801 * - charger error
803 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
804 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
805 if (ret < 0)
806 goto fail;
808 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
809 enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
810 CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
811 if (ret < 0)
812 goto fail;
814 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
815 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
816 fail:
817 smb347_set_writable(smb, false);
818 return ret;
821 static inline int smb347_irq_enable(struct smb347_charger *smb)
823 return smb347_irq_set(smb, true);
826 static inline int smb347_irq_disable(struct smb347_charger *smb)
828 return smb347_irq_set(smb, false);
831 static int smb347_irq_init(struct smb347_charger *smb,
832 struct i2c_client *client)
834 const struct smb347_charger_platform_data *pdata = smb->pdata;
835 int ret, irq = gpio_to_irq(pdata->irq_gpio);
837 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
838 if (ret < 0)
839 goto fail;
841 ret = request_threaded_irq(irq, NULL, smb347_interrupt,
842 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
843 client->name, smb);
844 if (ret < 0)
845 goto fail_gpio;
847 ret = smb347_set_writable(smb, true);
848 if (ret < 0)
849 goto fail_irq;
852 * Configure the STAT output to be suitable for interrupts: disable
853 * all other output (except interrupts) and make it active low.
855 ret = regmap_update_bits(smb->regmap, CFG_STAT,
856 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
857 CFG_STAT_DISABLED);
858 if (ret < 0)
859 goto fail_readonly;
861 smb347_set_writable(smb, false);
862 client->irq = irq;
863 return 0;
865 fail_readonly:
866 smb347_set_writable(smb, false);
867 fail_irq:
868 free_irq(irq, smb);
869 fail_gpio:
870 gpio_free(pdata->irq_gpio);
871 fail:
872 client->irq = 0;
873 return ret;
877 * Returns the constant charge current programmed
878 * into the charger in uA.
880 static int get_const_charge_current(struct smb347_charger *smb)
882 int ret, intval;
883 unsigned int v;
885 if (!smb347_is_ps_online(smb))
886 return -ENODATA;
888 ret = regmap_read(smb->regmap, STAT_B, &v);
889 if (ret < 0)
890 return ret;
893 * The current value is composition of FCC and PCC values
894 * and we can detect which table to use from bit 5.
896 if (v & 0x20) {
897 intval = hw_to_current(fcc_tbl, ARRAY_SIZE(fcc_tbl), v & 7);
898 } else {
899 v >>= 3;
900 intval = hw_to_current(pcc_tbl, ARRAY_SIZE(pcc_tbl), v & 7);
903 return intval;
907 * Returns the constant charge voltage programmed
908 * into the charger in uV.
910 static int get_const_charge_voltage(struct smb347_charger *smb)
912 int ret, intval;
913 unsigned int v;
915 if (!smb347_is_ps_online(smb))
916 return -ENODATA;
918 ret = regmap_read(smb->regmap, STAT_A, &v);
919 if (ret < 0)
920 return ret;
922 v &= STAT_A_FLOAT_VOLTAGE_MASK;
923 if (v > 0x3d)
924 v = 0x3d;
926 intval = 3500000 + v * 20000;
928 return intval;
931 static int smb347_mains_get_property(struct power_supply *psy,
932 enum power_supply_property prop,
933 union power_supply_propval *val)
935 struct smb347_charger *smb = power_supply_get_drvdata(psy);
936 int ret;
938 switch (prop) {
939 case POWER_SUPPLY_PROP_ONLINE:
940 val->intval = smb->mains_online;
941 break;
943 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
944 ret = get_const_charge_voltage(smb);
945 if (ret < 0)
946 return ret;
947 else
948 val->intval = ret;
949 break;
951 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
952 ret = get_const_charge_current(smb);
953 if (ret < 0)
954 return ret;
955 else
956 val->intval = ret;
957 break;
959 default:
960 return -EINVAL;
963 return 0;
966 static enum power_supply_property smb347_mains_properties[] = {
967 POWER_SUPPLY_PROP_ONLINE,
968 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
969 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
972 static int smb347_usb_get_property(struct power_supply *psy,
973 enum power_supply_property prop,
974 union power_supply_propval *val)
976 struct smb347_charger *smb = power_supply_get_drvdata(psy);
977 int ret;
979 switch (prop) {
980 case POWER_SUPPLY_PROP_ONLINE:
981 val->intval = smb->usb_online;
982 break;
984 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
985 ret = get_const_charge_voltage(smb);
986 if (ret < 0)
987 return ret;
988 else
989 val->intval = ret;
990 break;
992 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
993 ret = get_const_charge_current(smb);
994 if (ret < 0)
995 return ret;
996 else
997 val->intval = ret;
998 break;
1000 default:
1001 return -EINVAL;
1004 return 0;
1007 static enum power_supply_property smb347_usb_properties[] = {
1008 POWER_SUPPLY_PROP_ONLINE,
1009 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1010 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1013 static int smb347_get_charging_status(struct smb347_charger *smb)
1015 int ret, status;
1016 unsigned int val;
1018 if (!smb347_is_ps_online(smb))
1019 return POWER_SUPPLY_STATUS_DISCHARGING;
1021 ret = regmap_read(smb->regmap, STAT_C, &val);
1022 if (ret < 0)
1023 return ret;
1025 if ((val & STAT_C_CHARGER_ERROR) ||
1026 (val & STAT_C_HOLDOFF_STAT)) {
1028 * set to NOT CHARGING upon charger error
1029 * or charging has stopped.
1031 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1032 } else {
1033 if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1035 * set to charging if battery is in pre-charge,
1036 * fast charge or taper charging mode.
1038 status = POWER_SUPPLY_STATUS_CHARGING;
1039 } else if (val & STAT_C_CHG_TERM) {
1041 * set the status to FULL if battery is not in pre
1042 * charge, fast charge or taper charging mode AND
1043 * charging is terminated at least once.
1045 status = POWER_SUPPLY_STATUS_FULL;
1046 } else {
1048 * in this case no charger error or termination
1049 * occured but charging is not in progress!!!
1051 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1055 return status;
1058 static int smb347_battery_get_property(struct power_supply *psy,
1059 enum power_supply_property prop,
1060 union power_supply_propval *val)
1062 struct smb347_charger *smb = power_supply_get_drvdata(psy);
1063 const struct smb347_charger_platform_data *pdata = smb->pdata;
1064 int ret;
1066 ret = smb347_update_ps_status(smb);
1067 if (ret < 0)
1068 return ret;
1070 switch (prop) {
1071 case POWER_SUPPLY_PROP_STATUS:
1072 ret = smb347_get_charging_status(smb);
1073 if (ret < 0)
1074 return ret;
1075 val->intval = ret;
1076 break;
1078 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1079 if (!smb347_is_ps_online(smb))
1080 return -ENODATA;
1083 * We handle trickle and pre-charging the same, and taper
1084 * and none the same.
1086 switch (smb347_charging_status(smb)) {
1087 case 1:
1088 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1089 break;
1090 case 2:
1091 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1092 break;
1093 default:
1094 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1095 break;
1097 break;
1099 case POWER_SUPPLY_PROP_TECHNOLOGY:
1100 val->intval = pdata->battery_info.technology;
1101 break;
1103 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1104 val->intval = pdata->battery_info.voltage_min_design;
1105 break;
1107 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1108 val->intval = pdata->battery_info.voltage_max_design;
1109 break;
1111 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1112 val->intval = pdata->battery_info.charge_full_design;
1113 break;
1115 case POWER_SUPPLY_PROP_MODEL_NAME:
1116 val->strval = pdata->battery_info.name;
1117 break;
1119 default:
1120 return -EINVAL;
1123 return 0;
1126 static enum power_supply_property smb347_battery_properties[] = {
1127 POWER_SUPPLY_PROP_STATUS,
1128 POWER_SUPPLY_PROP_CHARGE_TYPE,
1129 POWER_SUPPLY_PROP_TECHNOLOGY,
1130 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1131 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1132 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
1133 POWER_SUPPLY_PROP_MODEL_NAME,
1136 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1138 switch (reg) {
1139 case IRQSTAT_A:
1140 case IRQSTAT_C:
1141 case IRQSTAT_E:
1142 case IRQSTAT_F:
1143 case STAT_A:
1144 case STAT_B:
1145 case STAT_C:
1146 case STAT_E:
1147 return true;
1150 return false;
1153 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1155 switch (reg) {
1156 case CFG_CHARGE_CURRENT:
1157 case CFG_CURRENT_LIMIT:
1158 case CFG_FLOAT_VOLTAGE:
1159 case CFG_STAT:
1160 case CFG_PIN:
1161 case CFG_THERM:
1162 case CFG_SYSOK:
1163 case CFG_OTHER:
1164 case CFG_OTG:
1165 case CFG_TEMP_LIMIT:
1166 case CFG_FAULT_IRQ:
1167 case CFG_STATUS_IRQ:
1168 case CFG_ADDRESS:
1169 case CMD_A:
1170 case CMD_B:
1171 case CMD_C:
1172 return true;
1175 return smb347_volatile_reg(dev, reg);
1178 static const struct regmap_config smb347_regmap = {
1179 .reg_bits = 8,
1180 .val_bits = 8,
1181 .max_register = SMB347_MAX_REGISTER,
1182 .volatile_reg = smb347_volatile_reg,
1183 .readable_reg = smb347_readable_reg,
1186 static const struct power_supply_desc smb347_mains_desc = {
1187 .name = "smb347-mains",
1188 .type = POWER_SUPPLY_TYPE_MAINS,
1189 .get_property = smb347_mains_get_property,
1190 .properties = smb347_mains_properties,
1191 .num_properties = ARRAY_SIZE(smb347_mains_properties),
1194 static const struct power_supply_desc smb347_usb_desc = {
1195 .name = "smb347-usb",
1196 .type = POWER_SUPPLY_TYPE_USB,
1197 .get_property = smb347_usb_get_property,
1198 .properties = smb347_usb_properties,
1199 .num_properties = ARRAY_SIZE(smb347_usb_properties),
1202 static const struct power_supply_desc smb347_battery_desc = {
1203 .name = "smb347-battery",
1204 .type = POWER_SUPPLY_TYPE_BATTERY,
1205 .get_property = smb347_battery_get_property,
1206 .properties = smb347_battery_properties,
1207 .num_properties = ARRAY_SIZE(smb347_battery_properties),
1210 static int smb347_probe(struct i2c_client *client,
1211 const struct i2c_device_id *id)
1213 static char *battery[] = { "smb347-battery" };
1214 const struct smb347_charger_platform_data *pdata;
1215 struct power_supply_config mains_usb_cfg = {}, battery_cfg = {};
1216 struct device *dev = &client->dev;
1217 struct smb347_charger *smb;
1218 int ret;
1220 pdata = dev->platform_data;
1221 if (!pdata)
1222 return -EINVAL;
1224 if (!pdata->use_mains && !pdata->use_usb)
1225 return -EINVAL;
1227 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1228 if (!smb)
1229 return -ENOMEM;
1231 i2c_set_clientdata(client, smb);
1233 mutex_init(&smb->lock);
1234 smb->dev = &client->dev;
1235 smb->pdata = pdata;
1237 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1238 if (IS_ERR(smb->regmap))
1239 return PTR_ERR(smb->regmap);
1241 ret = smb347_hw_init(smb);
1242 if (ret < 0)
1243 return ret;
1245 mains_usb_cfg.supplied_to = battery;
1246 mains_usb_cfg.num_supplicants = ARRAY_SIZE(battery);
1247 mains_usb_cfg.drv_data = smb;
1248 if (smb->pdata->use_mains) {
1249 smb->mains = power_supply_register(dev, &smb347_mains_desc,
1250 &mains_usb_cfg);
1251 if (IS_ERR(smb->mains))
1252 return PTR_ERR(smb->mains);
1255 if (smb->pdata->use_usb) {
1256 smb->usb = power_supply_register(dev, &smb347_usb_desc,
1257 &mains_usb_cfg);
1258 if (IS_ERR(smb->usb)) {
1259 if (smb->pdata->use_mains)
1260 power_supply_unregister(smb->mains);
1261 return PTR_ERR(smb->usb);
1265 battery_cfg.drv_data = smb;
1266 smb->battery = power_supply_register(dev, &smb347_battery_desc,
1267 &battery_cfg);
1268 if (IS_ERR(smb->battery)) {
1269 if (smb->pdata->use_usb)
1270 power_supply_unregister(smb->usb);
1271 if (smb->pdata->use_mains)
1272 power_supply_unregister(smb->mains);
1273 return PTR_ERR(smb->battery);
1277 * Interrupt pin is optional. If it is connected, we setup the
1278 * interrupt support here.
1280 if (pdata->irq_gpio >= 0) {
1281 ret = smb347_irq_init(smb, client);
1282 if (ret < 0) {
1283 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1284 dev_warn(dev, "disabling IRQ support\n");
1285 } else {
1286 smb347_irq_enable(smb);
1290 return 0;
1293 static int smb347_remove(struct i2c_client *client)
1295 struct smb347_charger *smb = i2c_get_clientdata(client);
1297 if (client->irq) {
1298 smb347_irq_disable(smb);
1299 free_irq(client->irq, smb);
1300 gpio_free(smb->pdata->irq_gpio);
1303 power_supply_unregister(smb->battery);
1304 if (smb->pdata->use_usb)
1305 power_supply_unregister(smb->usb);
1306 if (smb->pdata->use_mains)
1307 power_supply_unregister(smb->mains);
1308 return 0;
1311 static const struct i2c_device_id smb347_id[] = {
1312 { "smb347", 0 },
1315 MODULE_DEVICE_TABLE(i2c, smb347_id);
1317 static struct i2c_driver smb347_driver = {
1318 .driver = {
1319 .name = "smb347",
1321 .probe = smb347_probe,
1322 .remove = smb347_remove,
1323 .id_table = smb347_id,
1326 module_i2c_driver(smb347_driver);
1328 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1329 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1330 MODULE_DESCRIPTION("SMB347 battery charger driver");
1331 MODULE_LICENSE("GPL");