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/err.h>
15 #include <linux/gpio.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/i2c.h>
21 #include <linux/mutex.h>
22 #include <linux/power_supply.h>
23 #include <linux/power/smb347-charger.h>
24 #include <linux/regmap.h>
27 * Configuration registers. These are mirrored to volatile RAM and can be
28 * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
29 * reloaded from non-volatile registers after POR.
31 #define CFG_CHARGE_CURRENT 0x00
32 #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
33 #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
34 #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
35 #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
36 #define CFG_CHARGE_CURRENT_TC_MASK 0x07
37 #define CFG_CURRENT_LIMIT 0x01
38 #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
39 #define CFG_CURRENT_LIMIT_DC_SHIFT 4
40 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
41 #define CFG_FLOAT_VOLTAGE 0x03
42 #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
43 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
44 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
46 #define CFG_STAT_DISABLED BIT(5)
47 #define CFG_STAT_ACTIVE_HIGH BIT(7)
49 #define CFG_PIN_EN_CTRL_MASK 0x60
50 #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
51 #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
52 #define CFG_PIN_EN_APSD_IRQ BIT(1)
53 #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
54 #define CFG_THERM 0x07
55 #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
56 #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
57 #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
58 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
59 #define CFG_THERM_MONITOR_DISABLED BIT(4)
60 #define CFG_SYSOK 0x08
61 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
62 #define CFG_OTHER 0x09
63 #define CFG_OTHER_RID_MASK 0xc0
64 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
66 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
67 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
68 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
69 #define CFG_OTG_CC_COMPENSATION_SHIFT 6
70 #define CFG_TEMP_LIMIT 0x0b
71 #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
72 #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
73 #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
74 #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
75 #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
76 #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
77 #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
78 #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
79 #define CFG_FAULT_IRQ 0x0c
80 #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
81 #define CFG_STATUS_IRQ 0x0d
82 #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
83 #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7)
84 #define CFG_ADDRESS 0x0e
86 /* Command registers */
88 #define CMD_A_CHG_ENABLED BIT(1)
89 #define CMD_A_SUSPEND_ENABLED BIT(2)
90 #define CMD_A_ALLOW_WRITE BIT(7)
94 /* Interrupt Status registers */
95 #define IRQSTAT_A 0x35
96 #define IRQSTAT_C 0x37
97 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
98 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
99 #define IRQSTAT_C_TAPER_IRQ BIT(3)
100 #define IRQSTAT_D 0x38
101 #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2)
102 #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3)
103 #define IRQSTAT_E 0x39
104 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
105 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
106 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
107 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
108 #define IRQSTAT_F 0x3a
110 /* Status registers */
112 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
115 #define STAT_C_CHG_ENABLED BIT(0)
116 #define STAT_C_HOLDOFF_STAT BIT(3)
117 #define STAT_C_CHG_MASK 0x06
118 #define STAT_C_CHG_SHIFT 1
119 #define STAT_C_CHG_TERM BIT(5)
120 #define STAT_C_CHARGER_ERROR BIT(6)
123 #define SMB347_MAX_REGISTER 0x3f
126 * struct smb347_charger - smb347 charger instance
127 * @lock: protects concurrent access to online variables
128 * @dev: pointer to device
129 * @regmap: pointer to driver regmap
130 * @mains: power_supply instance for AC/DC power
131 * @usb: power_supply instance for USB power
132 * @battery: power_supply instance for battery
133 * @mains_online: is AC/DC input connected
134 * @usb_online: is USB input connected
135 * @charging_enabled: is charging enabled
136 * @pdata: pointer to platform data
138 struct smb347_charger {
141 struct regmap *regmap;
142 struct power_supply mains;
143 struct power_supply usb;
144 struct power_supply battery;
147 bool charging_enabled;
148 const struct smb347_charger_platform_data *pdata;
151 /* Fast charge current in uA */
152 static const unsigned int fcc_tbl[] = {
163 /* Pre-charge current in uA */
164 static const unsigned int pcc_tbl[] = {
171 /* Termination current in uA */
172 static const unsigned int tc_tbl[] = {
183 /* Input current limit in uA */
184 static const unsigned int icl_tbl[] = {
197 /* Charge current compensation in uA */
198 static const unsigned int ccc_tbl[] = {
205 /* Convert register value to current using lookup table */
206 static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
213 /* Convert current to register value using lookup table */
214 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
218 for (i = 0; i < size; i++)
221 return i > 0 ? i - 1 : -EINVAL;
225 * smb347_update_ps_status - refreshes the power source status
226 * @smb: pointer to smb347 charger instance
228 * Function checks whether any power source is connected to the charger and
229 * updates internal state accordingly. If there is a change to previous state
230 * function returns %1, otherwise %0 and negative errno in case of errror.
232 static int smb347_update_ps_status(struct smb347_charger *smb)
239 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
244 * Dc and usb are set depending on whether they are enabled in
245 * platform data _and_ whether corresponding undervoltage is set.
247 if (smb->pdata->use_mains)
248 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
249 if (smb->pdata->use_usb)
250 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
252 mutex_lock(&smb->lock);
253 ret = smb->mains_online != dc || smb->usb_online != usb;
254 smb->mains_online = dc;
255 smb->usb_online = usb;
256 mutex_unlock(&smb->lock);
262 * smb347_is_ps_online - returns whether input power source is connected
263 * @smb: pointer to smb347 charger instance
265 * Returns %true if input power source is connected. Note that this is
266 * dependent on what platform has configured for usable power sources. For
267 * example if USB is disabled, this will return %false even if the USB cable
270 static bool smb347_is_ps_online(struct smb347_charger *smb)
274 mutex_lock(&smb->lock);
275 ret = smb->usb_online || smb->mains_online;
276 mutex_unlock(&smb->lock);
282 * smb347_charging_status - returns status of charging
283 * @smb: pointer to smb347 charger instance
285 * Function returns charging status. %0 means no charging is in progress,
286 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
288 static int smb347_charging_status(struct smb347_charger *smb)
293 if (!smb347_is_ps_online(smb))
296 ret = regmap_read(smb->regmap, STAT_C, &val);
300 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
303 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
307 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
308 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
312 mutex_lock(&smb->lock);
313 if (smb->charging_enabled != enable) {
314 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
315 enable ? CMD_A_CHG_ENABLED : 0);
317 smb->charging_enabled = enable;
319 mutex_unlock(&smb->lock);
323 static inline int smb347_charging_enable(struct smb347_charger *smb)
325 return smb347_charging_set(smb, true);
328 static inline int smb347_charging_disable(struct smb347_charger *smb)
330 return smb347_charging_set(smb, false);
333 static int smb347_start_stop_charging(struct smb347_charger *smb)
338 * Depending on whether valid power source is connected or not, we
339 * disable or enable the charging. We do it manually because it
340 * depends on how the platform has configured the valid inputs.
342 if (smb347_is_ps_online(smb)) {
343 ret = smb347_charging_enable(smb);
345 dev_err(smb->dev, "failed to enable charging\n");
347 ret = smb347_charging_disable(smb);
349 dev_err(smb->dev, "failed to disable charging\n");
355 static int smb347_set_charge_current(struct smb347_charger *smb)
359 if (smb->pdata->max_charge_current) {
360 ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
361 smb->pdata->max_charge_current);
365 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
366 CFG_CHARGE_CURRENT_FCC_MASK,
367 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
372 if (smb->pdata->pre_charge_current) {
373 ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
374 smb->pdata->pre_charge_current);
378 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
379 CFG_CHARGE_CURRENT_PCC_MASK,
380 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
385 if (smb->pdata->termination_current) {
386 ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
387 smb->pdata->termination_current);
391 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
392 CFG_CHARGE_CURRENT_TC_MASK, ret);
400 static int smb347_set_current_limits(struct smb347_charger *smb)
404 if (smb->pdata->mains_current_limit) {
405 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
406 smb->pdata->mains_current_limit);
410 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
411 CFG_CURRENT_LIMIT_DC_MASK,
412 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
417 if (smb->pdata->usb_hc_current_limit) {
418 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
419 smb->pdata->usb_hc_current_limit);
423 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
424 CFG_CURRENT_LIMIT_USB_MASK, ret);
432 static int smb347_set_voltage_limits(struct smb347_charger *smb)
436 if (smb->pdata->pre_to_fast_voltage) {
437 ret = smb->pdata->pre_to_fast_voltage;
440 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
443 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
444 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
445 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
450 if (smb->pdata->max_charge_voltage) {
451 ret = smb->pdata->max_charge_voltage;
454 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
457 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
458 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
466 static int smb347_set_temp_limits(struct smb347_charger *smb)
468 bool enable_therm_monitor = false;
472 if (smb->pdata->chip_temp_threshold) {
473 val = smb->pdata->chip_temp_threshold;
476 val = clamp_val(val, 100, 130) - 100;
479 ret = regmap_update_bits(smb->regmap, CFG_OTG,
480 CFG_OTG_TEMP_THRESHOLD_MASK,
481 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
486 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
487 val = smb->pdata->soft_cold_temp_limit;
489 val = clamp_val(val, 0, 15);
491 /* this goes from higher to lower so invert the value */
494 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
495 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
496 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
500 enable_therm_monitor = true;
503 if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
504 val = smb->pdata->soft_hot_temp_limit;
506 val = clamp_val(val, 40, 55) - 40;
509 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
510 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
511 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
515 enable_therm_monitor = true;
518 if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
519 val = smb->pdata->hard_cold_temp_limit;
521 val = clamp_val(val, -5, 10) + 5;
523 /* this goes from higher to lower so invert the value */
526 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
527 CFG_TEMP_LIMIT_HARD_COLD_MASK,
528 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
532 enable_therm_monitor = true;
535 if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
536 val = smb->pdata->hard_hot_temp_limit;
538 val = clamp_val(val, 50, 65) - 50;
541 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
542 CFG_TEMP_LIMIT_HARD_HOT_MASK,
543 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
547 enable_therm_monitor = true;
551 * If any of the temperature limits are set, we also enable the
552 * thermistor monitoring.
554 * When soft limits are hit, the device will start to compensate
555 * current and/or voltage depending on the configuration.
557 * When hard limit is hit, the device will suspend charging
558 * depending on the configuration.
560 if (enable_therm_monitor) {
561 ret = regmap_update_bits(smb->regmap, CFG_THERM,
562 CFG_THERM_MONITOR_DISABLED, 0);
567 if (smb->pdata->suspend_on_hard_temp_limit) {
568 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
569 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
574 if (smb->pdata->soft_temp_limit_compensation !=
575 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
576 val = smb->pdata->soft_temp_limit_compensation & 0x3;
578 ret = regmap_update_bits(smb->regmap, CFG_THERM,
579 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
580 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
584 ret = regmap_update_bits(smb->regmap, CFG_THERM,
585 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
586 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
591 if (smb->pdata->charge_current_compensation) {
592 val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
593 smb->pdata->charge_current_compensation);
597 ret = regmap_update_bits(smb->regmap, CFG_OTG,
598 CFG_OTG_CC_COMPENSATION_MASK,
599 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
608 * smb347_set_writable - enables/disables writing to non-volatile registers
609 * @smb: pointer to smb347 charger instance
611 * You can enable/disable writing to the non-volatile configuration
612 * registers by calling this function.
614 * Returns %0 on success and negative errno in case of failure.
616 static int smb347_set_writable(struct smb347_charger *smb, bool writable)
618 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
619 writable ? CMD_A_ALLOW_WRITE : 0);
622 static int smb347_hw_init(struct smb347_charger *smb)
627 ret = smb347_set_writable(smb, true);
632 * Program the platform specific configuration values to the device
635 ret = smb347_set_charge_current(smb);
639 ret = smb347_set_current_limits(smb);
643 ret = smb347_set_voltage_limits(smb);
647 ret = smb347_set_temp_limits(smb);
651 /* If USB charging is disabled we put the USB in suspend mode */
652 if (!smb->pdata->use_usb) {
653 ret = regmap_update_bits(smb->regmap, CMD_A,
654 CMD_A_SUSPEND_ENABLED,
655 CMD_A_SUSPEND_ENABLED);
661 * If configured by platform data, we enable hardware Auto-OTG
662 * support for driving VBUS. Otherwise we disable it.
664 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
665 smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
670 * Make the charging functionality controllable by a write to the
671 * command register unless pin control is specified in the platform
674 switch (smb->pdata->enable_control) {
675 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
676 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
678 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
679 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
686 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
691 /* Disable Automatic Power Source Detection (APSD) interrupt. */
692 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
696 ret = smb347_update_ps_status(smb);
700 ret = smb347_start_stop_charging(smb);
703 smb347_set_writable(smb, false);
707 static irqreturn_t smb347_interrupt(int irq, void *data)
709 struct smb347_charger *smb = data;
710 unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
711 bool handled = false;
714 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
716 dev_warn(smb->dev, "reading STAT_C failed\n");
720 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
722 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
726 ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
728 dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
732 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
734 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
739 * If we get charger error we report the error back to user.
740 * If the error is recovered charging will resume again.
742 if (stat_c & STAT_C_CHARGER_ERROR) {
743 dev_err(smb->dev, "charging stopped due to charger error\n");
744 power_supply_changed(&smb->battery);
749 * If we reached the termination current the battery is charged and
750 * we can update the status now. Charging is automatically
751 * disabled by the hardware.
753 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
754 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT)
755 power_supply_changed(&smb->battery);
756 dev_dbg(smb->dev, "going to HW maintenance mode\n");
761 * If we got a charger timeout INT that means the charge
762 * full is not detected with in charge timeout value.
764 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
765 dev_dbg(smb->dev, "total Charge Timeout INT received\n");
767 if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
768 dev_warn(smb->dev, "charging stopped due to timeout\n");
769 power_supply_changed(&smb->battery);
774 * If we got an under voltage interrupt it means that AC/USB input
775 * was connected or disconnected.
777 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
778 if (smb347_update_ps_status(smb) > 0) {
779 smb347_start_stop_charging(smb);
780 if (smb->pdata->use_mains)
781 power_supply_changed(&smb->mains);
782 if (smb->pdata->use_usb)
783 power_supply_changed(&smb->usb);
788 return handled ? IRQ_HANDLED : IRQ_NONE;
791 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
795 ret = smb347_set_writable(smb, true);
800 * Enable/disable interrupts for:
802 * - termination current reached
806 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
807 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
811 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
812 enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
813 CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
817 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
818 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
820 smb347_set_writable(smb, false);
824 static inline int smb347_irq_enable(struct smb347_charger *smb)
826 return smb347_irq_set(smb, true);
829 static inline int smb347_irq_disable(struct smb347_charger *smb)
831 return smb347_irq_set(smb, false);
834 static int smb347_irq_init(struct smb347_charger *smb,
835 struct i2c_client *client)
837 const struct smb347_charger_platform_data *pdata = smb->pdata;
838 int ret, irq = gpio_to_irq(pdata->irq_gpio);
840 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
844 ret = request_threaded_irq(irq, NULL, smb347_interrupt,
845 IRQF_TRIGGER_FALLING, client->name, smb);
849 ret = smb347_set_writable(smb, true);
854 * Configure the STAT output to be suitable for interrupts: disable
855 * all other output (except interrupts) and make it active low.
857 ret = regmap_update_bits(smb->regmap, CFG_STAT,
858 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
863 smb347_set_writable(smb, false);
868 smb347_set_writable(smb, false);
872 gpio_free(pdata->irq_gpio);
879 * Returns the constant charge current programmed
880 * into the charger in uA.
882 static int get_const_charge_current(struct smb347_charger *smb)
887 if (!smb347_is_ps_online(smb))
890 ret = regmap_read(smb->regmap, STAT_B, &v);
895 * The current value is composition of FCC and PCC values
896 * and we can detect which table to use from bit 5.
899 intval = hw_to_current(fcc_tbl, ARRAY_SIZE(fcc_tbl), v & 7);
902 intval = hw_to_current(pcc_tbl, ARRAY_SIZE(pcc_tbl), v & 7);
909 * Returns the constant charge voltage programmed
910 * into the charger in uV.
912 static int get_const_charge_voltage(struct smb347_charger *smb)
917 if (!smb347_is_ps_online(smb))
920 ret = regmap_read(smb->regmap, STAT_A, &v);
924 v &= STAT_A_FLOAT_VOLTAGE_MASK;
928 intval = 3500000 + v * 20000;
933 static int smb347_mains_get_property(struct power_supply *psy,
934 enum power_supply_property prop,
935 union power_supply_propval *val)
937 struct smb347_charger *smb =
938 container_of(psy, struct smb347_charger, mains);
942 case POWER_SUPPLY_PROP_ONLINE:
943 val->intval = smb->mains_online;
946 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
947 ret = get_const_charge_voltage(smb);
954 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
955 ret = get_const_charge_current(smb);
969 static enum power_supply_property smb347_mains_properties[] = {
970 POWER_SUPPLY_PROP_ONLINE,
971 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
972 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
975 static int smb347_usb_get_property(struct power_supply *psy,
976 enum power_supply_property prop,
977 union power_supply_propval *val)
979 struct smb347_charger *smb =
980 container_of(psy, struct smb347_charger, usb);
984 case POWER_SUPPLY_PROP_ONLINE:
985 val->intval = smb->usb_online;
988 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
989 ret = get_const_charge_voltage(smb);
996 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
997 ret = get_const_charge_current(smb);
1011 static enum power_supply_property smb347_usb_properties[] = {
1012 POWER_SUPPLY_PROP_ONLINE,
1013 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1014 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1017 static int smb347_get_charging_status(struct smb347_charger *smb)
1022 if (!smb347_is_ps_online(smb))
1023 return POWER_SUPPLY_STATUS_DISCHARGING;
1025 ret = regmap_read(smb->regmap, STAT_C, &val);
1029 if ((val & STAT_C_CHARGER_ERROR) ||
1030 (val & STAT_C_HOLDOFF_STAT)) {
1032 * set to NOT CHARGING upon charger error
1033 * or charging has stopped.
1035 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1037 if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
1039 * set to charging if battery is in pre-charge,
1040 * fast charge or taper charging mode.
1042 status = POWER_SUPPLY_STATUS_CHARGING;
1043 } else if (val & STAT_C_CHG_TERM) {
1045 * set the status to FULL if battery is not in pre
1046 * charge, fast charge or taper charging mode AND
1047 * charging is terminated at least once.
1049 status = POWER_SUPPLY_STATUS_FULL;
1052 * in this case no charger error or termination
1053 * occured but charging is not in progress!!!
1055 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1062 static int smb347_battery_get_property(struct power_supply *psy,
1063 enum power_supply_property prop,
1064 union power_supply_propval *val)
1066 struct smb347_charger *smb =
1067 container_of(psy, struct smb347_charger, battery);
1068 const struct smb347_charger_platform_data *pdata = smb->pdata;
1071 ret = smb347_update_ps_status(smb);
1076 case POWER_SUPPLY_PROP_STATUS:
1077 ret = smb347_get_charging_status(smb);
1083 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1084 if (!smb347_is_ps_online(smb))
1088 * We handle trickle and pre-charging the same, and taper
1089 * and none the same.
1091 switch (smb347_charging_status(smb)) {
1093 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1096 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1099 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1104 case POWER_SUPPLY_PROP_TECHNOLOGY:
1105 val->intval = pdata->battery_info.technology;
1108 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1109 val->intval = pdata->battery_info.voltage_min_design;
1112 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1113 val->intval = pdata->battery_info.voltage_max_design;
1116 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1117 val->intval = pdata->battery_info.charge_full_design;
1120 case POWER_SUPPLY_PROP_MODEL_NAME:
1121 val->strval = pdata->battery_info.name;
1131 static enum power_supply_property smb347_battery_properties[] = {
1132 POWER_SUPPLY_PROP_STATUS,
1133 POWER_SUPPLY_PROP_CHARGE_TYPE,
1134 POWER_SUPPLY_PROP_TECHNOLOGY,
1135 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1136 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1137 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
1138 POWER_SUPPLY_PROP_MODEL_NAME,
1141 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
1158 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
1161 case CFG_CHARGE_CURRENT:
1162 case CFG_CURRENT_LIMIT:
1163 case CFG_FLOAT_VOLTAGE:
1170 case CFG_TEMP_LIMIT:
1172 case CFG_STATUS_IRQ:
1180 return smb347_volatile_reg(dev, reg);
1183 static const struct regmap_config smb347_regmap = {
1186 .max_register = SMB347_MAX_REGISTER,
1187 .volatile_reg = smb347_volatile_reg,
1188 .readable_reg = smb347_readable_reg,
1191 static int smb347_probe(struct i2c_client *client,
1192 const struct i2c_device_id *id)
1194 static char *battery[] = { "smb347-battery" };
1195 const struct smb347_charger_platform_data *pdata;
1196 struct device *dev = &client->dev;
1197 struct smb347_charger *smb;
1200 pdata = dev->platform_data;
1204 if (!pdata->use_mains && !pdata->use_usb)
1207 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1211 i2c_set_clientdata(client, smb);
1213 mutex_init(&smb->lock);
1214 smb->dev = &client->dev;
1217 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1218 if (IS_ERR(smb->regmap))
1219 return PTR_ERR(smb->regmap);
1221 ret = smb347_hw_init(smb);
1225 if (smb->pdata->use_mains) {
1226 smb->mains.name = "smb347-mains";
1227 smb->mains.type = POWER_SUPPLY_TYPE_MAINS;
1228 smb->mains.get_property = smb347_mains_get_property;
1229 smb->mains.properties = smb347_mains_properties;
1230 smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties);
1231 smb->mains.supplied_to = battery;
1232 smb->mains.num_supplicants = ARRAY_SIZE(battery);
1233 ret = power_supply_register(dev, &smb->mains);
1238 if (smb->pdata->use_usb) {
1239 smb->usb.name = "smb347-usb";
1240 smb->usb.type = POWER_SUPPLY_TYPE_USB;
1241 smb->usb.get_property = smb347_usb_get_property;
1242 smb->usb.properties = smb347_usb_properties;
1243 smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties);
1244 smb->usb.supplied_to = battery;
1245 smb->usb.num_supplicants = ARRAY_SIZE(battery);
1246 ret = power_supply_register(dev, &smb->usb);
1248 if (smb->pdata->use_mains)
1249 power_supply_unregister(&smb->mains);
1254 smb->battery.name = "smb347-battery";
1255 smb->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1256 smb->battery.get_property = smb347_battery_get_property;
1257 smb->battery.properties = smb347_battery_properties;
1258 smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties);
1261 ret = power_supply_register(dev, &smb->battery);
1263 if (smb->pdata->use_usb)
1264 power_supply_unregister(&smb->usb);
1265 if (smb->pdata->use_mains)
1266 power_supply_unregister(&smb->mains);
1271 * Interrupt pin is optional. If it is connected, we setup the
1272 * interrupt support here.
1274 if (pdata->irq_gpio >= 0) {
1275 ret = smb347_irq_init(smb, client);
1277 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1278 dev_warn(dev, "disabling IRQ support\n");
1280 smb347_irq_enable(smb);
1287 static int smb347_remove(struct i2c_client *client)
1289 struct smb347_charger *smb = i2c_get_clientdata(client);
1292 smb347_irq_disable(smb);
1293 free_irq(client->irq, smb);
1294 gpio_free(smb->pdata->irq_gpio);
1297 power_supply_unregister(&smb->battery);
1298 if (smb->pdata->use_usb)
1299 power_supply_unregister(&smb->usb);
1300 if (smb->pdata->use_mains)
1301 power_supply_unregister(&smb->mains);
1305 static const struct i2c_device_id smb347_id[] = {
1309 MODULE_DEVICE_TABLE(i2c, smb347_id);
1311 static struct i2c_driver smb347_driver = {
1315 .probe = smb347_probe,
1316 .remove = smb347_remove,
1317 .id_table = smb347_id,
1320 module_i2c_driver(smb347_driver);
1322 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1323 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1324 MODULE_DESCRIPTION("SMB347 battery charger driver");
1325 MODULE_LICENSE("GPL");
1326 MODULE_ALIAS("i2c:smb347");