123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419 |
- /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 and
- * only version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- #define pr_fmt(fmt) "%s: " fmt, __func__
- #include <linux/i2c.h>
- #include <linux/module.h>
- #include <linux/mutex.h>
- #include <linux/of.h>
- #include <linux/of_device.h>
- #include <linux/power_supply.h>
- #include <linux/slab.h>
- struct smb137c_chip {
- struct i2c_client *client;
- struct power_supply psy;
- struct power_supply *usb_psy;
- struct mutex lock;
- int charge_current_limit_ua;
- int input_current_limit_ua;
- int term_current_ua;
- bool charging_enabled;
- bool otg_mode_enabled;
- bool charging_allowed;
- bool usb_suspend_enabled;
- };
- struct input_current_config {
- int current_limit_ua;
- u8 cmd_b_reg;
- u8 var_func_reg;
- u8 input_cur_reg;
- };
- struct term_current_config {
- int term_current_ua;
- u8 charge_cur_reg;
- };
- #define INPUT_CURRENT(_current_limit_ua, _cmd_b_reg, _var_func_reg, \
- _input_cur_reg) \
- { \
- .current_limit_ua = _current_limit_ua, \
- .cmd_b_reg = _cmd_b_reg, \
- .var_func_reg = _var_func_reg, \
- .input_cur_reg = _input_cur_reg, \
- }
- #define CHARGE_CURRENT_REG 0x00
- #define CHARGE_CURRENT_FAST_CHG_MASK 0xE0
- #define CHARGE_CURRENT_FAST_CHG_SHIFT 5
- #define CHARGE_CURRENT_PRE_CHG_MASK 0x18
- #define CHARGE_CURRENT_PRE_CHG_SHIFT 3
- #define CHARGE_CURRENT_TERM_CUR_MASK 0x06
- #define INPUT_CURRENT_REG 0x01
- #define INPUT_CURRENT_LIMIT_MASK 0xE0
- #define FLOAT_VOLTAGE_REG 0x02
- #define FLOAT_VOLTAGE_MASK 0x7F
- #define FLOAT_VOLTAGE_SHIFT 0
- #define CTRL_A_REG 0x03
- #define CTRL_A_AUTO_RECHARGE_MASK 0x80
- #define CTRL_A_AUTO_RECHARGE_ENABLED 0x00
- #define CTRL_A_AUTO_RECHARGE_DISABLED 0x80
- #define CTRL_A_TERM_CUR_MASK 0x40
- #define CTRL_A_TERM_CUR_ENABLED 0x00
- #define CTRL_A_TERM_CUR_DISABLED 0x40
- #define CTRL_A_THRESH_VOLTAGE_MASK 0x38
- #define CTRL_A_THRESH_VOLTAGE_SHIFT 3
- #define CTRL_A_VOUTL_MASK 0x02
- #define CTRL_A_VOUTL_4250MV 0x00
- #define CTRL_A_VOUTL_4460MV 0x02
- #define CTRL_A_THERM_MONITOR_MASK 0x01
- #define CTRL_A_THERM_MONITOR_ENABLED 0x01
- #define CTRL_A_THERM_MONITOR_DISABLED 0x00
- #define PIN_CTRL_REG 0x05
- #define PIN_CTRL_DEAD_BATT_CHG_MASK 0x80
- #define PIN_CTRL_DEAD_BATT_CHG_ENABLED 0x80
- #define PIN_CTRL_DEAD_BATT_CHG_DISABLED 0x00
- #define PIN_CTRL_OTG_LBR_MASK 0x20
- #define PIN_CTRL_OTG 0x00
- #define PIN_CTRL_LBR 0x20
- #define PIN_CTRL_USB_CUR_LIMIT_MASK 0x10
- #define PIN_CTRL_USB_CUR_LIMIT_REG 0x00
- #define PIN_CTRL_USB_CUR_LIMIT_PIN 0x10
- #define PIN_CTRL_CHG_EN_MASK 0x0C
- #define PIN_CTRL_CHG_EN_REG_LOW 0x00
- #define PIN_CTRL_CHG_EN_REG_HIGH 0x04
- #define PIN_CTRL_CHG_EN_PIN_LOW 0x08
- #define PIN_CTRL_CHG_EN_PIN_HIGH 0x0C
- #define PIN_CTRL_OTG_CTRL_MASK 0x02
- #define PIN_CTRL_OTG_CTRL_REG 0x00
- #define PIN_CTRL_OTG_CTRL_PIN 0x02
- #define OTG_CTRL_REG 0x06
- #define OTG_CTRL_BMD_MASK 0x80
- #define OTG_CTRL_BMD_ENABLED 0x80
- #define OTG_CTRL_BMD_DISABLED 0x00
- #define OTG_CTRL_AUTO_RECHARGE_MASK 0x40
- #define OTG_CTRL_AUTO_RECHARGE_75MV 0x00
- #define OTG_CTRL_AUTO_RECHARGE_120MV 0x40
- #define TEMP_MON_REG 0x08
- #define TEMP_MON_THERM_CURRENT_MASK 0xC0
- #define TEMP_MON_THERM_CURRENT_SHIFT 6
- #define TEMP_MON_TEMP_LOW_MASK 0x38
- #define TEMP_MON_TEMP_LOW_SHIFT 3
- #define TEMP_MON_TEMP_HIGH_MASK 0x07
- #define TEMP_MON_TEMP_HIGH_SHIFT 0
- #define SAFETY_TIMER_REG 0x09
- #define SAFETY_TIMER_RELOAD_MASK 0x40
- #define SAFETY_TIMER_RELOAD_ENABLED 0x40
- #define SAFETY_TIMER_RELOAD_DISABLED 0x00
- #define SAFETY_TIMER_CHG_TIMEOUT_MASK 0x0C
- #define SAFETY_TIMER_CHG_TIMEOUT_SHIFT 2
- #define SAFETY_TIMER_PRE_CHG_TIME_MASK 0x03
- #define SAFETY_TIMER_PRE_CHG_TIME_SHIFT 0
- #define VAR_FUNC_REG 0x0C
- #define VAR_FUNC_USB_MODE_MASK 0x80
- #define VAR_FUNC_USB_SUSPEND_CTRL_MASK 0x20
- #define VAR_FUNC_USB_SUSPEND_CTRL_REG 0x00
- #define VAR_FUNC_USB_SUSPEND_CTRL_PIN 0x20
- #define VAR_FUNC_BMD_MASK 0x0C
- #define VAR_FUNC_BMD_DISABLED 0x00
- #define VAR_FUNC_BMD_ALGO_PERIODIC 0x04
- #define VAR_FUNC_BMD_ALGO 0x08
- #define VAR_FUNC_BMD_THERM 0x0C
- #define CMD_A_REG 0x30
- #define CMD_A_VOLATILE_WRITE_MASK 0x80
- #define CMD_A_VOLATILE_WRITE_ALLOW 0x80
- #define CMD_A_VOLATILE_WRITE_DISALLOW 0x00
- #define CMD_A_FAST_CHG_MASK 0x40
- #define CMD_A_FAST_CHG_ALLOW 0x40
- #define CMD_A_FAST_CHG_DISALLOW 0x00
- #define CMD_A_OTG_MASK 0x10
- #define CMD_A_OTG_ENABLED 0x10
- #define CMD_A_OTG_DISABLED 0x00
- #define CMD_A_USB_SUSPEND_MASK 0x04
- #define CMD_A_USB_SUSPEND_DISABLED 0x00
- #define CMD_A_USB_SUSPEND_ENABLED 0x04
- #define CMD_A_CHARGING_MASK 0x02
- #define CMD_A_CHARGING_ENABLED 0x00
- #define CMD_A_CHARGING_DISABLED 0x02
- #define CMD_B_REG 0x31
- #define CMD_B_USB_MODE_MASK 0x03
- #define DEV_ID_REG 0x33
- #define DEV_ID_PART_MASK 0x80
- #define DEV_ID_PART_SMB137C 0x00
- #define DEV_ID_GUI_REV_MASK 0x70
- #define DEV_ID_GUI_REV_SHIFT 4
- #define DEV_ID_SILICON_REV_MASK 0x0F
- #define DEV_ID_SILICON_REV_SHIFT 0
- #define IRQ_STAT_A_REG 0x35
- #define IRQ_STAT_A_BATT_HOT 0x40
- #define IRQ_STAT_A_BATT_COLD 0x10
- #define IRQ_STAT_B_REG 0x36
- #define IRQ_STAT_B_BATT_OVERVOLT 0x40
- #define IRQ_STAT_B_BATT_MISSING 0x10
- #define IRQ_STAT_B_BATT_UNDERVOLT 0x04
- #define STAT_C_REG 0x3D
- #define STAT_C_CHG_ERROR 0x40
- #define STAT_C_VBATT_LEVEL_BELOW_2P1V 0x10
- #define STAT_C_CHG_STAT_MASK 0x06
- #define STAT_C_CHG_STAT_SHIFT 1
- #define STAT_C_CHG_ENABLED 0x01
- /* Charge status register values */
- enum smb137c_charge_status {
- CHARGE_STAT_NO_CHG = 0,
- CHARGE_STAT_PRE_CHG = 1,
- CHARGE_STAT_FAST_CHG = 2,
- CHARGE_STAT_TAPER_CHG = 3,
- };
- #define PRE_CHARGE_CURRENT_MIN_UA 50000
- #define PRE_CHARGE_CURRENT_MAX_UA 200000
- #define PRE_CHARGE_CURRENT_STEP_UA 50000
- #define FLOAT_VOLTAGE_MIN_UV 3460000
- #define FLOAT_VOLTAGE_MAX_UV 4730000
- #define FLOAT_VOLTAGE_STEP_UV 10000
- #define PRE_CHG_THRESH_VOLTAGE_MIN_UV 2400000
- #define PRE_CHG_THRESH_VOLTAGE_MAX_UV 3100000
- #define PRE_CHG_THRESH_VOLTAGE_STEP_UV 100000
- #define USB_MIN_CURRENT_UA 100000
- static int smb137c_read_reg(struct smb137c_chip *chip, u8 reg, u8 *val)
- {
- int rc;
- rc = i2c_smbus_read_byte_data(chip->client, reg);
- if (rc < 0) {
- pr_err("i2c_smbus_read_byte_data failed. reg=0x%02X, rc=%d\n",
- reg, rc);
- } else {
- *val = rc;
- rc = 0;
- pr_debug("read(0x%02X)=0x%02X\n", reg, *val);
- }
- return rc;
- }
- static int smb137c_write_reg(struct smb137c_chip *chip, u8 reg, u8 val)
- {
- int rc;
- rc = i2c_smbus_write_byte_data(chip->client, reg, val);
- if (rc < 0)
- pr_err("i2c_smbus_write_byte_data failed. reg=0x%02X, rc=%d\n",
- reg, rc);
- else
- pr_debug("write(0x%02X)=0x%02X\n", reg, val);
- return rc;
- }
- static int smb137c_masked_write_reg(struct smb137c_chip *chip, u8 reg, u8 mask,
- u8 val)
- {
- u8 reg_val;
- int rc;
- pr_debug("masked write(0x%02X), mask=0x%02X, value=0x%02X\n", reg, mask,
- val);
- rc = smb137c_read_reg(chip, reg, ®_val);
- if (rc < 0)
- return rc;
- val = (reg_val & ~mask) | (val & mask);
- if (val != reg_val)
- rc = smb137c_write_reg(chip, reg, val);
- return rc;
- }
- static int smb137c_enable_charging(struct smb137c_chip *chip)
- {
- int rc = 0;
- chip->charging_allowed = true;
- if (!chip->charging_enabled && chip->charge_current_limit_ua > 0) {
- rc = smb137c_masked_write_reg(chip, CMD_A_REG,
- CMD_A_CHARGING_MASK, CMD_A_CHARGING_ENABLED);
- if (!rc)
- chip->charging_enabled = true;
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s\n", __func__);
- return rc;
- }
- static int smb137c_disable_charging(struct smb137c_chip *chip)
- {
- int rc = 0;
- chip->charging_allowed = false;
- if (chip->charging_enabled) {
- rc = smb137c_masked_write_reg(chip, CMD_A_REG,
- CMD_A_CHARGING_MASK, CMD_A_CHARGING_DISABLED);
- if (!rc)
- chip->charging_enabled = false;
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s\n", __func__);
- return rc;
- }
- static int smb137c_enable_otg_mode(struct smb137c_chip *chip)
- {
- int rc = 0;
- if (!chip->otg_mode_enabled) {
- rc = smb137c_masked_write_reg(chip, CMD_A_REG, CMD_A_OTG_MASK,
- CMD_A_OTG_ENABLED);
- if (!rc)
- chip->otg_mode_enabled = true;
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s\n", __func__);
- return rc;
- }
- static int smb137c_disable_otg_mode(struct smb137c_chip *chip)
- {
- int rc = 0;
- if (chip->otg_mode_enabled) {
- rc = smb137c_masked_write_reg(chip, CMD_A_REG, CMD_A_OTG_MASK,
- CMD_A_OTG_DISABLED);
- if (!rc)
- chip->otg_mode_enabled = false;
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s\n", __func__);
- return rc;
- }
- static int smb137c_enable_usb_suspend(struct smb137c_chip *chip)
- {
- int rc = 0;
- if (!chip->usb_suspend_enabled) {
- rc = smb137c_masked_write_reg(chip, CMD_A_REG,
- CMD_A_USB_SUSPEND_MASK, CMD_A_USB_SUSPEND_ENABLED);
- if (!rc)
- chip->usb_suspend_enabled = true;
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s\n", __func__);
- return rc;
- }
- static int smb137c_disable_usb_suspend(struct smb137c_chip *chip)
- {
- int rc = 0;
- if (chip->input_current_limit_ua > 0 && chip->usb_suspend_enabled) {
- rc = smb137c_masked_write_reg(chip, CMD_A_REG,
- CMD_A_USB_SUSPEND_MASK, CMD_A_USB_SUSPEND_DISABLED);
- if (!rc)
- chip->usb_suspend_enabled = false;
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s\n", __func__);
- return rc;
- }
- static struct input_current_config supported_input_current[] = {
- INPUT_CURRENT(100000, 0x00, 0x00, 0x00),
- INPUT_CURRENT(150000, 0x00, 0x80, 0x00),
- INPUT_CURRENT(500000, 0x02, 0x00, 0x00),
- INPUT_CURRENT(700000, 0x01, 0x00, 0x00),
- INPUT_CURRENT(800000, 0x01, 0x00, 0x20),
- INPUT_CURRENT(900000, 0x01, 0x00, 0x40),
- INPUT_CURRENT(1000000, 0x01, 0x00, 0x60),
- INPUT_CURRENT(1100000, 0x01, 0x00, 0x80),
- INPUT_CURRENT(1200000, 0x01, 0x00, 0xA0),
- INPUT_CURRENT(1300000, 0x01, 0x00, 0xC0),
- INPUT_CURRENT(1500000, 0x01, 0x00, 0xE0),
- };
- static int smb137c_set_usb_input_current_limit(struct smb137c_chip *chip,
- int current_limit_ua)
- {
- struct input_current_config *config = NULL;
- int rc = 0;
- int i;
- for (i = ARRAY_SIZE(supported_input_current) - 1; i >= 0; i--) {
- if (current_limit_ua
- >= supported_input_current[i].current_limit_ua) {
- config = &supported_input_current[i];
- break;
- }
- }
- if (config) {
- if (chip->input_current_limit_ua != config->current_limit_ua) {
- rc = smb137c_masked_write_reg(chip, INPUT_CURRENT_REG,
- INPUT_CURRENT_LIMIT_MASK, config->input_cur_reg);
- if (rc)
- return rc;
- rc = smb137c_masked_write_reg(chip, VAR_FUNC_REG,
- VAR_FUNC_USB_MODE_MASK, config->var_func_reg);
- if (rc)
- return rc;
- rc = smb137c_masked_write_reg(chip, CMD_B_REG,
- CMD_B_USB_MODE_MASK, config->cmd_b_reg);
- if (rc)
- return rc;
- chip->input_current_limit_ua = config->current_limit_ua;
- }
- rc = smb137c_disable_usb_suspend(chip);
- } else {
- chip->input_current_limit_ua = 0;
- rc = smb137c_enable_usb_suspend(chip);
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
- chip->input_current_limit_ua);
- return rc;
- }
- static int fast_charge_current_ua[] = {
- 500000,
- 650000,
- 750000,
- 850000,
- 950000,
- 1100000,
- 1300000,
- 1500000,
- };
- static int smb137c_set_charge_current_limit(struct smb137c_chip *chip,
- int current_limit_ua)
- {
- int fast_charge_limit_ua = 0;
- int rc = 0;
- u8 val = 0;
- int i;
- for (i = ARRAY_SIZE(fast_charge_current_ua) - 1; i >= 0; i--) {
- if (current_limit_ua >= fast_charge_current_ua[i]) {
- val = i << CHARGE_CURRENT_FAST_CHG_SHIFT;
- fast_charge_limit_ua = fast_charge_current_ua[i];
- break;
- }
- }
- if (fast_charge_limit_ua
- && chip->charge_current_limit_ua != fast_charge_limit_ua)
- rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
- CHARGE_CURRENT_FAST_CHG_MASK, val);
- else if (fast_charge_limit_ua == 0)
- rc = smb137c_disable_charging(chip);
- chip->charge_current_limit_ua = fast_charge_limit_ua;
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
- fast_charge_limit_ua);
- return rc;
- }
- static int smb137c_get_charge_current_limit(struct smb137c_chip *chip)
- {
- int fast_charge_limit_ua = 0;
- u8 val = 0;
- int rc, i;
- rc = smb137c_read_reg(chip, CHARGE_CURRENT_REG, &val);
- if (rc)
- return rc;
- i = (val & CHARGE_CURRENT_FAST_CHG_MASK)
- >> CHARGE_CURRENT_FAST_CHG_SHIFT;
- if (i >= 0 && i < ARRAY_SIZE(fast_charge_current_ua))
- fast_charge_limit_ua = fast_charge_current_ua[i];
- dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
- fast_charge_limit_ua);
- return fast_charge_limit_ua;
- }
- static struct term_current_config term_current_ua[] = {
- { 35000, 0x06},
- { 50000, 0x00},
- {100000, 0x02},
- {150000, 0x04},
- };
- static int smb137c_set_term_current(struct smb137c_chip *chip,
- int current_limit_ua)
- {
- int term_current_limit_ua = 0;
- int rc = 0;
- u8 val = 0;
- int i;
- for (i = ARRAY_SIZE(term_current_ua) - 1; i >= 0; i--) {
- if (current_limit_ua >= term_current_ua[i].term_current_ua) {
- val = term_current_ua[i].charge_cur_reg;
- term_current_limit_ua
- = term_current_ua[i].term_current_ua;
- break;
- }
- }
- if (term_current_limit_ua) {
- rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
- CHARGE_CURRENT_TERM_CUR_MASK, val);
- if (rc)
- return rc;
- rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
- CTRL_A_TERM_CUR_MASK, CTRL_A_TERM_CUR_ENABLED);
- } else {
- rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
- CTRL_A_TERM_CUR_MASK, CTRL_A_TERM_CUR_DISABLED);
- }
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
- term_current_limit_ua);
- return rc;
- }
- static int smb137c_set_pre_charge_current_limit(struct smb137c_chip *chip,
- int current_limit_ua)
- {
- int setpoint, rc;
- u8 val;
- if (current_limit_ua < PRE_CHARGE_CURRENT_MIN_UA ||
- current_limit_ua > PRE_CHARGE_CURRENT_MAX_UA) {
- dev_err(&chip->client->dev, "%s: current limit out of bounds: %d\n",
- __func__, current_limit_ua);
- return -EINVAL;
- }
- setpoint = (current_limit_ua - PRE_CHARGE_CURRENT_MIN_UA)
- / PRE_CHARGE_CURRENT_STEP_UA;
- val = setpoint << CHARGE_CURRENT_PRE_CHG_SHIFT;
- rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
- CHARGE_CURRENT_PRE_CHG_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
- setpoint * PRE_CHARGE_CURRENT_STEP_UA
- + PRE_CHARGE_CURRENT_MIN_UA);
- return rc;
- }
- static int smb137c_set_float_voltage(struct smb137c_chip *chip, int voltage_uv)
- {
- int setpoint, rc;
- u8 val;
- if (voltage_uv < FLOAT_VOLTAGE_MIN_UV ||
- voltage_uv > FLOAT_VOLTAGE_MAX_UV) {
- dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
- __func__, voltage_uv);
- return -EINVAL;
- }
- setpoint = (voltage_uv - FLOAT_VOLTAGE_MIN_UV) / FLOAT_VOLTAGE_STEP_UV;
- val = setpoint << FLOAT_VOLTAGE_SHIFT;
- rc = smb137c_masked_write_reg(chip, FLOAT_VOLTAGE_REG,
- FLOAT_VOLTAGE_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
- setpoint * FLOAT_VOLTAGE_STEP_UV + FLOAT_VOLTAGE_MIN_UV);
- return rc;
- }
- static int smb137c_set_pre_charge_threshold_voltage(struct smb137c_chip *chip,
- int voltage_uv)
- {
- int setpoint, rc;
- u8 val;
- if (voltage_uv < PRE_CHG_THRESH_VOLTAGE_MIN_UV ||
- voltage_uv > PRE_CHG_THRESH_VOLTAGE_MAX_UV) {
- dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
- __func__, voltage_uv);
- return -EINVAL;
- }
- setpoint = (voltage_uv - PRE_CHG_THRESH_VOLTAGE_MIN_UV)
- / PRE_CHG_THRESH_VOLTAGE_STEP_UV;
- val = setpoint << CTRL_A_THRESH_VOLTAGE_SHIFT;
- rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
- CTRL_A_THRESH_VOLTAGE_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
- setpoint * PRE_CHG_THRESH_VOLTAGE_STEP_UV
- + PRE_CHG_THRESH_VOLTAGE_MIN_UV);
- return rc;
- }
- static int smb137c_set_recharge_threshold_voltage(struct smb137c_chip *chip,
- int voltage_uv)
- {
- int rc;
- u8 val;
- if (voltage_uv == 75000) {
- val = OTG_CTRL_AUTO_RECHARGE_75MV;
- } else if (voltage_uv == 120000) {
- val = OTG_CTRL_AUTO_RECHARGE_120MV;
- } else {
- dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
- __func__, voltage_uv);
- return -EINVAL;
- }
- rc = smb137c_masked_write_reg(chip, OTG_CTRL_REG,
- OTG_CTRL_AUTO_RECHARGE_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
- voltage_uv);
- return rc;
- }
- static int smb137c_set_system_voltage(struct smb137c_chip *chip, int voltage_uv)
- {
- int rc;
- u8 val;
- if (voltage_uv == 4250000) {
- val = CTRL_A_VOUTL_4250MV;
- } else if (voltage_uv == 4460000) {
- val = CTRL_A_VOUTL_4460MV;
- } else {
- dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
- __func__, voltage_uv);
- return -EINVAL;
- }
- rc = smb137c_masked_write_reg(chip, CTRL_A_REG, CTRL_A_VOUTL_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
- voltage_uv);
- return rc;
- }
- static int charging_timeout[] = {
- 382,
- 764,
- 1527,
- };
- static int smb137c_set_charging_timeout(struct smb137c_chip *chip, int timeout)
- {
- int timeout_chosen = 0;
- u8 val = 3 << SAFETY_TIMER_CHG_TIMEOUT_SHIFT;
- int rc, i;
- for (i = ARRAY_SIZE(charging_timeout) - 1; i >= 0; i--) {
- if (timeout >= charging_timeout[i]) {
- val = i << SAFETY_TIMER_CHG_TIMEOUT_SHIFT;
- timeout_chosen = charging_timeout[i];
- break;
- }
- }
- rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
- SAFETY_TIMER_CHG_TIMEOUT_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: timeout=%d min\n", __func__,
- timeout_chosen);
- return rc;
- }
- static int pre_charge_timeout[] = {
- 48,
- 95,
- 191,
- };
- static int smb137c_set_pre_charge_timeout(struct smb137c_chip *chip,
- int timeout)
- {
- int timeout_chosen = 0;
- u8 val = 3 << SAFETY_TIMER_PRE_CHG_TIME_SHIFT;
- int rc, i;
- for (i = ARRAY_SIZE(pre_charge_timeout) - 1; i >= 0; i--) {
- if (timeout >= pre_charge_timeout[i]) {
- val = i << SAFETY_TIMER_PRE_CHG_TIME_SHIFT;
- timeout_chosen = pre_charge_timeout[i];
- break;
- }
- }
- rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
- SAFETY_TIMER_PRE_CHG_TIME_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: timeout=%d min\n", __func__,
- timeout_chosen);
- return rc;
- }
- static int thermistor_current[] = {
- 100,
- 40,
- 20,
- 10,
- };
- static int smb137c_set_thermistor_current(struct smb137c_chip *chip,
- int current_ua)
- {
- bool found = false;
- u8 val = 0;
- int rc, i;
- for (i = 0; i < ARRAY_SIZE(thermistor_current); i++) {
- if (current_ua == thermistor_current[i]) {
- found = true;
- val = i << TEMP_MON_THERM_CURRENT_SHIFT;
- }
- }
- if (!found) {
- dev_err(&chip->client->dev, "%s: current out of bounds: %d\n",
- __func__, current_ua);
- return -EINVAL;
- }
- rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
- TEMP_MON_THERM_CURRENT_MASK, val);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
- current_ua);
- return 0;
- }
- static int smb137c_set_temperature_low_limit(struct smb137c_chip *chip,
- int value)
- {
- int rc;
- if (value < 0 || value > 7) {
- dev_err(&chip->client->dev, "%s: temperature value out of bounds: %d\n",
- __func__, value);
- return -EINVAL;
- }
- rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
- TEMP_MON_TEMP_LOW_MASK, value << TEMP_MON_TEMP_LOW_SHIFT);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: temperature value=%d\n",
- __func__, value);
- return rc;
- }
- static int smb137c_set_temperature_high_limit(struct smb137c_chip *chip,
- int value)
- {
- int rc;
- if (value < 0 || value > 7) {
- dev_err(&chip->client->dev, "%s: temperature value out of bounds: %d\n",
- __func__, value);
- return -EINVAL;
- }
- rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
- TEMP_MON_TEMP_HIGH_MASK, value << TEMP_MON_TEMP_HIGH_SHIFT);
- if (!rc)
- dev_dbg(&chip->client->dev, "%s: temperature value=%d\n",
- __func__, value);
- return rc;
- }
- static int charge_status_type_map[] = {
- [CHARGE_STAT_NO_CHG] = POWER_SUPPLY_CHARGE_TYPE_NONE,
- [CHARGE_STAT_PRE_CHG] = POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
- [CHARGE_STAT_FAST_CHG] = POWER_SUPPLY_CHARGE_TYPE_FAST,
- [CHARGE_STAT_TAPER_CHG] = POWER_SUPPLY_CHARGE_TYPE_FAST,
- };
- static const char * const charge_status_name[] = {
- [CHARGE_STAT_NO_CHG] = "none",
- [CHARGE_STAT_PRE_CHG] = "pre-charge",
- [CHARGE_STAT_FAST_CHG] = "fast-charge",
- [CHARGE_STAT_TAPER_CHG] = "taper-charge",
- };
- static int smb137c_get_property_status(struct smb137c_chip *chip)
- {
- int status = POWER_SUPPLY_STATUS_DISCHARGING;
- enum smb137c_charge_status charging_status;
- bool charging_enabled;
- bool charging_error;
- int rc;
- u8 val;
- rc = smb137c_read_reg(chip, STAT_C_REG, &val);
- if (rc)
- return POWER_SUPPLY_STATUS_UNKNOWN;
- charging_enabled = val & STAT_C_CHG_ENABLED;
- charging_error = val & STAT_C_CHG_ERROR;
- charging_status = (val & STAT_C_CHG_STAT_MASK) >> STAT_C_CHG_STAT_SHIFT;
- if (charging_enabled && !charging_error
- && charging_status != CHARGE_STAT_NO_CHG)
- status = POWER_SUPPLY_STATUS_CHARGING;
- dev_dbg(&chip->client->dev, "%s: status=%s\n", __func__,
- (status == POWER_SUPPLY_STATUS_CHARGING ? "charging"
- : "discharging"));
- return status;
- }
- static int smb137c_get_property_battery_present(struct smb137c_chip *chip)
- {
- int rc;
- u8 val;
- rc = smb137c_read_reg(chip, IRQ_STAT_B_REG, &val);
- if (rc || (val & IRQ_STAT_B_BATT_MISSING))
- return 0;
- /* Treat battery voltage less than 2.1 V as battery not present. */
- rc = smb137c_read_reg(chip, STAT_C_REG, &val);
- if (rc || (val & STAT_C_VBATT_LEVEL_BELOW_2P1V))
- return 0;
- return 1;
- }
- static int smb137c_get_property_battery_health(struct smb137c_chip *chip)
- {
- int rc;
- u8 val;
- /* The health of a disconnected battery is unknown. */
- if (!smb137c_get_property_battery_present(chip))
- return POWER_SUPPLY_HEALTH_UNKNOWN;
- rc = smb137c_read_reg(chip, IRQ_STAT_B_REG, &val);
- if (rc)
- return POWER_SUPPLY_HEALTH_UNKNOWN;
- if (val & IRQ_STAT_B_BATT_OVERVOLT)
- return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
- else if (val & IRQ_STAT_B_BATT_UNDERVOLT)
- return POWER_SUPPLY_HEALTH_DEAD;
- rc = smb137c_read_reg(chip, IRQ_STAT_A_REG, &val);
- if (rc)
- return POWER_SUPPLY_HEALTH_UNKNOWN;
- if (val & IRQ_STAT_A_BATT_HOT)
- return POWER_SUPPLY_HEALTH_OVERHEAT;
- else if (val & IRQ_STAT_A_BATT_COLD)
- return POWER_SUPPLY_HEALTH_COLD;
- return POWER_SUPPLY_HEALTH_GOOD;
- }
- static int smb137c_get_property_charge_type(struct smb137c_chip *chip)
- {
- enum smb137c_charge_status status;
- int charge_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
- bool charging_enabled;
- bool charging_error;
- int rc;
- u8 val;
- rc = smb137c_read_reg(chip, STAT_C_REG, &val);
- if (rc)
- return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
- charging_enabled = val & STAT_C_CHG_ENABLED;
- charging_error = val & STAT_C_CHG_ERROR;
- status = (val & STAT_C_CHG_STAT_MASK) >> STAT_C_CHG_STAT_SHIFT;
- if (!charging_enabled) {
- dev_dbg(&chip->client->dev, "%s: not charging\n", __func__);
- } else if (charging_error) {
- dev_warn(&chip->client->dev, "%s: charger error detected\n",
- __func__);
- } else {
- charge_type = charge_status_type_map[status];
- }
- dev_dbg(&chip->client->dev, "%s: charging status=%s\n", __func__,
- charge_status_name[status]);
- return charge_type;
- }
- static enum power_supply_property smb137c_power_properties[] = {
- POWER_SUPPLY_PROP_STATUS,
- POWER_SUPPLY_PROP_HEALTH,
- POWER_SUPPLY_PROP_PRESENT,
- POWER_SUPPLY_PROP_CHARGING_ENABLED,
- POWER_SUPPLY_PROP_CHARGE_TYPE,
- POWER_SUPPLY_PROP_CURRENT_MAX,
- POWER_SUPPLY_PROP_TECHNOLOGY,
- POWER_SUPPLY_PROP_MODEL_NAME,
- POWER_SUPPLY_PROP_MANUFACTURER,
- };
- static int smb137c_property_is_writeable(struct power_supply *psy,
- enum power_supply_property psp)
- {
- switch (psp) {
- case POWER_SUPPLY_PROP_CHARGING_ENABLED:
- case POWER_SUPPLY_PROP_CURRENT_MAX:
- return 1;
- default:
- break;
- }
- return 0;
- }
- static int smb137c_power_set_property(struct power_supply *psy,
- enum power_supply_property psp,
- const union power_supply_propval *val)
- {
- struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
- mutex_lock(&chip->lock);
- switch (psp) {
- case POWER_SUPPLY_PROP_CHARGING_ENABLED:
- if (val->intval)
- smb137c_enable_charging(chip);
- else
- smb137c_disable_charging(chip);
- break;
- case POWER_SUPPLY_PROP_CURRENT_MAX:
- smb137c_set_charge_current_limit(chip, val->intval);
- break;
- default:
- mutex_unlock(&chip->lock);
- return -EINVAL;
- }
- mutex_unlock(&chip->lock);
- power_supply_changed(&chip->psy);
- return 0;
- }
- static int smb137c_power_get_property(struct power_supply *psy,
- enum power_supply_property psp,
- union power_supply_propval *val)
- {
- struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
- mutex_lock(&chip->lock);
- switch (psp) {
- case POWER_SUPPLY_PROP_STATUS:
- val->intval = smb137c_get_property_status(chip);
- break;
- case POWER_SUPPLY_PROP_HEALTH:
- val->intval = smb137c_get_property_battery_health(chip);
- break;
- case POWER_SUPPLY_PROP_PRESENT:
- val->intval = smb137c_get_property_battery_present(chip);
- break;
- case POWER_SUPPLY_PROP_CHARGING_ENABLED:
- val->intval = chip->charging_enabled;
- break;
- case POWER_SUPPLY_PROP_CHARGE_TYPE:
- val->intval = smb137c_get_property_charge_type(chip);
- break;
- case POWER_SUPPLY_PROP_CURRENT_MAX:
- val->intval = chip->charge_current_limit_ua;
- break;
- case POWER_SUPPLY_PROP_TECHNOLOGY:
- val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
- break;
- case POWER_SUPPLY_PROP_MODEL_NAME:
- val->strval = "SMB137C";
- break;
- case POWER_SUPPLY_PROP_MANUFACTURER:
- val->strval = "Summit Microelectronics";
- break;
- default:
- mutex_unlock(&chip->lock);
- return -EINVAL;
- }
- mutex_unlock(&chip->lock);
- return 0;
- }
- static void smb137c_external_power_changed(struct power_supply *psy)
- {
- struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
- union power_supply_propval prop = {0,};
- int scope = POWER_SUPPLY_SCOPE_DEVICE;
- int current_limit = 0;
- int online = 0;
- int rc;
- mutex_lock(&chip->lock);
- dev_dbg(&chip->client->dev, "%s: start\n", __func__);
- rc = chip->usb_psy->get_property(chip->usb_psy,
- POWER_SUPPLY_PROP_ONLINE, &prop);
- if (rc)
- dev_err(&chip->client->dev, "%s: could not read USB online property, rc=%d\n",
- __func__, rc);
- else
- online = prop.intval;
- rc = chip->usb_psy->get_property(chip->usb_psy, POWER_SUPPLY_PROP_SCOPE,
- &prop);
- if (rc)
- dev_err(&chip->client->dev, "%s: could not read USB scope property, rc=%d\n",
- __func__, rc);
- else
- scope = prop.intval;
- rc = chip->usb_psy->get_property(chip->usb_psy,
- POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
- if (rc)
- dev_err(&chip->client->dev, "%s: could not read USB current_max property, rc=%d\n",
- __func__, rc);
- else
- current_limit = prop.intval;
- if (scope == POWER_SUPPLY_SCOPE_SYSTEM) {
- /* USB host mode */
- smb137c_disable_charging(chip);
- smb137c_enable_otg_mode(chip);
- } else if (online) {
- /* USB online in device mode */
- smb137c_set_usb_input_current_limit(chip, current_limit);
- smb137c_enable_charging(chip);
- smb137c_disable_otg_mode(chip);
- } else {
- /* USB offline */
- smb137c_disable_charging(chip);
- smb137c_disable_otg_mode(chip);
- smb137c_set_usb_input_current_limit(chip,
- min(current_limit, USB_MIN_CURRENT_UA));
- }
- dev_dbg(&chip->client->dev, "%s: end\n", __func__);
- mutex_unlock(&chip->lock);
- power_supply_changed(&chip->psy);
- }
- static int __devinit smb137c_set_register_defaults(struct smb137c_chip *chip)
- {
- int rc;
- u8 val, mask;
- /* Allow volatile register writes. */
- rc = smb137c_masked_write_reg(chip, CMD_A_REG,
- CMD_A_VOLATILE_WRITE_MASK, CMD_A_VOLATILE_WRITE_ALLOW);
- if (rc)
- return rc;
- /* Do not reset register values on USB reinsertion. */
- rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
- SAFETY_TIMER_RELOAD_MASK, SAFETY_TIMER_RELOAD_DISABLED);
- if (rc)
- return rc;
- /* Set various default control parameters. */
- val = PIN_CTRL_DEAD_BATT_CHG_ENABLED | PIN_CTRL_OTG
- | PIN_CTRL_USB_CUR_LIMIT_REG | PIN_CTRL_CHG_EN_REG_LOW
- | PIN_CTRL_OTG_CTRL_REG;
- mask = PIN_CTRL_DEAD_BATT_CHG_MASK | PIN_CTRL_OTG_LBR_MASK
- | PIN_CTRL_USB_CUR_LIMIT_MASK | PIN_CTRL_CHG_EN_MASK
- | PIN_CTRL_OTG_CTRL_MASK;
- rc = smb137c_masked_write_reg(chip, PIN_CTRL_REG, mask, val);
- if (rc)
- return rc;
- /* Disable charging, disable OTG mode, and allow fast-charge current. */
- val = CMD_A_CHARGING_DISABLED | CMD_A_OTG_DISABLED
- | CMD_A_FAST_CHG_ALLOW;
- mask = CMD_A_CHARGING_MASK | CMD_A_OTG_MASK | CMD_A_FAST_CHG_MASK;
- rc = smb137c_masked_write_reg(chip, CMD_A_REG, mask, val);
- if (rc)
- return rc;
- /* Enable auto recharging and full-time THERM monitor. */
- val = CTRL_A_AUTO_RECHARGE_ENABLED | CTRL_A_THERM_MONITOR_ENABLED;
- mask = CTRL_A_AUTO_RECHARGE_MASK | CTRL_A_THERM_MONITOR_MASK;
- rc = smb137c_masked_write_reg(chip, CTRL_A_REG, mask, val);
- if (rc)
- return rc;
- /* Use register value instead of pin to control USB suspend. */
- rc = smb137c_masked_write_reg(chip, VAR_FUNC_REG,
- VAR_FUNC_USB_SUSPEND_CTRL_MASK, VAR_FUNC_USB_SUSPEND_CTRL_REG);
- if (rc)
- return rc;
- return rc;
- }
- static int __devinit smb137c_apply_dt_configs(struct smb137c_chip *chip)
- {
- struct device *dev = &chip->client->dev;
- struct device_node *node = chip->client->dev.of_node;
- int ret, current_ma, voltage_mv, timeout, value;
- int rc = 0;
- /*
- * All device tree parameters are optional so it is ok if read calls
- * fail.
- */
- ret = of_property_read_u32(node, "summit,chg-current-ma", ¤t_ma);
- if (ret == 0) {
- rc = smb137c_set_charge_current_limit(chip, current_ma * 1000);
- if (rc) {
- dev_err(dev, "%s: Failed to set charge current, rc=%d\n",
- __func__, rc);
- return rc;
- }
- } else {
- chip->charge_current_limit_ua
- = smb137c_get_charge_current_limit(chip);
- rc = chip->charge_current_limit_ua;
- if (rc < 0) {
- dev_err(dev, "%s: Failed to get charge current, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,term-current-ma", ¤t_ma);
- if (ret == 0) {
- rc = smb137c_set_term_current(chip, current_ma * 1000);
- if (rc) {
- dev_err(dev, "%s: Failed to set termination current, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,pre-chg-current-ma",
- ¤t_ma);
- if (ret == 0) {
- rc = smb137c_set_pre_charge_current_limit(chip,
- current_ma * 1000);
- if (rc) {
- dev_err(dev, "%s: Failed to set pre-charge current limit, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,float-voltage-mv",
- &voltage_mv);
- if (ret == 0) {
- rc = smb137c_set_float_voltage(chip, voltage_mv * 1000);
- if (rc) {
- dev_err(dev, "%s: Failed to set float voltage, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,thresh-voltage-mv",
- &voltage_mv);
- if (ret == 0) {
- rc = smb137c_set_pre_charge_threshold_voltage(chip,
- voltage_mv * 1000);
- if (rc) {
- dev_err(dev, "%s: Failed to set fast-charge threshold voltage, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,recharge-thresh-mv",
- &voltage_mv);
- if (ret == 0) {
- rc = smb137c_set_recharge_threshold_voltage(chip,
- voltage_mv * 1000);
- if (rc) {
- dev_err(dev, "%s: Failed to set recharge threshold voltage, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,system-voltage-mv",
- &voltage_mv);
- if (ret == 0) {
- rc = smb137c_set_system_voltage(chip, voltage_mv * 1000);
- if (rc) {
- dev_err(dev, "%s: Failed to set system voltage, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,charging-timeout", &timeout);
- if (ret == 0) {
- rc = smb137c_set_charging_timeout(chip, timeout);
- if (rc) {
- dev_err(dev, "%s: Failed to set charging timeout, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,pre-charge-timeout", &timeout);
- if (ret == 0) {
- rc = smb137c_set_pre_charge_timeout(chip, timeout);
- if (rc) {
- dev_err(dev, "%s: Failed to set pre-charge timeout, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,therm-current-ua", &value);
- if (ret == 0) {
- rc = smb137c_set_thermistor_current(chip, value);
- if (rc) {
- dev_err(dev, "%s: Failed to set thermistor current, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,temperature-min", &value);
- if (ret == 0) {
- rc = smb137c_set_temperature_low_limit(chip, value);
- if (rc) {
- dev_err(dev, "%s: Failed to set low temperature limit, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- ret = of_property_read_u32(node, "summit,temperature-max", &value);
- if (ret == 0) {
- rc = smb137c_set_temperature_high_limit(chip, value);
- if (rc) {
- dev_err(dev, "%s: Failed to set high temperature limit, rc=%d\n",
- __func__, rc);
- return rc;
- }
- }
- return rc;
- }
- static int __devinit smb137c_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct smb137c_chip *chip;
- struct device *dev = &client->dev;
- struct device_node *node = client->dev.of_node;
- int rc = 0;
- int gui_rev, silicon_rev;
- u8 dev_id;
- if (!node) {
- dev_err(dev, "%s: device tree information missing\n", __func__);
- return -ENODEV;
- }
- if (!i2c_check_functionality(client->adapter,
- I2C_FUNC_SMBUS_BYTE_DATA)) {
- dev_err(dev, "%s: SMBUS_BYTE_DATA unsupported\n", __func__);
- return -EIO;
- }
- chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
- if (!chip) {
- dev_err(dev, "%s: devm_kzalloc failed\n", __func__);
- return -ENOMEM;
- }
- mutex_init(&chip->lock);
- chip->client = client;
- i2c_set_clientdata(client, chip);
- chip->usb_psy = power_supply_get_by_name("usb");
- if (!chip->usb_psy) {
- dev_dbg(dev, "%s: USB supply not found; deferring charger probe\n",
- __func__);
- return -EPROBE_DEFER;
- }
- rc = smb137c_read_reg(chip, DEV_ID_REG, &dev_id);
- if (rc)
- return rc;
- if ((dev_id & DEV_ID_PART_MASK) != DEV_ID_PART_SMB137C) {
- dev_err(dev, "%s: invalid device ID=0x%02X\n", __func__,
- dev_id);
- return -ENODEV;
- }
- gui_rev = (dev_id & DEV_ID_GUI_REV_MASK) >> DEV_ID_GUI_REV_SHIFT;
- silicon_rev = (dev_id & DEV_ID_SILICON_REV_MASK)
- >> DEV_ID_SILICON_REV_SHIFT;
- rc = smb137c_set_register_defaults(chip);
- if (rc)
- return rc;
- rc = smb137c_apply_dt_configs(chip);
- if (rc)
- return rc;
- chip->psy.name = "battery";
- chip->psy.type = POWER_SUPPLY_TYPE_BATTERY;
- chip->psy.properties = smb137c_power_properties;
- chip->psy.num_properties = ARRAY_SIZE(smb137c_power_properties);
- chip->psy.get_property = smb137c_power_get_property;
- chip->psy.set_property = smb137c_power_set_property;
- chip->psy.property_is_writeable = smb137c_property_is_writeable;
- chip->psy.external_power_changed = smb137c_external_power_changed;
- rc = power_supply_register(dev, &chip->psy);
- if (rc < 0) {
- dev_err(dev, "%s: power_supply_register failed, rc=%d\n",
- __func__, rc);
- return rc;
- }
- smb137c_external_power_changed(&chip->psy);
- dev_info(dev, "%s: SMB137C charger probed successfully, gui_rev=%d, silicon_rev=%d\n",
- __func__, gui_rev, silicon_rev);
- return rc;
- }
- static int __devexit smb137c_remove(struct i2c_client *client)
- {
- return 0;
- }
- static const struct i2c_device_id smb137c_id[] = {
- { .name = "smb137c", },
- {},
- };
- MODULE_DEVICE_TABLE(i2c, smb137c_id);
- static const struct of_device_id smb137c_match[] = {
- { .compatible = "summit,smb137c", },
- { },
- };
- static struct i2c_driver smb137c_driver = {
- .driver = {
- .name = "smb137c",
- .owner = THIS_MODULE,
- .of_match_table = smb137c_match,
- },
- .probe = smb137c_probe,
- .remove = __devexit_p(smb137c_remove),
- .id_table = smb137c_id,
- };
- static int __init smb137c_init(void)
- {
- return i2c_add_driver(&smb137c_driver);
- }
- module_init(smb137c_init);
- static void __exit smb137c_exit(void)
- {
- return i2c_del_driver(&smb137c_driver);
- }
- module_exit(smb137c_exit);
- MODULE_DESCRIPTION("SMB137C Charger");
- MODULE_LICENSE("GPL v2");
- MODULE_ALIAS("i2c:smb137c");
|