2 * Summit Microelectronics SMB347 Battery Charger Driver
4 * Copyright (C) 2011, Intel Corporation
6 * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/gpio.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/mutex.h>
21 #include <linux/power_supply.h>
22 #include <linux/power/smb347-charger.h>
23 #include <linux/regmap.h>
26 * Configuration registers. These are mirrored to volatile RAM and can be
27 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
28 * reloaded from non-volatile registers after POR.
30 #define CFG_CHARGE_CURRENT 0x00
31 #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
32 #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
33 #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
34 #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
35 #define CFG_CHARGE_CURRENT_TC_MASK 0x07
36 #define CFG_CURRENT_LIMIT 0x01
37 #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
38 #define CFG_CURRENT_LIMIT_DC_SHIFT 4
39 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
40 #define CFG_FLOAT_VOLTAGE 0x03
41 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
42 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
43 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
45 #define CFG_STAT_DISABLED BIT(5)
46 #define CFG_STAT_ACTIVE_HIGH BIT(7)
48 #define CFG_PIN_EN_CTRL_MASK 0x60
49 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
50 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
51 #define CFG_PIN_EN_APSD_IRQ BIT(1)
52 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
53 #define CFG_THERM 0x07
54 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
55 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
56 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
57 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
58 #define CFG_THERM_MONITOR_DISABLED BIT(4)
59 #define CFG_SYSOK 0x08
60 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
61 #define CFG_OTHER 0x09
62 #define CFG_OTHER_RID_MASK 0xc0
63 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
65 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
66 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
67 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
68 #define CFG_OTG_CC_COMPENSATION_SHIFT 6
69 #define CFG_TEMP_LIMIT 0x0b
70 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
71 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
72 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
73 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
74 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
75 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
76 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
77 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
78 #define CFG_FAULT_IRQ 0x0c
79 #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
80 #define CFG_STATUS_IRQ 0x0d
81 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
82 #define CFG_ADDRESS 0x0e
84 /* Command registers */
86 #define CMD_A_CHG_ENABLED BIT(1)
87 #define CMD_A_SUSPEND_ENABLED BIT(2)
88 #define CMD_A_ALLOW_WRITE BIT(7)
92 /* Interrupt Status registers */
93 #define IRQSTAT_A 0x35
94 #define IRQSTAT_C 0x37
95 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
96 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
97 #define IRQSTAT_C_TAPER_IRQ BIT(3)
98 #define IRQSTAT_E 0x39
99 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
100 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
101 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
102 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
103 #define IRQSTAT_F 0x3a
105 /* Status registers */
107 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
110 #define STAT_C_CHG_ENABLED BIT(0)
111 #define STAT_C_CHG_MASK 0x06
112 #define STAT_C_CHG_SHIFT 1
113 #define STAT_C_CHARGER_ERROR BIT(6)
116 #define SMB347_MAX_REGISTER 0x3f
119 * struct smb347_charger - smb347 charger instance
120 * @lock: protects concurrent access to online variables
121 * @dev: pointer to device
122 * @regmap: pointer to driver regmap
123 * @mains: power_supply instance for AC/DC power
124 * @usb: power_supply instance for USB power
125 * @battery: power_supply instance for battery
126 * @mains_online: is AC/DC input connected
127 * @usb_online: is USB input connected
128 * @charging_enabled: is charging enabled
129 * @pdata: pointer to platform data
131 struct smb347_charger
{
134 struct regmap
*regmap
;
135 struct power_supply mains
;
136 struct power_supply usb
;
137 struct power_supply battery
;
140 bool charging_enabled
;
141 const struct smb347_charger_platform_data
*pdata
;
144 /* Fast charge current in uA */
145 static const unsigned int fcc_tbl
[] = {
156 /* Pre-charge current in uA */
157 static const unsigned int pcc_tbl
[] = {
164 /* Termination current in uA */
165 static const unsigned int tc_tbl
[] = {
176 /* Input current limit in uA */
177 static const unsigned int icl_tbl
[] = {
190 /* Charge current compensation in uA */
191 static const unsigned int ccc_tbl
[] = {
198 /* Convert current to register value using lookup table */
199 static int current_to_hw(const unsigned int *tbl
, size_t size
, unsigned int val
)
203 for (i
= 0; i
< size
; i
++)
206 return i
> 0 ? i
- 1 : -EINVAL
;
210 * smb347_update_ps_status - refreshes the power source status
211 * @smb: pointer to smb347 charger instance
213 * Function checks whether any power source is connected to the charger and
214 * updates internal state accordingly. If there is a change to previous state
215 * function returns %1, otherwise %0 and negative errno in case of errror.
217 static int smb347_update_ps_status(struct smb347_charger
*smb
)
224 ret
= regmap_read(smb
->regmap
, IRQSTAT_E
, &val
);
229 * Dc and usb are set depending on whether they are enabled in
230 * platform data _and_ whether corresponding undervoltage is set.
232 if (smb
->pdata
->use_mains
)
233 dc
= !(val
& IRQSTAT_E_DCIN_UV_STAT
);
234 if (smb
->pdata
->use_usb
)
235 usb
= !(val
& IRQSTAT_E_USBIN_UV_STAT
);
237 mutex_lock(&smb
->lock
);
238 ret
= smb
->mains_online
!= dc
|| smb
->usb_online
!= usb
;
239 smb
->mains_online
= dc
;
240 smb
->usb_online
= usb
;
241 mutex_unlock(&smb
->lock
);
247 * smb347_is_ps_online - returns whether input power source is connected
248 * @smb: pointer to smb347 charger instance
250 * Returns %true if input power source is connected. Note that this is
251 * dependent on what platform has configured for usable power sources. For
252 * example if USB is disabled, this will return %false even if the USB cable
255 static bool smb347_is_ps_online(struct smb347_charger
*smb
)
259 mutex_lock(&smb
->lock
);
260 ret
= smb
->usb_online
|| smb
->mains_online
;
261 mutex_unlock(&smb
->lock
);
267 * smb347_charging_status - returns status of charging
268 * @smb: pointer to smb347 charger instance
270 * Function returns charging status. %0 means no charging is in progress,
271 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
273 static int smb347_charging_status(struct smb347_charger
*smb
)
278 if (!smb347_is_ps_online(smb
))
281 ret
= regmap_read(smb
->regmap
, STAT_C
, &val
);
285 return (val
& STAT_C_CHG_MASK
) >> STAT_C_CHG_SHIFT
;
288 static int smb347_charging_set(struct smb347_charger
*smb
, bool enable
)
292 if (smb
->pdata
->enable_control
!= SMB347_CHG_ENABLE_SW
) {
293 dev_dbg(smb
->dev
, "charging enable/disable in SW disabled\n");
297 mutex_lock(&smb
->lock
);
298 if (smb
->charging_enabled
!= enable
) {
299 ret
= regmap_update_bits(smb
->regmap
, CMD_A
, CMD_A_CHG_ENABLED
,
300 enable
? CMD_A_CHG_ENABLED
: 0);
302 smb
->charging_enabled
= enable
;
304 mutex_unlock(&smb
->lock
);
308 static inline int smb347_charging_enable(struct smb347_charger
*smb
)
310 return smb347_charging_set(smb
, true);
313 static inline int smb347_charging_disable(struct smb347_charger
*smb
)
315 return smb347_charging_set(smb
, false);
318 static int smb347_start_stop_charging(struct smb347_charger
*smb
)
323 * Depending on whether valid power source is connected or not, we
324 * disable or enable the charging. We do it manually because it
325 * depends on how the platform has configured the valid inputs.
327 if (smb347_is_ps_online(smb
)) {
328 ret
= smb347_charging_enable(smb
);
330 dev_err(smb
->dev
, "failed to enable charging\n");
332 ret
= smb347_charging_disable(smb
);
334 dev_err(smb
->dev
, "failed to disable charging\n");
340 static int smb347_set_charge_current(struct smb347_charger
*smb
)
344 if (smb
->pdata
->max_charge_current
) {
345 ret
= current_to_hw(fcc_tbl
, ARRAY_SIZE(fcc_tbl
),
346 smb
->pdata
->max_charge_current
);
350 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
351 CFG_CHARGE_CURRENT_FCC_MASK
,
352 ret
<< CFG_CHARGE_CURRENT_FCC_SHIFT
);
357 if (smb
->pdata
->pre_charge_current
) {
358 ret
= current_to_hw(pcc_tbl
, ARRAY_SIZE(pcc_tbl
),
359 smb
->pdata
->pre_charge_current
);
363 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
364 CFG_CHARGE_CURRENT_PCC_MASK
,
365 ret
<< CFG_CHARGE_CURRENT_PCC_SHIFT
);
370 if (smb
->pdata
->termination_current
) {
371 ret
= current_to_hw(tc_tbl
, ARRAY_SIZE(tc_tbl
),
372 smb
->pdata
->termination_current
);
376 ret
= regmap_update_bits(smb
->regmap
, CFG_CHARGE_CURRENT
,
377 CFG_CHARGE_CURRENT_TC_MASK
, ret
);
385 static int smb347_set_current_limits(struct smb347_charger
*smb
)
389 if (smb
->pdata
->mains_current_limit
) {
390 ret
= current_to_hw(icl_tbl
, ARRAY_SIZE(icl_tbl
),
391 smb
->pdata
->mains_current_limit
);
395 ret
= regmap_update_bits(smb
->regmap
, CFG_CURRENT_LIMIT
,
396 CFG_CURRENT_LIMIT_DC_MASK
,
397 ret
<< CFG_CURRENT_LIMIT_DC_SHIFT
);
402 if (smb
->pdata
->usb_hc_current_limit
) {
403 ret
= current_to_hw(icl_tbl
, ARRAY_SIZE(icl_tbl
),
404 smb
->pdata
->usb_hc_current_limit
);
408 ret
= regmap_update_bits(smb
->regmap
, CFG_CURRENT_LIMIT
,
409 CFG_CURRENT_LIMIT_USB_MASK
, ret
);
417 static int smb347_set_voltage_limits(struct smb347_charger
*smb
)
421 if (smb
->pdata
->pre_to_fast_voltage
) {
422 ret
= smb
->pdata
->pre_to_fast_voltage
;
425 ret
= clamp_val(ret
, 2400000, 3000000) - 2400000;
428 ret
= regmap_update_bits(smb
->regmap
, CFG_FLOAT_VOLTAGE
,
429 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK
,
430 ret
<< CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT
);
435 if (smb
->pdata
->max_charge_voltage
) {
436 ret
= smb
->pdata
->max_charge_voltage
;
439 ret
= clamp_val(ret
, 3500000, 4500000) - 3500000;
442 ret
= regmap_update_bits(smb
->regmap
, CFG_FLOAT_VOLTAGE
,
443 CFG_FLOAT_VOLTAGE_FLOAT_MASK
, ret
);
451 static int smb347_set_temp_limits(struct smb347_charger
*smb
)
453 bool enable_therm_monitor
= false;
457 if (smb
->pdata
->chip_temp_threshold
) {
458 val
= smb
->pdata
->chip_temp_threshold
;
461 val
= clamp_val(val
, 100, 130) - 100;
464 ret
= regmap_update_bits(smb
->regmap
, CFG_OTG
,
465 CFG_OTG_TEMP_THRESHOLD_MASK
,
466 val
<< CFG_OTG_TEMP_THRESHOLD_SHIFT
);
471 if (smb
->pdata
->soft_cold_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
472 val
= smb
->pdata
->soft_cold_temp_limit
;
474 val
= clamp_val(val
, 0, 15);
476 /* this goes from higher to lower so invert the value */
479 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
480 CFG_TEMP_LIMIT_SOFT_COLD_MASK
,
481 val
<< CFG_TEMP_LIMIT_SOFT_COLD_SHIFT
);
485 enable_therm_monitor
= true;
488 if (smb
->pdata
->soft_hot_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
489 val
= smb
->pdata
->soft_hot_temp_limit
;
491 val
= clamp_val(val
, 40, 55) - 40;
494 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
495 CFG_TEMP_LIMIT_SOFT_HOT_MASK
,
496 val
<< CFG_TEMP_LIMIT_SOFT_HOT_SHIFT
);
500 enable_therm_monitor
= true;
503 if (smb
->pdata
->hard_cold_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
504 val
= smb
->pdata
->hard_cold_temp_limit
;
506 val
= clamp_val(val
, -5, 10) + 5;
508 /* this goes from higher to lower so invert the value */
511 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
512 CFG_TEMP_LIMIT_HARD_COLD_MASK
,
513 val
<< CFG_TEMP_LIMIT_HARD_COLD_SHIFT
);
517 enable_therm_monitor
= true;
520 if (smb
->pdata
->hard_hot_temp_limit
!= SMB347_TEMP_USE_DEFAULT
) {
521 val
= smb
->pdata
->hard_hot_temp_limit
;
523 val
= clamp_val(val
, 50, 65) - 50;
526 ret
= regmap_update_bits(smb
->regmap
, CFG_TEMP_LIMIT
,
527 CFG_TEMP_LIMIT_HARD_HOT_MASK
,
528 val
<< CFG_TEMP_LIMIT_HARD_HOT_SHIFT
);
532 enable_therm_monitor
= true;
536 * If any of the temperature limits are set, we also enable the
537 * thermistor monitoring.
539 * When soft limits are hit, the device will start to compensate
540 * current and/or voltage depending on the configuration.
542 * When hard limit is hit, the device will suspend charging
543 * depending on the configuration.
545 if (enable_therm_monitor
) {
546 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
547 CFG_THERM_MONITOR_DISABLED
, 0);
552 if (smb
->pdata
->suspend_on_hard_temp_limit
) {
553 ret
= regmap_update_bits(smb
->regmap
, CFG_SYSOK
,
554 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED
, 0);
559 if (smb
->pdata
->soft_temp_limit_compensation
!=
560 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT
) {
561 val
= smb
->pdata
->soft_temp_limit_compensation
& 0x3;
563 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
564 CFG_THERM_SOFT_HOT_COMPENSATION_MASK
,
565 val
<< CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT
);
569 ret
= regmap_update_bits(smb
->regmap
, CFG_THERM
,
570 CFG_THERM_SOFT_COLD_COMPENSATION_MASK
,
571 val
<< CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT
);
576 if (smb
->pdata
->charge_current_compensation
) {
577 val
= current_to_hw(ccc_tbl
, ARRAY_SIZE(ccc_tbl
),
578 smb
->pdata
->charge_current_compensation
);
582 ret
= regmap_update_bits(smb
->regmap
, CFG_OTG
,
583 CFG_OTG_CC_COMPENSATION_MASK
,
584 (val
& 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT
);
593 * smb347_set_writable - enables/disables writing to non-volatile registers
594 * @smb: pointer to smb347 charger instance
596 * You can enable/disable writing to the non-volatile configuration
597 * registers by calling this function.
599 * Returns %0 on success and negative errno in case of failure.
601 static int smb347_set_writable(struct smb347_charger
*smb
, bool writable
)
603 return regmap_update_bits(smb
->regmap
, CMD_A
, CMD_A_ALLOW_WRITE
,
604 writable
? CMD_A_ALLOW_WRITE
: 0);
607 static int smb347_hw_init(struct smb347_charger
*smb
)
612 ret
= smb347_set_writable(smb
, true);
617 * Program the platform specific configuration values to the device
620 ret
= smb347_set_charge_current(smb
);
624 ret
= smb347_set_current_limits(smb
);
628 ret
= smb347_set_voltage_limits(smb
);
632 ret
= smb347_set_temp_limits(smb
);
636 /* If USB charging is disabled we put the USB in suspend mode */
637 if (!smb
->pdata
->use_usb
) {
638 ret
= regmap_update_bits(smb
->regmap
, CMD_A
,
639 CMD_A_SUSPEND_ENABLED
,
640 CMD_A_SUSPEND_ENABLED
);
646 * If configured by platform data, we enable hardware Auto-OTG
647 * support for driving VBUS. Otherwise we disable it.
649 ret
= regmap_update_bits(smb
->regmap
, CFG_OTHER
, CFG_OTHER_RID_MASK
,
650 smb
->pdata
->use_usb_otg
? CFG_OTHER_RID_ENABLED_AUTO_OTG
: 0);
655 * Make the charging functionality controllable by a write to the
656 * command register unless pin control is specified in the platform
659 switch (smb
->pdata
->enable_control
) {
660 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW
:
661 val
= CFG_PIN_EN_CTRL_ACTIVE_LOW
;
663 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH
:
664 val
= CFG_PIN_EN_CTRL_ACTIVE_HIGH
;
671 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_CTRL_MASK
,
676 /* Disable Automatic Power Source Detection (APSD) interrupt. */
677 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_APSD_IRQ
, 0);
681 ret
= smb347_update_ps_status(smb
);
685 ret
= smb347_start_stop_charging(smb
);
688 smb347_set_writable(smb
, false);
692 static irqreturn_t
smb347_interrupt(int irq
, void *data
)
694 struct smb347_charger
*smb
= data
;
695 unsigned int stat_c
, irqstat_e
, irqstat_c
;
696 bool handled
= false;
699 ret
= regmap_read(smb
->regmap
, STAT_C
, &stat_c
);
701 dev_warn(smb
->dev
, "reading STAT_C failed\n");
705 ret
= regmap_read(smb
->regmap
, IRQSTAT_C
, &irqstat_c
);
707 dev_warn(smb
->dev
, "reading IRQSTAT_C failed\n");
711 ret
= regmap_read(smb
->regmap
, IRQSTAT_E
, &irqstat_e
);
713 dev_warn(smb
->dev
, "reading IRQSTAT_E failed\n");
718 * If we get charger error we report the error back to user and
721 if (stat_c
& STAT_C_CHARGER_ERROR
) {
722 dev_err(smb
->dev
, "error in charger, disabling charging\n");
724 smb347_charging_disable(smb
);
725 power_supply_changed(&smb
->battery
);
730 * If we reached the termination current the battery is charged and
731 * we can update the status now. Charging is automatically
732 * disabled by the hardware.
734 if (irqstat_c
& (IRQSTAT_C_TERMINATION_IRQ
| IRQSTAT_C_TAPER_IRQ
)) {
735 if (irqstat_c
& IRQSTAT_C_TERMINATION_STAT
)
736 power_supply_changed(&smb
->battery
);
741 * If we got an under voltage interrupt it means that AC/USB input
742 * was connected or disconnected.
744 if (irqstat_e
& (IRQSTAT_E_USBIN_UV_IRQ
| IRQSTAT_E_DCIN_UV_IRQ
)) {
745 if (smb347_update_ps_status(smb
) > 0) {
746 smb347_start_stop_charging(smb
);
747 if (smb
->pdata
->use_mains
)
748 power_supply_changed(&smb
->mains
);
749 if (smb
->pdata
->use_usb
)
750 power_supply_changed(&smb
->usb
);
755 return handled
? IRQ_HANDLED
: IRQ_NONE
;
758 static int smb347_irq_set(struct smb347_charger
*smb
, bool enable
)
762 ret
= smb347_set_writable(smb
, true);
767 * Enable/disable interrupts for:
769 * - termination current reached
772 ret
= regmap_update_bits(smb
->regmap
, CFG_FAULT_IRQ
, 0xff,
773 enable
? CFG_FAULT_IRQ_DCIN_UV
: 0);
777 ret
= regmap_update_bits(smb
->regmap
, CFG_STATUS_IRQ
, 0xff,
778 enable
? CFG_STATUS_IRQ_TERMINATION_OR_TAPER
: 0);
782 ret
= regmap_update_bits(smb
->regmap
, CFG_PIN
, CFG_PIN_EN_CHARGER_ERROR
,
783 enable
? CFG_PIN_EN_CHARGER_ERROR
: 0);
785 smb347_set_writable(smb
, false);
789 static inline int smb347_irq_enable(struct smb347_charger
*smb
)
791 return smb347_irq_set(smb
, true);
794 static inline int smb347_irq_disable(struct smb347_charger
*smb
)
796 return smb347_irq_set(smb
, false);
799 static int smb347_irq_init(struct smb347_charger
*smb
,
800 struct i2c_client
*client
)
802 const struct smb347_charger_platform_data
*pdata
= smb
->pdata
;
803 int ret
, irq
= gpio_to_irq(pdata
->irq_gpio
);
805 ret
= gpio_request_one(pdata
->irq_gpio
, GPIOF_IN
, client
->name
);
809 ret
= request_threaded_irq(irq
, NULL
, smb347_interrupt
,
810 IRQF_TRIGGER_FALLING
, client
->name
, smb
);
814 ret
= smb347_set_writable(smb
, true);
819 * Configure the STAT output to be suitable for interrupts: disable
820 * all other output (except interrupts) and make it active low.
822 ret
= regmap_update_bits(smb
->regmap
, CFG_STAT
,
823 CFG_STAT_ACTIVE_HIGH
| CFG_STAT_DISABLED
,
828 smb347_set_writable(smb
, false);
833 smb347_set_writable(smb
, false);
837 gpio_free(pdata
->irq_gpio
);
843 static int smb347_mains_get_property(struct power_supply
*psy
,
844 enum power_supply_property prop
,
845 union power_supply_propval
*val
)
847 struct smb347_charger
*smb
=
848 container_of(psy
, struct smb347_charger
, mains
);
850 if (prop
== POWER_SUPPLY_PROP_ONLINE
) {
851 val
->intval
= smb
->mains_online
;
857 static enum power_supply_property smb347_mains_properties
[] = {
858 POWER_SUPPLY_PROP_ONLINE
,
861 static int smb347_usb_get_property(struct power_supply
*psy
,
862 enum power_supply_property prop
,
863 union power_supply_propval
*val
)
865 struct smb347_charger
*smb
=
866 container_of(psy
, struct smb347_charger
, usb
);
868 if (prop
== POWER_SUPPLY_PROP_ONLINE
) {
869 val
->intval
= smb
->usb_online
;
875 static enum power_supply_property smb347_usb_properties
[] = {
876 POWER_SUPPLY_PROP_ONLINE
,
879 static int smb347_battery_get_property(struct power_supply
*psy
,
880 enum power_supply_property prop
,
881 union power_supply_propval
*val
)
883 struct smb347_charger
*smb
=
884 container_of(psy
, struct smb347_charger
, battery
);
885 const struct smb347_charger_platform_data
*pdata
= smb
->pdata
;
888 ret
= smb347_update_ps_status(smb
);
893 case POWER_SUPPLY_PROP_STATUS
:
894 if (!smb347_is_ps_online(smb
)) {
895 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
898 if (smb347_charging_status(smb
))
899 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
901 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
904 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
905 if (!smb347_is_ps_online(smb
))
909 * We handle trickle and pre-charging the same, and taper
912 switch (smb347_charging_status(smb
)) {
914 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
917 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
920 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
925 case POWER_SUPPLY_PROP_TECHNOLOGY
:
926 val
->intval
= pdata
->battery_info
.technology
;
929 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
930 val
->intval
= pdata
->battery_info
.voltage_min_design
;
933 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
934 val
->intval
= pdata
->battery_info
.voltage_max_design
;
937 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
938 val
->intval
= pdata
->battery_info
.charge_full_design
;
941 case POWER_SUPPLY_PROP_MODEL_NAME
:
942 val
->strval
= pdata
->battery_info
.name
;
952 static enum power_supply_property smb347_battery_properties
[] = {
953 POWER_SUPPLY_PROP_STATUS
,
954 POWER_SUPPLY_PROP_CHARGE_TYPE
,
955 POWER_SUPPLY_PROP_TECHNOLOGY
,
956 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
957 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
958 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
959 POWER_SUPPLY_PROP_MODEL_NAME
,
962 static bool smb347_volatile_reg(struct device
*dev
, unsigned int reg
)
979 static bool smb347_readable_reg(struct device
*dev
, unsigned int reg
)
982 case CFG_CHARGE_CURRENT
:
983 case CFG_CURRENT_LIMIT
:
984 case CFG_FLOAT_VOLTAGE
:
1001 return smb347_volatile_reg(dev
, reg
);
1004 static const struct regmap_config smb347_regmap
= {
1007 .max_register
= SMB347_MAX_REGISTER
,
1008 .volatile_reg
= smb347_volatile_reg
,
1009 .readable_reg
= smb347_readable_reg
,
1012 static int smb347_probe(struct i2c_client
*client
,
1013 const struct i2c_device_id
*id
)
1015 static char *battery
[] = { "smb347-battery" };
1016 const struct smb347_charger_platform_data
*pdata
;
1017 struct device
*dev
= &client
->dev
;
1018 struct smb347_charger
*smb
;
1021 pdata
= dev
->platform_data
;
1025 if (!pdata
->use_mains
&& !pdata
->use_usb
)
1028 smb
= devm_kzalloc(dev
, sizeof(*smb
), GFP_KERNEL
);
1032 i2c_set_clientdata(client
, smb
);
1034 mutex_init(&smb
->lock
);
1035 smb
->dev
= &client
->dev
;
1038 smb
->regmap
= devm_regmap_init_i2c(client
, &smb347_regmap
);
1039 if (IS_ERR(smb
->regmap
))
1040 return PTR_ERR(smb
->regmap
);
1042 ret
= smb347_hw_init(smb
);
1046 if (smb
->pdata
->use_mains
) {
1047 smb
->mains
.name
= "smb347-mains";
1048 smb
->mains
.type
= POWER_SUPPLY_TYPE_MAINS
;
1049 smb
->mains
.get_property
= smb347_mains_get_property
;
1050 smb
->mains
.properties
= smb347_mains_properties
;
1051 smb
->mains
.num_properties
= ARRAY_SIZE(smb347_mains_properties
);
1052 smb
->mains
.supplied_to
= battery
;
1053 smb
->mains
.num_supplicants
= ARRAY_SIZE(battery
);
1054 ret
= power_supply_register(dev
, &smb
->mains
);
1059 if (smb
->pdata
->use_usb
) {
1060 smb
->usb
.name
= "smb347-usb";
1061 smb
->usb
.type
= POWER_SUPPLY_TYPE_USB
;
1062 smb
->usb
.get_property
= smb347_usb_get_property
;
1063 smb
->usb
.properties
= smb347_usb_properties
;
1064 smb
->usb
.num_properties
= ARRAY_SIZE(smb347_usb_properties
);
1065 smb
->usb
.supplied_to
= battery
;
1066 smb
->usb
.num_supplicants
= ARRAY_SIZE(battery
);
1067 ret
= power_supply_register(dev
, &smb
->usb
);
1069 if (smb
->pdata
->use_mains
)
1070 power_supply_unregister(&smb
->mains
);
1075 smb
->battery
.name
= "smb347-battery";
1076 smb
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
1077 smb
->battery
.get_property
= smb347_battery_get_property
;
1078 smb
->battery
.properties
= smb347_battery_properties
;
1079 smb
->battery
.num_properties
= ARRAY_SIZE(smb347_battery_properties
);
1082 ret
= power_supply_register(dev
, &smb
->battery
);
1084 if (smb
->pdata
->use_usb
)
1085 power_supply_unregister(&smb
->usb
);
1086 if (smb
->pdata
->use_mains
)
1087 power_supply_unregister(&smb
->mains
);
1092 * Interrupt pin is optional. If it is connected, we setup the
1093 * interrupt support here.
1095 if (pdata
->irq_gpio
>= 0) {
1096 ret
= smb347_irq_init(smb
, client
);
1098 dev_warn(dev
, "failed to initialize IRQ: %d\n", ret
);
1099 dev_warn(dev
, "disabling IRQ support\n");
1101 smb347_irq_enable(smb
);
1108 static int smb347_remove(struct i2c_client
*client
)
1110 struct smb347_charger
*smb
= i2c_get_clientdata(client
);
1113 smb347_irq_disable(smb
);
1114 free_irq(client
->irq
, smb
);
1115 gpio_free(smb
->pdata
->irq_gpio
);
1118 power_supply_unregister(&smb
->battery
);
1119 if (smb
->pdata
->use_usb
)
1120 power_supply_unregister(&smb
->usb
);
1121 if (smb
->pdata
->use_mains
)
1122 power_supply_unregister(&smb
->mains
);
1126 static const struct i2c_device_id smb347_id
[] = {
1130 MODULE_DEVICE_TABLE(i2c
, smb347_id
);
1132 static struct i2c_driver smb347_driver
= {
1136 .probe
= smb347_probe
,
1137 .remove
= __devexit_p(smb347_remove
),
1138 .id_table
= smb347_id
,
1141 module_i2c_driver(smb347_driver
);
1143 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1144 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1145 MODULE_DESCRIPTION("SMB347 battery charger driver");
1146 MODULE_LICENSE("GPL");
1147 MODULE_ALIAS("i2c:smb347");