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_ADDRESS 0x0e
85 /* Command registers */
87 #define CMD_A_CHG_ENABLED BIT(1)
88 #define CMD_A_SUSPEND_ENABLED BIT(2)
89 #define CMD_A_ALLOW_WRITE BIT(7)
93 /* Interrupt Status registers */
94 #define IRQSTAT_A 0x35
95 #define IRQSTAT_C 0x37
96 #define IRQSTAT_C_TERMINATION_STAT BIT(0)
97 #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
98 #define IRQSTAT_C_TAPER_IRQ BIT(3)
99 #define IRQSTAT_E 0x39
100 #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
101 #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
102 #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
103 #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
104 #define IRQSTAT_F 0x3a
106 /* Status registers */
108 #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
111 #define STAT_C_CHG_ENABLED BIT(0)
112 #define STAT_C_CHG_MASK 0x06
113 #define STAT_C_CHG_SHIFT 1
114 #define STAT_C_CHARGER_ERROR BIT(6)
117 #define SMB347_MAX_REGISTER 0x3f
120 * struct smb347_charger - smb347 charger instance
121 * @lock: protects concurrent access to online variables
122 * @dev: pointer to device
123 * @regmap: pointer to driver regmap
124 * @mains: power_supply instance for AC/DC power
125 * @usb: power_supply instance for USB power
126 * @battery: power_supply instance for battery
127 * @mains_online: is AC/DC input connected
128 * @usb_online: is USB input connected
129 * @charging_enabled: is charging enabled
130 * @pdata: pointer to platform data
132 struct smb347_charger {
135 struct regmap *regmap;
136 struct power_supply mains;
137 struct power_supply usb;
138 struct power_supply battery;
141 bool charging_enabled;
142 const struct smb347_charger_platform_data *pdata;
145 /* Fast charge current in uA */
146 static const unsigned int fcc_tbl[] = {
157 /* Pre-charge current in uA */
158 static const unsigned int pcc_tbl[] = {
165 /* Termination current in uA */
166 static const unsigned int tc_tbl[] = {
177 /* Input current limit in uA */
178 static const unsigned int icl_tbl[] = {
191 /* Charge current compensation in uA */
192 static const unsigned int ccc_tbl[] = {
199 /* Convert current to register value using lookup table */
200 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
204 for (i = 0; i < size; i++)
207 return i > 0 ? i - 1 : -EINVAL;
211 * smb347_update_ps_status - refreshes the power source status
212 * @smb: pointer to smb347 charger instance
214 * Function checks whether any power source is connected to the charger and
215 * updates internal state accordingly. If there is a change to previous state
216 * function returns %1, otherwise %0 and negative errno in case of errror.
218 static int smb347_update_ps_status(struct smb347_charger *smb)
225 ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
230 * Dc and usb are set depending on whether they are enabled in
231 * platform data _and_ whether corresponding undervoltage is set.
233 if (smb->pdata->use_mains)
234 dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
235 if (smb->pdata->use_usb)
236 usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
238 mutex_lock(&smb->lock);
239 ret = smb->mains_online != dc || smb->usb_online != usb;
240 smb->mains_online = dc;
241 smb->usb_online = usb;
242 mutex_unlock(&smb->lock);
248 * smb347_is_ps_online - returns whether input power source is connected
249 * @smb: pointer to smb347 charger instance
251 * Returns %true if input power source is connected. Note that this is
252 * dependent on what platform has configured for usable power sources. For
253 * example if USB is disabled, this will return %false even if the USB cable
256 static bool smb347_is_ps_online(struct smb347_charger *smb)
260 mutex_lock(&smb->lock);
261 ret = smb->usb_online || smb->mains_online;
262 mutex_unlock(&smb->lock);
268 * smb347_charging_status - returns status of charging
269 * @smb: pointer to smb347 charger instance
271 * Function returns charging status. %0 means no charging is in progress,
272 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
274 static int smb347_charging_status(struct smb347_charger *smb)
279 if (!smb347_is_ps_online(smb))
282 ret = regmap_read(smb->regmap, STAT_C, &val);
286 return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
289 static int smb347_charging_set(struct smb347_charger *smb, bool enable)
293 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
294 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
298 mutex_lock(&smb->lock);
299 if (smb->charging_enabled != enable) {
300 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
301 enable ? CMD_A_CHG_ENABLED : 0);
303 smb->charging_enabled = enable;
305 mutex_unlock(&smb->lock);
309 static inline int smb347_charging_enable(struct smb347_charger *smb)
311 return smb347_charging_set(smb, true);
314 static inline int smb347_charging_disable(struct smb347_charger *smb)
316 return smb347_charging_set(smb, false);
319 static int smb347_start_stop_charging(struct smb347_charger *smb)
324 * Depending on whether valid power source is connected or not, we
325 * disable or enable the charging. We do it manually because it
326 * depends on how the platform has configured the valid inputs.
328 if (smb347_is_ps_online(smb)) {
329 ret = smb347_charging_enable(smb);
331 dev_err(smb->dev, "failed to enable charging\n");
333 ret = smb347_charging_disable(smb);
335 dev_err(smb->dev, "failed to disable charging\n");
341 static int smb347_set_charge_current(struct smb347_charger *smb)
345 if (smb->pdata->max_charge_current) {
346 ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
347 smb->pdata->max_charge_current);
351 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
352 CFG_CHARGE_CURRENT_FCC_MASK,
353 ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
358 if (smb->pdata->pre_charge_current) {
359 ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
360 smb->pdata->pre_charge_current);
364 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
365 CFG_CHARGE_CURRENT_PCC_MASK,
366 ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
371 if (smb->pdata->termination_current) {
372 ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
373 smb->pdata->termination_current);
377 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
378 CFG_CHARGE_CURRENT_TC_MASK, ret);
386 static int smb347_set_current_limits(struct smb347_charger *smb)
390 if (smb->pdata->mains_current_limit) {
391 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
392 smb->pdata->mains_current_limit);
396 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
397 CFG_CURRENT_LIMIT_DC_MASK,
398 ret << CFG_CURRENT_LIMIT_DC_SHIFT);
403 if (smb->pdata->usb_hc_current_limit) {
404 ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
405 smb->pdata->usb_hc_current_limit);
409 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
410 CFG_CURRENT_LIMIT_USB_MASK, ret);
418 static int smb347_set_voltage_limits(struct smb347_charger *smb)
422 if (smb->pdata->pre_to_fast_voltage) {
423 ret = smb->pdata->pre_to_fast_voltage;
426 ret = clamp_val(ret, 2400000, 3000000) - 2400000;
429 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
430 CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
431 ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
436 if (smb->pdata->max_charge_voltage) {
437 ret = smb->pdata->max_charge_voltage;
440 ret = clamp_val(ret, 3500000, 4500000) - 3500000;
443 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
444 CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
452 static int smb347_set_temp_limits(struct smb347_charger *smb)
454 bool enable_therm_monitor = false;
458 if (smb->pdata->chip_temp_threshold) {
459 val = smb->pdata->chip_temp_threshold;
462 val = clamp_val(val, 100, 130) - 100;
465 ret = regmap_update_bits(smb->regmap, CFG_OTG,
466 CFG_OTG_TEMP_THRESHOLD_MASK,
467 val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
472 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
473 val = smb->pdata->soft_cold_temp_limit;
475 val = clamp_val(val, 0, 15);
477 /* this goes from higher to lower so invert the value */
480 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
481 CFG_TEMP_LIMIT_SOFT_COLD_MASK,
482 val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
486 enable_therm_monitor = true;
489 if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
490 val = smb->pdata->soft_hot_temp_limit;
492 val = clamp_val(val, 40, 55) - 40;
495 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
496 CFG_TEMP_LIMIT_SOFT_HOT_MASK,
497 val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
501 enable_therm_monitor = true;
504 if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
505 val = smb->pdata->hard_cold_temp_limit;
507 val = clamp_val(val, -5, 10) + 5;
509 /* this goes from higher to lower so invert the value */
512 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
513 CFG_TEMP_LIMIT_HARD_COLD_MASK,
514 val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
518 enable_therm_monitor = true;
521 if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
522 val = smb->pdata->hard_hot_temp_limit;
524 val = clamp_val(val, 50, 65) - 50;
527 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
528 CFG_TEMP_LIMIT_HARD_HOT_MASK,
529 val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
533 enable_therm_monitor = true;
537 * If any of the temperature limits are set, we also enable the
538 * thermistor monitoring.
540 * When soft limits are hit, the device will start to compensate
541 * current and/or voltage depending on the configuration.
543 * When hard limit is hit, the device will suspend charging
544 * depending on the configuration.
546 if (enable_therm_monitor) {
547 ret = regmap_update_bits(smb->regmap, CFG_THERM,
548 CFG_THERM_MONITOR_DISABLED, 0);
553 if (smb->pdata->suspend_on_hard_temp_limit) {
554 ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
555 CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
560 if (smb->pdata->soft_temp_limit_compensation !=
561 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
562 val = smb->pdata->soft_temp_limit_compensation & 0x3;
564 ret = regmap_update_bits(smb->regmap, CFG_THERM,
565 CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
566 val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
570 ret = regmap_update_bits(smb->regmap, CFG_THERM,
571 CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
572 val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
577 if (smb->pdata->charge_current_compensation) {
578 val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
579 smb->pdata->charge_current_compensation);
583 ret = regmap_update_bits(smb->regmap, CFG_OTG,
584 CFG_OTG_CC_COMPENSATION_MASK,
585 (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
594 * smb347_set_writable - enables/disables writing to non-volatile registers
595 * @smb: pointer to smb347 charger instance
597 * You can enable/disable writing to the non-volatile configuration
598 * registers by calling this function.
600 * Returns %0 on success and negative errno in case of failure.
602 static int smb347_set_writable(struct smb347_charger *smb, bool writable)
604 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
605 writable ? CMD_A_ALLOW_WRITE : 0);
608 static int smb347_hw_init(struct smb347_charger *smb)
613 ret = smb347_set_writable(smb, true);
618 * Program the platform specific configuration values to the device
621 ret = smb347_set_charge_current(smb);
625 ret = smb347_set_current_limits(smb);
629 ret = smb347_set_voltage_limits(smb);
633 ret = smb347_set_temp_limits(smb);
637 /* If USB charging is disabled we put the USB in suspend mode */
638 if (!smb->pdata->use_usb) {
639 ret = regmap_update_bits(smb->regmap, CMD_A,
640 CMD_A_SUSPEND_ENABLED,
641 CMD_A_SUSPEND_ENABLED);
647 * If configured by platform data, we enable hardware Auto-OTG
648 * support for driving VBUS. Otherwise we disable it.
650 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
651 smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
656 * Make the charging functionality controllable by a write to the
657 * command register unless pin control is specified in the platform
660 switch (smb->pdata->enable_control) {
661 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
662 val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
664 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
665 val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
672 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
677 /* Disable Automatic Power Source Detection (APSD) interrupt. */
678 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
682 ret = smb347_update_ps_status(smb);
686 ret = smb347_start_stop_charging(smb);
689 smb347_set_writable(smb, false);
693 static irqreturn_t smb347_interrupt(int irq, void *data)
695 struct smb347_charger *smb = data;
696 unsigned int stat_c, irqstat_e, irqstat_c;
697 bool handled = false;
700 ret = regmap_read(smb->regmap, STAT_C, &stat_c);
702 dev_warn(smb->dev, "reading STAT_C failed\n");
706 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
708 dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
712 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
714 dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
719 * If we get charger error we report the error back to user and
722 if (stat_c & STAT_C_CHARGER_ERROR) {
723 dev_err(smb->dev, "error in charger, disabling charging\n");
725 smb347_charging_disable(smb);
726 power_supply_changed(&smb->battery);
731 * If we reached the termination current the battery is charged and
732 * we can update the status now. Charging is automatically
733 * disabled by the hardware.
735 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
736 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT)
737 power_supply_changed(&smb->battery);
742 * If we got an under voltage interrupt it means that AC/USB input
743 * was connected or disconnected.
745 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
746 if (smb347_update_ps_status(smb) > 0) {
747 smb347_start_stop_charging(smb);
748 if (smb->pdata->use_mains)
749 power_supply_changed(&smb->mains);
750 if (smb->pdata->use_usb)
751 power_supply_changed(&smb->usb);
756 return handled ? IRQ_HANDLED : IRQ_NONE;
759 static int smb347_irq_set(struct smb347_charger *smb, bool enable)
763 ret = smb347_set_writable(smb, true);
768 * Enable/disable interrupts for:
770 * - termination current reached
773 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
774 enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
778 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
779 enable ? CFG_STATUS_IRQ_TERMINATION_OR_TAPER : 0);
783 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
784 enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
786 smb347_set_writable(smb, false);
790 static inline int smb347_irq_enable(struct smb347_charger *smb)
792 return smb347_irq_set(smb, true);
795 static inline int smb347_irq_disable(struct smb347_charger *smb)
797 return smb347_irq_set(smb, false);
800 static int smb347_irq_init(struct smb347_charger *smb,
801 struct i2c_client *client)
803 const struct smb347_charger_platform_data *pdata = smb->pdata;
804 int ret, irq = gpio_to_irq(pdata->irq_gpio);
806 ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
810 ret = request_threaded_irq(irq, NULL, smb347_interrupt,
811 IRQF_TRIGGER_FALLING, client->name, smb);
815 ret = smb347_set_writable(smb, true);
820 * Configure the STAT output to be suitable for interrupts: disable
821 * all other output (except interrupts) and make it active low.
823 ret = regmap_update_bits(smb->regmap, CFG_STAT,
824 CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
829 smb347_set_writable(smb, false);
834 smb347_set_writable(smb, false);
838 gpio_free(pdata->irq_gpio);
844 static int smb347_mains_get_property(struct power_supply *psy,
845 enum power_supply_property prop,
846 union power_supply_propval *val)
848 struct smb347_charger *smb =
849 container_of(psy, struct smb347_charger, mains);
851 if (prop == POWER_SUPPLY_PROP_ONLINE) {
852 val->intval = smb->mains_online;
858 static enum power_supply_property smb347_mains_properties[] = {
859 POWER_SUPPLY_PROP_ONLINE,
862 static int smb347_usb_get_property(struct power_supply *psy,
863 enum power_supply_property prop,
864 union power_supply_propval *val)
866 struct smb347_charger *smb =
867 container_of(psy, struct smb347_charger, usb);
869 if (prop == POWER_SUPPLY_PROP_ONLINE) {
870 val->intval = smb->usb_online;
876 static enum power_supply_property smb347_usb_properties[] = {
877 POWER_SUPPLY_PROP_ONLINE,
880 static int smb347_battery_get_property(struct power_supply *psy,
881 enum power_supply_property prop,
882 union power_supply_propval *val)
884 struct smb347_charger *smb =
885 container_of(psy, struct smb347_charger, battery);
886 const struct smb347_charger_platform_data *pdata = smb->pdata;
889 ret = smb347_update_ps_status(smb);
894 case POWER_SUPPLY_PROP_STATUS:
895 if (!smb347_is_ps_online(smb)) {
896 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
899 if (smb347_charging_status(smb))
900 val->intval = POWER_SUPPLY_STATUS_CHARGING;
902 val->intval = POWER_SUPPLY_STATUS_FULL;
905 case POWER_SUPPLY_PROP_CHARGE_TYPE:
906 if (!smb347_is_ps_online(smb))
910 * We handle trickle and pre-charging the same, and taper
913 switch (smb347_charging_status(smb)) {
915 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
918 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
921 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
926 case POWER_SUPPLY_PROP_TECHNOLOGY:
927 val->intval = pdata->battery_info.technology;
930 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
931 val->intval = pdata->battery_info.voltage_min_design;
934 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
935 val->intval = pdata->battery_info.voltage_max_design;
938 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
939 val->intval = pdata->battery_info.charge_full_design;
942 case POWER_SUPPLY_PROP_MODEL_NAME:
943 val->strval = pdata->battery_info.name;
953 static enum power_supply_property smb347_battery_properties[] = {
954 POWER_SUPPLY_PROP_STATUS,
955 POWER_SUPPLY_PROP_CHARGE_TYPE,
956 POWER_SUPPLY_PROP_TECHNOLOGY,
957 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
958 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
959 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
960 POWER_SUPPLY_PROP_MODEL_NAME,
963 static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
980 static bool smb347_readable_reg(struct device *dev, unsigned int reg)
983 case CFG_CHARGE_CURRENT:
984 case CFG_CURRENT_LIMIT:
985 case CFG_FLOAT_VOLTAGE:
1002 return smb347_volatile_reg(dev, reg);
1005 static const struct regmap_config smb347_regmap = {
1008 .max_register = SMB347_MAX_REGISTER,
1009 .volatile_reg = smb347_volatile_reg,
1010 .readable_reg = smb347_readable_reg,
1013 static int smb347_probe(struct i2c_client *client,
1014 const struct i2c_device_id *id)
1016 static char *battery[] = { "smb347-battery" };
1017 const struct smb347_charger_platform_data *pdata;
1018 struct device *dev = &client->dev;
1019 struct smb347_charger *smb;
1022 pdata = dev->platform_data;
1026 if (!pdata->use_mains && !pdata->use_usb)
1029 smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
1033 i2c_set_clientdata(client, smb);
1035 mutex_init(&smb->lock);
1036 smb->dev = &client->dev;
1039 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
1040 if (IS_ERR(smb->regmap))
1041 return PTR_ERR(smb->regmap);
1043 ret = smb347_hw_init(smb);
1047 if (smb->pdata->use_mains) {
1048 smb->mains.name = "smb347-mains";
1049 smb->mains.type = POWER_SUPPLY_TYPE_MAINS;
1050 smb->mains.get_property = smb347_mains_get_property;
1051 smb->mains.properties = smb347_mains_properties;
1052 smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties);
1053 smb->mains.supplied_to = battery;
1054 smb->mains.num_supplicants = ARRAY_SIZE(battery);
1055 ret = power_supply_register(dev, &smb->mains);
1060 if (smb->pdata->use_usb) {
1061 smb->usb.name = "smb347-usb";
1062 smb->usb.type = POWER_SUPPLY_TYPE_USB;
1063 smb->usb.get_property = smb347_usb_get_property;
1064 smb->usb.properties = smb347_usb_properties;
1065 smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties);
1066 smb->usb.supplied_to = battery;
1067 smb->usb.num_supplicants = ARRAY_SIZE(battery);
1068 ret = power_supply_register(dev, &smb->usb);
1070 if (smb->pdata->use_mains)
1071 power_supply_unregister(&smb->mains);
1076 smb->battery.name = "smb347-battery";
1077 smb->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1078 smb->battery.get_property = smb347_battery_get_property;
1079 smb->battery.properties = smb347_battery_properties;
1080 smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties);
1083 ret = power_supply_register(dev, &smb->battery);
1085 if (smb->pdata->use_usb)
1086 power_supply_unregister(&smb->usb);
1087 if (smb->pdata->use_mains)
1088 power_supply_unregister(&smb->mains);
1093 * Interrupt pin is optional. If it is connected, we setup the
1094 * interrupt support here.
1096 if (pdata->irq_gpio >= 0) {
1097 ret = smb347_irq_init(smb, client);
1099 dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
1100 dev_warn(dev, "disabling IRQ support\n");
1102 smb347_irq_enable(smb);
1109 static int smb347_remove(struct i2c_client *client)
1111 struct smb347_charger *smb = i2c_get_clientdata(client);
1114 smb347_irq_disable(smb);
1115 free_irq(client->irq, smb);
1116 gpio_free(smb->pdata->irq_gpio);
1119 power_supply_unregister(&smb->battery);
1120 if (smb->pdata->use_usb)
1121 power_supply_unregister(&smb->usb);
1122 if (smb->pdata->use_mains)
1123 power_supply_unregister(&smb->mains);
1127 static const struct i2c_device_id smb347_id[] = {
1131 MODULE_DEVICE_TABLE(i2c, smb347_id);
1133 static struct i2c_driver smb347_driver = {
1137 .probe = smb347_probe,
1138 .remove = __devexit_p(smb347_remove),
1139 .id_table = smb347_id,
1142 module_i2c_driver(smb347_driver);
1144 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
1145 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1146 MODULE_DESCRIPTION("SMB347 battery charger driver");
1147 MODULE_LICENSE("GPL");
1148 MODULE_ALIAS("i2c:smb347");