smb350_charger.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /* Copyright (c) 2012-2013 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/i2c.h>
  15. #include <linux/gpio.h>
  16. #include <linux/errno.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/slab.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/i2c/smb350.h>
  25. #include <linux/bitops.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/printk.h>
  30. /* Register definitions */
  31. #define CHG_CURRENT_REG 0x00 /* Non-Volatile + mirror */
  32. #define CHG_OTHER_CURRENT_REG 0x01 /* Non-Volatile + mirror */
  33. #define VAR_FUNC_REG 0x02 /* Non-Volatile + mirror */
  34. #define FLOAT_VOLTAGE_REG 0x03 /* Non-Volatile + mirror */
  35. #define CHG_CTRL_REG 0x04 /* Non-Volatile + mirror */
  36. #define STAT_TIMER_REG 0x05 /* Non-Volatile + mirror */
  37. #define PIN_ENABLE_CTRL_REG 0x06 /* Non-Volatile + mirror */
  38. #define THERM_CTRL_A_REG 0x07 /* Non-Volatile + mirror */
  39. #define SYSOK_USB3_SELECT_REG 0x08 /* Non-Volatile + mirror */
  40. #define CTRL_FUNCTIONS_REG 0x09 /* Non-Volatile + mirror */
  41. #define OTG_TLIM_THERM_CNTRL_REG 0x0A /* Non-Volatile + mirror */
  42. #define TEMP_MONITOR_REG 0x0B /* Non-Volatile + mirror */
  43. #define FAULT_IRQ_REG 0x0C /* Non-Volatile */
  44. #define IRQ_ENABLE_REG 0x0D /* Non-Volatile */
  45. #define SYSOK_REG 0x0E /* Non-Volatile + mirror */
  46. #define AUTO_INPUT_VOLT_DETECT_REG 0x10 /* Non-Volatile Read-Only */
  47. #define STATUS_IRQ_REG 0x11 /* Non-Volatile Read-Only */
  48. #define I2C_SLAVE_ADDR_REG 0x12 /* Non-Volatile Read-Only */
  49. #define CMD_A_REG 0x30 /* Volatile Read-Write */
  50. #define CMD_B_REG 0x31 /* Volatile Read-Write */
  51. #define CMD_C_REG 0x33 /* Volatile Read-Write */
  52. #define HW_VERSION_REG 0x34 /* Volatile Read-Only */
  53. #define IRQ_STATUS_A_REG 0x35 /* Volatile Read-Only */
  54. #define IRQ_STATUS_B_REG 0x36 /* Volatile Read-Only */
  55. #define IRQ_STATUS_C_REG 0x37 /* Volatile Read-Only */
  56. #define IRQ_STATUS_D_REG 0x38 /* Volatile Read-Only */
  57. #define IRQ_STATUS_E_REG 0x39 /* Volatile Read-Only */
  58. #define IRQ_STATUS_F_REG 0x3A /* Volatile Read-Only */
  59. #define STATUS_A_REG 0x3B /* Volatile Read-Only */
  60. #define STATUS_B_REG 0x3D /* Volatile Read-Only */
  61. /* Note: STATUS_C_REG was removed from SMB349 to SMB350 */
  62. #define STATUS_D_REG 0x3E /* Volatile Read-Only */
  63. #define STATUS_E_REG 0x3F /* Volatile Read-Only */
  64. #define IRQ_STATUS_NUM (IRQ_STATUS_F_REG - IRQ_STATUS_A_REG + 1)
  65. /* Status bits and masks */
  66. #define SMB350_MASK(BITS, POS) ((u8)(((1 << BITS) - 1) << POS))
  67. #define FAST_CHG_CURRENT_MASK SMB350_MASK(4, 4)
  68. #define SMB350_FAST_CHG_MIN_MA 1000
  69. #define SMB350_FAST_CHG_STEP_MA 200
  70. #define SMB350_FAST_CHG_MAX_MA 3600
  71. #define TERM_CURRENT_MASK SMB350_MASK(3, 2)
  72. #define SMB350_TERM_CUR_MIN_MA 200
  73. #define SMB350_TERM_CUR_STEP_MA 100
  74. #define SMB350_TERM_CUR_MAX_MA 700
  75. #define CMD_A_VOLATILE_WR_PERM BIT(7)
  76. #define CHG_CTRL_CURR_TERM_END_CHG BIT(6)
  77. enum smb350_chg_status {
  78. SMB_CHG_STATUS_NONE = 0,
  79. SMB_CHG_STATUS_PRE_CHARGE = 1,
  80. SMB_CHG_STATUS_FAST_CHARGE = 2,
  81. SMB_CHG_STATUS_TAPER_CHARGE = 3,
  82. };
  83. static const char * const smb350_chg_status[] = {
  84. "none",
  85. "pre-charge",
  86. "fast-charge",
  87. "taper-charge"
  88. };
  89. struct smb350_device {
  90. /* setup */
  91. int chg_current_ma;
  92. int term_current_ma;
  93. int chg_en_n_gpio;
  94. int chg_susp_n_gpio;
  95. int stat_gpio;
  96. int irq;
  97. /* internal */
  98. enum smb350_chg_status chg_status;
  99. struct i2c_client *client;
  100. struct delayed_work irq_work;
  101. struct dentry *dent;
  102. struct wake_lock chg_wake_lock;
  103. struct power_supply dc_psy;
  104. };
  105. static struct smb350_device *smb350_dev;
  106. struct debug_reg {
  107. char *name;
  108. u8 reg;
  109. };
  110. #define SMB350_DEBUG_REG(x) {#x, x##_REG}
  111. static struct debug_reg smb350_debug_regs[] = {
  112. SMB350_DEBUG_REG(CHG_CURRENT),
  113. SMB350_DEBUG_REG(CHG_OTHER_CURRENT),
  114. SMB350_DEBUG_REG(VAR_FUNC),
  115. SMB350_DEBUG_REG(FLOAT_VOLTAGE),
  116. SMB350_DEBUG_REG(CHG_CTRL),
  117. SMB350_DEBUG_REG(STAT_TIMER),
  118. SMB350_DEBUG_REG(PIN_ENABLE_CTRL),
  119. SMB350_DEBUG_REG(THERM_CTRL_A),
  120. SMB350_DEBUG_REG(SYSOK_USB3_SELECT),
  121. SMB350_DEBUG_REG(CTRL_FUNCTIONS),
  122. SMB350_DEBUG_REG(OTG_TLIM_THERM_CNTRL),
  123. SMB350_DEBUG_REG(TEMP_MONITOR),
  124. SMB350_DEBUG_REG(FAULT_IRQ),
  125. SMB350_DEBUG_REG(IRQ_ENABLE),
  126. SMB350_DEBUG_REG(SYSOK),
  127. SMB350_DEBUG_REG(AUTO_INPUT_VOLT_DETECT),
  128. SMB350_DEBUG_REG(STATUS_IRQ),
  129. SMB350_DEBUG_REG(I2C_SLAVE_ADDR),
  130. SMB350_DEBUG_REG(CMD_A),
  131. SMB350_DEBUG_REG(CMD_B),
  132. SMB350_DEBUG_REG(CMD_C),
  133. SMB350_DEBUG_REG(HW_VERSION),
  134. SMB350_DEBUG_REG(IRQ_STATUS_A),
  135. SMB350_DEBUG_REG(IRQ_STATUS_B),
  136. SMB350_DEBUG_REG(IRQ_STATUS_C),
  137. SMB350_DEBUG_REG(IRQ_STATUS_D),
  138. SMB350_DEBUG_REG(IRQ_STATUS_E),
  139. SMB350_DEBUG_REG(IRQ_STATUS_F),
  140. SMB350_DEBUG_REG(STATUS_A),
  141. SMB350_DEBUG_REG(STATUS_B),
  142. SMB350_DEBUG_REG(STATUS_D),
  143. SMB350_DEBUG_REG(STATUS_E),
  144. };
  145. /*
  146. * Read 8-bit register value. return negative value on error.
  147. */
  148. static int smb350_read_reg(struct i2c_client *client, u8 reg)
  149. {
  150. int val;
  151. val = i2c_smbus_read_byte_data(client, reg);
  152. if (val < 0)
  153. pr_err("i2c read fail. reg=0x%x.ret=%d.\n", reg, val);
  154. else
  155. pr_debug("reg=0x%02X.val=0x%02X.\n", reg , val);
  156. return val;
  157. }
  158. /*
  159. * Write 8-bit register value. return negative value on error.
  160. */
  161. static int smb350_write_reg(struct i2c_client *client, u8 reg, u8 val)
  162. {
  163. int ret;
  164. ret = i2c_smbus_write_byte_data(client, reg, val);
  165. if (ret < 0)
  166. pr_err("i2c read fail. reg=0x%x.val=0x%x.ret=%d.\n",
  167. reg, val, ret);
  168. else
  169. pr_debug("reg=0x%02X.val=0x%02X.\n", reg , val);
  170. return ret;
  171. }
  172. static int smb350_masked_write(struct i2c_client *client, int reg, u8 mask,
  173. u8 val)
  174. {
  175. int ret;
  176. int temp;
  177. int shift = find_first_bit((unsigned long *) &mask, 8);
  178. temp = smb350_read_reg(client, reg);
  179. if (temp < 0)
  180. return temp;
  181. temp &= ~mask;
  182. temp |= (val << shift) & mask;
  183. ret = smb350_write_reg(client, reg, temp);
  184. return ret;
  185. }
  186. static bool smb350_is_dc_present(struct i2c_client *client)
  187. {
  188. u16 irq_status_f = smb350_read_reg(client, IRQ_STATUS_F_REG);
  189. bool power_ok = irq_status_f & 0x01;
  190. /* Power-ok , IRQ_STATUS_F_REG bit#0 */
  191. if (power_ok)
  192. pr_debug("DC is present.\n");
  193. else
  194. pr_debug("DC is missing.\n");
  195. return power_ok;
  196. }
  197. static bool smb350_is_charger_present(struct i2c_client *client)
  198. {
  199. int val;
  200. /* Normally the device is non-removable and embedded on the board.
  201. * Verify that charger is present by getting I2C response.
  202. */
  203. val = smb350_read_reg(client, STATUS_B_REG);
  204. if (val < 0)
  205. return false;
  206. return true;
  207. }
  208. static int smb350_get_prop_charge_type(struct smb350_device *dev)
  209. {
  210. int status_b;
  211. enum smb350_chg_status status;
  212. int chg_type = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  213. bool chg_enabled;
  214. bool charger_err;
  215. struct i2c_client *client = dev->client;
  216. status_b = smb350_read_reg(client, STATUS_B_REG);
  217. if (status_b < 0) {
  218. pr_err("failed to read STATUS_B_REG.\n");
  219. return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  220. }
  221. chg_enabled = (bool) (status_b & 0x01);
  222. charger_err = (bool) (status_b & (1<<6));
  223. if (!chg_enabled) {
  224. pr_warn("Charging not enabled.\n");
  225. /* release the wake-lock when DC power removed */
  226. if (wake_lock_active(&dev->chg_wake_lock))
  227. wake_unlock(&dev->chg_wake_lock);
  228. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  229. }
  230. if (charger_err) {
  231. pr_warn("Charger error detected.\n");
  232. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  233. }
  234. status = (status_b >> 1) & 0x3;
  235. if (status == SMB_CHG_STATUS_NONE)
  236. chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
  237. else if (status == SMB_CHG_STATUS_FAST_CHARGE) /* constant current */
  238. chg_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
  239. else if (status == SMB_CHG_STATUS_TAPER_CHARGE) /* constant voltage */
  240. chg_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
  241. else if (status == SMB_CHG_STATUS_PRE_CHARGE)
  242. chg_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  243. pr_debug("smb-chg-status=%d=%s.\n", status, smb350_chg_status[status]);
  244. if (dev->chg_status != status) { /* Status changed */
  245. if (status == SMB_CHG_STATUS_NONE) {
  246. pr_debug("Charging stopped.\n");
  247. wake_unlock(&dev->chg_wake_lock);
  248. } else {
  249. pr_debug("Charging started.\n");
  250. wake_lock(&dev->chg_wake_lock);
  251. }
  252. }
  253. dev->chg_status = status;
  254. return chg_type;
  255. }
  256. static void smb350_enable_charging(struct smb350_device *dev, bool enable)
  257. {
  258. int val = !enable; /* active low */
  259. pr_debug("enable=%d.\n", enable);
  260. gpio_set_value_cansleep(dev->chg_en_n_gpio, val);
  261. }
  262. /* When the status bit of a certain condition is read,
  263. * the corresponding IRQ signal is cleared.
  264. */
  265. static int smb350_clear_irq(struct i2c_client *client)
  266. {
  267. int ret;
  268. ret = smb350_read_reg(client, IRQ_STATUS_A_REG);
  269. if (ret < 0)
  270. return ret;
  271. ret = smb350_read_reg(client, IRQ_STATUS_B_REG);
  272. if (ret < 0)
  273. return ret;
  274. ret = smb350_read_reg(client, IRQ_STATUS_C_REG);
  275. if (ret < 0)
  276. return ret;
  277. ret = smb350_read_reg(client, IRQ_STATUS_D_REG);
  278. if (ret < 0)
  279. return ret;
  280. ret = smb350_read_reg(client, IRQ_STATUS_E_REG);
  281. if (ret < 0)
  282. return ret;
  283. ret = smb350_read_reg(client, IRQ_STATUS_F_REG);
  284. if (ret < 0)
  285. return ret;
  286. return 0;
  287. }
  288. /*
  289. * Do the IRQ work from a thread context rather than interrupt context.
  290. * Read status registers to clear interrupt source.
  291. * Notify the power-supply driver about change detected.
  292. * Relevant events for start/stop charging:
  293. * 1. DC insert/remove
  294. * 2. End-Of-Charging
  295. * 3. Battery insert/remove
  296. * 4. Temperture too hot/cold
  297. * 5. Charging timeout expired.
  298. */
  299. static void smb350_irq_worker(struct work_struct *work)
  300. {
  301. int ret = 0;
  302. struct smb350_device *dev =
  303. container_of(work, struct smb350_device, irq_work.work);
  304. ret = smb350_clear_irq(dev->client);
  305. if (ret == 0) { /* Cleared ok */
  306. /* Notify Battery-psy about status changed */
  307. pr_debug("Notify power_supply_changed.\n");
  308. power_supply_changed(&dev->dc_psy);
  309. }
  310. }
  311. /*
  312. * The STAT pin is low when charging and high when not charging.
  313. * When the smb350 start/stop charging the STAT pin triggers an interrupt.
  314. * Interrupt is triggered on both rising or falling edge.
  315. */
  316. static irqreturn_t smb350_irq(int irq, void *dev_id)
  317. {
  318. struct smb350_device *dev = dev_id;
  319. pr_debug("\n");
  320. /* I2C transfers API should not run in interrupt context */
  321. schedule_delayed_work(&dev->irq_work, msecs_to_jiffies(100));
  322. return IRQ_HANDLED;
  323. }
  324. static enum power_supply_property pm_power_props[] = {
  325. /* real time */
  326. POWER_SUPPLY_PROP_PRESENT,
  327. POWER_SUPPLY_PROP_ONLINE,
  328. POWER_SUPPLY_PROP_CHARGE_TYPE,
  329. /* fixed */
  330. POWER_SUPPLY_PROP_MANUFACTURER,
  331. POWER_SUPPLY_PROP_MODEL_NAME,
  332. POWER_SUPPLY_PROP_CURRENT_MAX,
  333. };
  334. static char *pm_power_supplied_to[] = {
  335. "battery",
  336. };
  337. static int smb350_get_property(struct power_supply *psy,
  338. enum power_supply_property psp,
  339. union power_supply_propval *val)
  340. {
  341. int ret = 0;
  342. struct smb350_device *dev = container_of(psy,
  343. struct smb350_device,
  344. dc_psy);
  345. struct i2c_client *client = dev->client;
  346. switch (psp) {
  347. case POWER_SUPPLY_PROP_PRESENT:
  348. val->intval = smb350_is_charger_present(client);
  349. break;
  350. case POWER_SUPPLY_PROP_ONLINE:
  351. val->intval = smb350_is_dc_present(client);
  352. break;
  353. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  354. val->intval = smb350_get_prop_charge_type(dev);
  355. break;
  356. case POWER_SUPPLY_PROP_MODEL_NAME:
  357. val->strval = SMB350_NAME;
  358. break;
  359. case POWER_SUPPLY_PROP_MANUFACTURER:
  360. val->strval = "Summit Microelectronics";
  361. break;
  362. case POWER_SUPPLY_PROP_CURRENT_MAX:
  363. val->intval = dev->chg_current_ma;
  364. break;
  365. default:
  366. pr_err("Invalid prop = %d.\n", psp);
  367. ret = -EINVAL;
  368. break;
  369. }
  370. return ret;
  371. }
  372. static int smb350_set_property(struct power_supply *psy,
  373. enum power_supply_property psp,
  374. const union power_supply_propval *val)
  375. {
  376. int ret = 0;
  377. struct smb350_device *dev =
  378. container_of(psy, struct smb350_device, dc_psy);
  379. switch (psp) {
  380. /*
  381. * Allow a smart battery to Start/Stop charging.
  382. * i.e. when End-Of-Charging detected.
  383. * The SMB350 can be configured to terminate charging
  384. * when charge-current reaching Termination-Current.
  385. */
  386. case POWER_SUPPLY_PROP_ONLINE:
  387. smb350_enable_charging(dev, val->intval);
  388. break;
  389. default:
  390. pr_err("Invalid prop = %d.\n", psp);
  391. ret = -EINVAL;
  392. }
  393. return ret;
  394. }
  395. static int smb350_set_chg_current(struct i2c_client *client, int current_ma)
  396. {
  397. int ret;
  398. u8 temp;
  399. if ((current_ma < SMB350_FAST_CHG_MIN_MA) ||
  400. (current_ma > SMB350_FAST_CHG_MAX_MA)) {
  401. pr_err("invalid current %d mA.\n", current_ma);
  402. return -EINVAL;
  403. }
  404. temp = (current_ma - SMB350_FAST_CHG_MIN_MA) / SMB350_FAST_CHG_STEP_MA;
  405. pr_debug("fast-chg-current=%d mA setting %02x\n", current_ma, temp);
  406. ret = smb350_masked_write(client, CHG_CURRENT_REG,
  407. FAST_CHG_CURRENT_MASK, temp);
  408. return ret;
  409. }
  410. static int smb350_set_term_current(struct i2c_client *client, int current_ma)
  411. {
  412. int ret;
  413. u8 temp;
  414. if ((current_ma < SMB350_TERM_CUR_MIN_MA) ||
  415. (current_ma > SMB350_TERM_CUR_MAX_MA)) {
  416. pr_err("invalid current %d mA to set\n", current_ma);
  417. return -EINVAL;
  418. }
  419. temp = (current_ma - SMB350_TERM_CUR_MIN_MA) / SMB350_TERM_CUR_STEP_MA;
  420. pr_debug("term-current=%d mA setting %02x\n", current_ma, temp);
  421. ret = smb350_masked_write(client, CHG_OTHER_CURRENT_REG,
  422. TERM_CURRENT_MASK, temp);
  423. return ret;
  424. }
  425. static int smb350_set_reg(void *data, u64 val)
  426. {
  427. u32 addr = (u32) data;
  428. int ret;
  429. struct i2c_client *client = smb350_dev->client;
  430. ret = smb350_write_reg(client, addr, (u8) val);
  431. return ret;
  432. }
  433. static int smb350_get_reg(void *data, u64 *val)
  434. {
  435. u32 addr = (u32) data;
  436. int ret;
  437. struct i2c_client *client = smb350_dev->client;
  438. ret = smb350_read_reg(client, addr);
  439. if (ret < 0)
  440. return ret;
  441. *val = ret;
  442. return 0;
  443. }
  444. DEFINE_SIMPLE_ATTRIBUTE(reg_fops, smb350_get_reg, smb350_set_reg, "0x%02llx\n");
  445. static int smb350_create_debugfs_entries(struct smb350_device *dev)
  446. {
  447. int i;
  448. dev->dent = debugfs_create_dir(SMB350_NAME, NULL);
  449. if (IS_ERR(dev->dent)) {
  450. pr_err("smb350 driver couldn't create debugfs dir\n");
  451. return -EFAULT;
  452. }
  453. for (i = 0 ; i < ARRAY_SIZE(smb350_debug_regs) ; i++) {
  454. char *name = smb350_debug_regs[i].name;
  455. u32 reg = smb350_debug_regs[i].reg;
  456. struct dentry *file;
  457. file = debugfs_create_file(name, 0644, dev->dent,
  458. (void *) reg, &reg_fops);
  459. if (IS_ERR(file)) {
  460. pr_err("debugfs_create_file %s failed.\n", name);
  461. return -EFAULT;
  462. }
  463. }
  464. return 0;
  465. }
  466. static int smb350_set_volatile_params(struct smb350_device *dev)
  467. {
  468. int ret;
  469. struct i2c_client *client = dev->client;
  470. pr_debug("\n");
  471. ret = smb350_write_reg(client, CMD_A_REG, CMD_A_VOLATILE_WR_PERM);
  472. if (ret) {
  473. pr_err("Failed to set VOLATILE_WR_PERM ret=%d\n", ret);
  474. return ret;
  475. }
  476. /* Disable SMB350 pulse-IRQ mechanism,
  477. * we use interrupts based on charging-status-transition
  478. */
  479. /* Enable STATUS output (regardless of IRQ-pulses) */
  480. smb350_masked_write(client, CMD_A_REG, BIT(0), 0);
  481. /* Disable LED blinking - avoid periodic irq */
  482. smb350_masked_write(client, PIN_ENABLE_CTRL_REG, BIT(7), 0);
  483. /* Disable Failure SMB-IRQ */
  484. ret = smb350_write_reg(client, FAULT_IRQ_REG, 0x00);
  485. if (ret) {
  486. pr_err("Failed to set FAULT_IRQ_REG ret=%d\n", ret);
  487. return ret;
  488. }
  489. /* Disable Event IRQ */
  490. ret = smb350_write_reg(client, IRQ_ENABLE_REG, 0x00);
  491. if (ret) {
  492. pr_err("Failed to set IRQ_ENABLE_REG ret=%d\n", ret);
  493. return ret;
  494. }
  495. /* Enable charging/not-charging status output via STAT pin */
  496. smb350_masked_write(client, STAT_TIMER_REG, BIT(5), 0);
  497. /* Disable Automatic Recharge */
  498. smb350_masked_write(client, CHG_CTRL_REG, BIT(7), 1);
  499. /* Set fast-charge current */
  500. ret = smb350_set_chg_current(client, dev->chg_current_ma);
  501. if (ret) {
  502. pr_err("Failed to set FAST_CHG_CURRENT ret=%d\n", ret);
  503. return ret;
  504. }
  505. if (dev->term_current_ma > 0) {
  506. /* Enable Current Termination */
  507. smb350_masked_write(client, CHG_CTRL_REG, BIT(6), 0);
  508. /* Set Termination current */
  509. smb350_set_term_current(client, dev->term_current_ma);
  510. } else {
  511. /* Disable Current Termination */
  512. smb350_masked_write(client, CHG_CTRL_REG, BIT(6), 1);
  513. }
  514. return 0;
  515. }
  516. static int __devinit smb350_register_psy(struct smb350_device *dev)
  517. {
  518. int ret;
  519. dev->dc_psy.name = "dc";
  520. dev->dc_psy.type = POWER_SUPPLY_TYPE_MAINS;
  521. dev->dc_psy.supplied_to = pm_power_supplied_to;
  522. dev->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to);
  523. dev->dc_psy.properties = pm_power_props;
  524. dev->dc_psy.num_properties = ARRAY_SIZE(pm_power_props);
  525. dev->dc_psy.get_property = smb350_get_property;
  526. dev->dc_psy.set_property = smb350_set_property;
  527. ret = power_supply_register(&dev->client->dev,
  528. &dev->dc_psy);
  529. if (ret) {
  530. pr_err("failed to register power_supply. ret=%d.\n", ret);
  531. return ret;
  532. }
  533. return 0;
  534. }
  535. static int __devinit smb350_probe(struct i2c_client *client,
  536. const struct i2c_device_id *id)
  537. {
  538. int ret = 0;
  539. const struct smb350_platform_data *pdata;
  540. struct device_node *dev_node = client->dev.of_node;
  541. struct smb350_device *dev;
  542. u8 version;
  543. /* STAT pin change on start/stop charging */
  544. u32 irq_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
  545. if (!i2c_check_functionality(client->adapter,
  546. I2C_FUNC_SMBUS_BYTE_DATA)) {
  547. pr_err("i2c func fail.\n");
  548. return -EIO;
  549. }
  550. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  551. if (!dev) {
  552. pr_err("alloc fail.\n");
  553. return -ENOMEM;
  554. }
  555. smb350_dev = dev;
  556. dev->client = client;
  557. if (dev_node) {
  558. dev->chg_en_n_gpio =
  559. of_get_named_gpio(dev_node, "summit,chg-en-n-gpio", 0);
  560. pr_debug("chg_en_n_gpio = %d.\n", dev->chg_en_n_gpio);
  561. dev->chg_susp_n_gpio =
  562. of_get_named_gpio(dev_node,
  563. "summit,chg-susp-n-gpio", 0);
  564. pr_debug("chg_susp_n_gpio = %d.\n", dev->chg_susp_n_gpio);
  565. dev->stat_gpio =
  566. of_get_named_gpio(dev_node, "summit,stat-gpio", 0);
  567. pr_debug("stat_gpio = %d.\n", dev->stat_gpio);
  568. ret = of_property_read_u32(dev_node, "summit,chg-current-ma",
  569. &(dev->chg_current_ma));
  570. pr_debug("chg_current_ma = %d.\n", dev->chg_current_ma);
  571. if (ret) {
  572. pr_err("Unable to read chg_current.\n");
  573. return ret;
  574. }
  575. ret = of_property_read_u32(dev_node, "summit,term-current-ma",
  576. &(dev->term_current_ma));
  577. pr_debug("term_current_ma = %d.\n", dev->term_current_ma);
  578. if (ret) {
  579. pr_err("Unable to read term_current_ma.\n");
  580. return ret;
  581. }
  582. } else {
  583. pdata = client->dev.platform_data;
  584. if (pdata == NULL) {
  585. pr_err("no platform data.\n");
  586. return -EINVAL;
  587. }
  588. dev->chg_en_n_gpio = pdata->chg_en_n_gpio;
  589. dev->chg_susp_n_gpio = pdata->chg_susp_n_gpio;
  590. dev->stat_gpio = pdata->stat_gpio;
  591. dev->chg_current_ma = pdata->chg_current_ma;
  592. dev->term_current_ma = pdata->term_current_ma;
  593. }
  594. ret = gpio_request(dev->stat_gpio, "smb350_stat");
  595. if (ret) {
  596. pr_err("gpio_request failed for %d ret=%d\n",
  597. dev->stat_gpio, ret);
  598. goto err_stat_gpio;
  599. }
  600. dev->irq = gpio_to_irq(dev->stat_gpio);
  601. pr_debug("irq#=%d.\n", dev->irq);
  602. ret = gpio_request(dev->chg_susp_n_gpio, "smb350_suspend");
  603. if (ret) {
  604. pr_err("gpio_request failed for %d ret=%d\n",
  605. dev->chg_susp_n_gpio, ret);
  606. goto err_susp_gpio;
  607. }
  608. ret = gpio_request(dev->chg_en_n_gpio, "smb350_charger_enable");
  609. if (ret) {
  610. pr_err("gpio_request failed for %d ret=%d\n",
  611. dev->chg_en_n_gpio, ret);
  612. goto err_en_gpio;
  613. }
  614. i2c_set_clientdata(client, dev);
  615. /* Disable battery charging by default on power up.
  616. * Battery charging is enabled by BMS or Battery-Gauge
  617. * by using the set_property callback.
  618. */
  619. smb350_enable_charging(dev, false);
  620. msleep(100);
  621. gpio_set_value_cansleep(dev->chg_susp_n_gpio, 1); /* Normal */
  622. msleep(100); /* Allow the device to exist shutdown */
  623. /* I2C transaction allowed only after device exit suspend */
  624. ret = smb350_read_reg(client, I2C_SLAVE_ADDR_REG);
  625. if ((ret>>1) != client->addr) {
  626. pr_err("No device.\n");
  627. ret = -ENODEV;
  628. goto err_no_dev;
  629. }
  630. version = smb350_read_reg(client, HW_VERSION_REG);
  631. version &= 0x0F; /* bits 0..3 */
  632. ret = smb350_set_volatile_params(dev);
  633. if (ret)
  634. goto err_set_params;
  635. ret = smb350_register_psy(dev);
  636. if (ret)
  637. goto err_set_params;
  638. ret = smb350_create_debugfs_entries(dev);
  639. if (ret)
  640. goto err_debugfs;
  641. INIT_DELAYED_WORK(&dev->irq_work, smb350_irq_worker);
  642. wake_lock_init(&dev->chg_wake_lock,
  643. WAKE_LOCK_SUSPEND, SMB350_NAME);
  644. ret = request_irq(dev->irq, smb350_irq, irq_flags,
  645. "smb350_irq", dev);
  646. if (ret) {
  647. pr_err("request_irq %d failed.ret=%d\n", dev->irq, ret);
  648. goto err_irq;
  649. }
  650. pr_info("HW Version = 0x%X.\n", version);
  651. return 0;
  652. err_irq:
  653. err_debugfs:
  654. if (dev->dent)
  655. debugfs_remove_recursive(dev->dent);
  656. err_no_dev:
  657. err_set_params:
  658. gpio_free(dev->chg_en_n_gpio);
  659. err_en_gpio:
  660. gpio_free(dev->chg_susp_n_gpio);
  661. err_susp_gpio:
  662. gpio_free(dev->stat_gpio);
  663. err_stat_gpio:
  664. kfree(smb350_dev);
  665. smb350_dev = NULL;
  666. pr_info("FAIL.\n");
  667. return ret;
  668. }
  669. static int __devexit smb350_remove(struct i2c_client *client)
  670. {
  671. struct smb350_device *dev = i2c_get_clientdata(client);
  672. power_supply_unregister(&dev->dc_psy);
  673. gpio_free(dev->chg_en_n_gpio);
  674. gpio_free(dev->chg_susp_n_gpio);
  675. if (dev->stat_gpio)
  676. gpio_free(dev->stat_gpio);
  677. if (dev->irq)
  678. free_irq(dev->irq, dev);
  679. if (dev->dent)
  680. debugfs_remove_recursive(dev->dent);
  681. kfree(smb350_dev);
  682. smb350_dev = NULL;
  683. return 0;
  684. }
  685. static const struct i2c_device_id smb350_id[] = {
  686. {SMB350_NAME, 0},
  687. {},
  688. };
  689. MODULE_DEVICE_TABLE(i2c, smb350_id);
  690. static const struct of_device_id smb350_match[] = {
  691. { .compatible = "summit,smb350-charger", },
  692. { },
  693. };
  694. static struct i2c_driver smb350_driver = {
  695. .driver = {
  696. .name = SMB350_NAME,
  697. .owner = THIS_MODULE,
  698. .of_match_table = of_match_ptr(smb350_match),
  699. },
  700. .probe = smb350_probe,
  701. .remove = __devexit_p(smb350_remove),
  702. .id_table = smb350_id,
  703. };
  704. static int __init smb350_init(void)
  705. {
  706. return i2c_add_driver(&smb350_driver);
  707. }
  708. module_init(smb350_init);
  709. static void __exit smb350_exit(void)
  710. {
  711. return i2c_del_driver(&smb350_driver);
  712. }
  713. module_exit(smb350_exit);
  714. MODULE_DESCRIPTION("Driver for SMB350 charger chip");
  715. MODULE_LICENSE("GPL v2");
  716. MODULE_ALIAS("i2c:" SMB350_NAME);