smb349.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* Copyright (c) 2012 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/i2c/smb349.h>
  24. #include <linux/power_supply.h>
  25. #define SMB349_MASK(BITS, POS) ((unsigned char)(((1 << BITS) - 1) << POS))
  26. /* Register definitions */
  27. #define CHG_CURRENT_REG 0x00
  28. #define CHG_OTHER_CURRENT_REG 0x01
  29. #define VAR_FUNC_REG 0x02
  30. #define FLOAT_VOLTAGE_REG 0x03
  31. #define CHG_CTRL_REG 0x04
  32. #define STAT_TIMER_REG 0x05
  33. #define PIN_ENABLE_CTRL_REG 0x06
  34. #define THERM_CTRL_A_REG 0x07
  35. #define SYSOK_USB3_SELECT_REG 0x08
  36. #define CTRL_FUNCTIONS_REG 0x09
  37. #define OTG_TLIM_THERM_CNTRL_REG 0x0A
  38. #define HARD_SOFT_LIMIT_CELL_TEMP_MONITOR_REG 0x0B
  39. #define FAULT_IRQ_REG 0x0C
  40. #define STATUS_IRQ_REG 0x0D
  41. #define SYSOK_REG 0x0E
  42. #define CMD_A_REG 0x30
  43. #define CMD_B_REG 0x31
  44. #define CMD_C_REG 0x33
  45. #define IRQ_A_REG 0x35
  46. #define IRQ_B_REG 0x36
  47. #define IRQ_C_REG 0x37
  48. #define IRQ_D_REG 0x38
  49. #define IRQ_E_REG 0x39
  50. #define IRQ_F_REG 0x3A
  51. #define STATUS_A_REG 0x3B
  52. #define STATUS_B_REG 0x3C
  53. #define STATUS_C_REG 0x3D
  54. #define STATUS_D_REG 0x3E
  55. #define STATUS_E_REG 0x3F
  56. /* Status bits and masks */
  57. #define CHG_STATUS_MASK SMB349_MASK(2, 1)
  58. #define CHG_ENABLE_STATUS_BIT BIT(0)
  59. /* Control bits and masks */
  60. #define FAST_CHG_CURRENT_MASK SMB349_MASK(4, 4)
  61. #define AC_INPUT_CURRENT_LIMIT_MASK SMB349_MASK(4, 0)
  62. #define PRE_CHG_CURRENT_MASK SMB349_MASK(3, 5)
  63. #define TERMINATION_CURRENT_MASK SMB349_MASK(3, 2)
  64. #define PRE_CHG_TO_FAST_CHG_THRESH_MASK SMB349_MASK(2, 6)
  65. #define FLOAT_VOLTAGE_MASK SMB349_MASK(6, 0)
  66. #define CHG_ENABLE_BIT BIT(1)
  67. #define VOLATILE_W_PERM_BIT BIT(7)
  68. #define USB_SELECTION_BIT BIT(1)
  69. #define SYSTEM_FET_ENABLE_BIT BIT(7)
  70. #define AUTOMATIC_INPUT_CURR_LIMIT_BIT BIT(4)
  71. #define AUTOMATIC_POWER_SOURCE_DETECTION_BIT BIT(2)
  72. #define BATT_OV_END_CHG_BIT BIT(1)
  73. #define VCHG_FUNCTION BIT(0)
  74. #define CURR_TERM_END_CHG_BIT BIT(6)
  75. struct smb349_struct {
  76. struct i2c_client *client;
  77. bool charging;
  78. bool present;
  79. int chg_current_ma;
  80. int en_n_gpio;
  81. int chg_susp_gpio;
  82. struct dentry *dent;
  83. spinlock_t lock;
  84. struct work_struct chg_work;
  85. struct power_supply dc_psy;
  86. };
  87. struct chg_ma_limit_entry {
  88. int fast_chg_ma_limit;
  89. int ac_input_ma_limit;
  90. u8 chg_current_value;
  91. };
  92. static struct smb349_struct *the_smb349_chg;
  93. static int smb349_read_reg(struct i2c_client *client, int reg,
  94. u8 *val)
  95. {
  96. s32 ret;
  97. struct smb349_struct *smb349_chg;
  98. smb349_chg = i2c_get_clientdata(client);
  99. ret = i2c_smbus_read_byte_data(smb349_chg->client, reg);
  100. if (ret < 0) {
  101. dev_err(&smb349_chg->client->dev,
  102. "i2c read fail: can't read from %02x: %d\n", reg, ret);
  103. return ret;
  104. } else {
  105. *val = ret;
  106. }
  107. return 0;
  108. }
  109. static int smb349_write_reg(struct i2c_client *client, int reg,
  110. u8 val)
  111. {
  112. s32 ret;
  113. struct smb349_struct *smb349_chg;
  114. smb349_chg = i2c_get_clientdata(client);
  115. ret = i2c_smbus_write_byte_data(smb349_chg->client, reg, val);
  116. if (ret < 0) {
  117. dev_err(&smb349_chg->client->dev,
  118. "i2c write fail: can't write %02x to %02x: %d\n",
  119. val, reg, ret);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. static int smb349_masked_write(struct i2c_client *client, int reg,
  125. u8 mask, u8 val)
  126. {
  127. s32 rc;
  128. u8 temp;
  129. rc = smb349_read_reg(client, reg, &temp);
  130. if (rc) {
  131. pr_err("smb349_read_reg failed: reg=%03X, rc=%d\n", reg, rc);
  132. return rc;
  133. }
  134. temp &= ~mask;
  135. temp |= val & mask;
  136. rc = smb349_write_reg(client, reg, temp);
  137. if (rc) {
  138. pr_err("smb349_write failed: reg=%03X, rc=%d\n", reg, rc);
  139. return rc;
  140. }
  141. return 0;
  142. }
  143. static enum power_supply_property pm_power_props[] = {
  144. POWER_SUPPLY_PROP_ONLINE,
  145. POWER_SUPPLY_PROP_CURRENT_MAX,
  146. POWER_SUPPLY_PROP_CHARGE_TYPE,
  147. };
  148. static char *pm_power_supplied_to[] = {
  149. "battery",
  150. };
  151. static int get_prop_charge_type(struct smb349_struct *smb349_chg)
  152. {
  153. if (smb349_chg->charging)
  154. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  155. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  156. }
  157. static int pm_power_get_property(struct power_supply *psy,
  158. enum power_supply_property psp,
  159. union power_supply_propval *val)
  160. {
  161. struct smb349_struct *smb349_chg = container_of(psy,
  162. struct smb349_struct,
  163. dc_psy);
  164. switch (psp) {
  165. case POWER_SUPPLY_PROP_CURRENT_MAX:
  166. val->intval = smb349_chg->chg_current_ma;
  167. break;
  168. case POWER_SUPPLY_PROP_ONLINE:
  169. val->intval = (int)smb349_chg->present;
  170. break;
  171. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  172. val->intval = get_prop_charge_type(smb349_chg);
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. return 0;
  178. }
  179. #define SMB349_FAST_CHG_MIN_MA 1000
  180. #define SMB349_FAST_CHG_STEP_MA 200
  181. #define SMB349_FAST_CHG_MAX_MA 4000
  182. #define SMB349_FAST_CHG_SHIFT 4
  183. static int chg_current_set(struct smb349_struct *smb349_chg)
  184. {
  185. u8 temp;
  186. if ((smb349_chg->chg_current_ma < SMB349_FAST_CHG_MIN_MA) ||
  187. (smb349_chg->chg_current_ma > SMB349_FAST_CHG_MAX_MA)) {
  188. pr_err("bad mA=%d asked to set\n", smb349_chg->chg_current_ma);
  189. return -EINVAL;
  190. }
  191. temp = (smb349_chg->chg_current_ma - SMB349_FAST_CHG_MIN_MA)
  192. / SMB349_FAST_CHG_STEP_MA;
  193. temp = temp << SMB349_FAST_CHG_SHIFT;
  194. pr_debug("fastchg limit=%d setting %02x\n",
  195. smb349_chg->chg_current_ma, temp);
  196. return smb349_masked_write(smb349_chg->client, CHG_CURRENT_REG,
  197. FAST_CHG_CURRENT_MASK, temp);
  198. }
  199. static int set_reg(void *data, u64 val)
  200. {
  201. int addr = (int)data;
  202. int ret;
  203. u8 temp;
  204. temp = (u16) val;
  205. ret = smb349_write_reg(the_smb349_chg->client, addr, temp);
  206. if (ret) {
  207. pr_err("smb349_write_reg to %x value =%d errored = %d\n",
  208. addr, temp, ret);
  209. return -EAGAIN;
  210. }
  211. return 0;
  212. }
  213. static int get_reg(void *data, u64 *val)
  214. {
  215. int addr = (int)data;
  216. int ret;
  217. u8 temp = 0;
  218. ret = smb349_read_reg(the_smb349_chg->client, addr, &temp);
  219. if (ret) {
  220. pr_err("smb349_read_reg to %x value =%d errored = %d\n",
  221. addr, temp, ret);
  222. return -EAGAIN;
  223. }
  224. *val = temp;
  225. return 0;
  226. }
  227. DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
  228. static void create_debugfs_entries(struct smb349_struct *smb349_chg)
  229. {
  230. struct dentry *file;
  231. smb349_chg->dent = debugfs_create_dir(SMB349_NAME, NULL);
  232. if (IS_ERR(smb349_chg->dent)) {
  233. pr_err("smb349 driver couldn't create debugfs dir\n");
  234. return;
  235. }
  236. file = debugfs_create_file("CHG_CURRENT_REG", 0644, smb349_chg->dent,
  237. (void *) CHG_CURRENT_REG, &reg_fops);
  238. if (IS_ERR(file)) {
  239. pr_err("smb349 driver couldn't create debugfs files\n");
  240. return;
  241. }
  242. file = debugfs_create_file("CHG_OTHER_CURRENT_REG", 0644,
  243. smb349_chg->dent, (void *) CHG_OTHER_CURRENT_REG, &reg_fops);
  244. if (IS_ERR(file)) {
  245. pr_err("smb349 driver couldn't create debugfs files\n");
  246. return;
  247. }
  248. file = debugfs_create_file("VAR_FUNC_REG", 0644, smb349_chg->dent,
  249. (void *) VAR_FUNC_REG, &reg_fops);
  250. if (IS_ERR(file)) {
  251. pr_err("smb349 driver couldn't create debugfs files\n");
  252. return;
  253. }
  254. file = debugfs_create_file("FLOAT_VOLTAGE_REG", 0644, smb349_chg->dent,
  255. (void *) FLOAT_VOLTAGE_REG, &reg_fops);
  256. if (IS_ERR(file)) {
  257. pr_err("smb349 driver couldn't create debugfs files\n");
  258. return;
  259. }
  260. file = debugfs_create_file("CHG_CTRL_REG", 0644, smb349_chg->dent,
  261. (void *) CHG_CTRL_REG, &reg_fops);
  262. if (IS_ERR(file)) {
  263. pr_err("smb349 driver couldn't create debugfs files\n");
  264. return;
  265. }
  266. file = debugfs_create_file("STAT_TIMER_REG", 0644, smb349_chg->dent,
  267. (void *) STAT_TIMER_REG, &reg_fops);
  268. if (IS_ERR(file)) {
  269. pr_err("smb349 driver couldn't create debugfs files\n");
  270. return;
  271. }
  272. file = debugfs_create_file("PIN_ENABLE_CTRL_REG", 0644,
  273. smb349_chg->dent, (void *) PIN_ENABLE_CTRL_REG, &reg_fops);
  274. if (IS_ERR(file)) {
  275. pr_err("smb349 driver couldn't create debugfs files\n");
  276. return;
  277. }
  278. file = debugfs_create_file("PIN_ENABLE_CTRL_REG", 0644,
  279. smb349_chg->dent, (void *) PIN_ENABLE_CTRL_REG, &reg_fops);
  280. if (IS_ERR(file)) {
  281. pr_err("smb349 driver couldn't create debugfs files\n");
  282. return;
  283. }
  284. file = debugfs_create_file("PIN_ENABLE_CTRL_REG", 0644,
  285. smb349_chg->dent, (void *) PIN_ENABLE_CTRL_REG, &reg_fops);
  286. if (IS_ERR(file)) {
  287. pr_err("smb349 driver couldn't create debugfs files\n");
  288. return;
  289. }
  290. file = debugfs_create_file("THERM_CTRL_A_REG", 0644, smb349_chg->dent,
  291. (void *) THERM_CTRL_A_REG, &reg_fops);
  292. if (IS_ERR(file)) {
  293. pr_err("smb349 driver couldn't create debugfs files\n");
  294. return;
  295. }
  296. file = debugfs_create_file("SYSOK_USB3_SELECT_REG", 0644,
  297. smb349_chg->dent, (void *) SYSOK_USB3_SELECT_REG, &reg_fops);
  298. if (IS_ERR(file)) {
  299. pr_err("smb349 driver couldn't create debugfs files\n");
  300. return;
  301. }
  302. file = debugfs_create_file("CTRL_FUNCTIONS_REG", 0644,
  303. smb349_chg->dent, (void *) CTRL_FUNCTIONS_REG, &reg_fops);
  304. if (IS_ERR(file)) {
  305. pr_err("smb349 driver couldn't create debugfs files\n");
  306. return;
  307. }
  308. file = debugfs_create_file("OTG_TLIM_THERM_CNTRL_REG", 0644,
  309. smb349_chg->dent, (void *) OTG_TLIM_THERM_CNTRL_REG, &reg_fops);
  310. if (IS_ERR(file)) {
  311. pr_err("smb349 driver couldn't create debugfs files\n");
  312. return;
  313. }
  314. file = debugfs_create_file("HARD_SOFT_LIMIT_CELL_TEMP_MONITOR_REG",
  315. 0644, smb349_chg->dent,
  316. (void *) HARD_SOFT_LIMIT_CELL_TEMP_MONITOR_REG, &reg_fops);
  317. if (IS_ERR(file)) {
  318. pr_err("smb349 driver couldn't create debugfs files\n");
  319. return;
  320. }
  321. file = debugfs_create_file("SYSOK_REG", 0644, smb349_chg->dent,
  322. (void *) SYSOK_REG, &reg_fops);
  323. if (IS_ERR(file)) {
  324. pr_err("smb349 driver couldn't create debugfs files\n");
  325. return;
  326. }
  327. file = debugfs_create_file("CMD_A_REG", 0644, smb349_chg->dent,
  328. (void *) CMD_A_REG, &reg_fops);
  329. if (IS_ERR(file)) {
  330. pr_err("smb349 driver couldn't create debugfs files\n");
  331. return;
  332. }
  333. file = debugfs_create_file("CMD_B_REG", 0644, smb349_chg->dent,
  334. (void *) CMD_B_REG, &reg_fops);
  335. if (IS_ERR(file)) {
  336. pr_err("smb349 driver couldn't create debugfs files\n");
  337. return;
  338. }
  339. file = debugfs_create_file("CMD_C_REG", 0644, smb349_chg->dent,
  340. (void *) CMD_C_REG, &reg_fops);
  341. if (IS_ERR(file)) {
  342. pr_err("smb349 driver couldn't create debugfs files\n");
  343. return;
  344. }
  345. file = debugfs_create_file("STATUS_A_REG", 0644, smb349_chg->dent,
  346. (void *) STATUS_A_REG, &reg_fops);
  347. if (IS_ERR(file)) {
  348. pr_err("smb349 driver couldn't create debugfs files\n");
  349. return;
  350. }
  351. file = debugfs_create_file("STATUS_B_REG", 0644, smb349_chg->dent,
  352. (void *) STATUS_B_REG, &reg_fops);
  353. if (IS_ERR(file)) {
  354. pr_err("smb349 driver couldn't create debugfs files\n");
  355. return;
  356. }
  357. file = debugfs_create_file("STATUS_C_REG", 0644, smb349_chg->dent,
  358. (void *) STATUS_C_REG, &reg_fops);
  359. if (IS_ERR(file)) {
  360. pr_err("smb349 driver couldn't create debugfs files\n");
  361. return;
  362. }
  363. file = debugfs_create_file("STATUS_D_REG", 0644, smb349_chg->dent,
  364. (void *) STATUS_D_REG, &reg_fops);
  365. if (IS_ERR(file)) {
  366. pr_err("smb349 driver couldn't create debugfs files\n");
  367. return;
  368. }
  369. file = debugfs_create_file("STATUS_E_REG", 0644, smb349_chg->dent,
  370. (void *) STATUS_E_REG, &reg_fops);
  371. if (IS_ERR(file)) {
  372. pr_err("smb349 driver couldn't create debugfs files\n");
  373. return;
  374. }
  375. }
  376. static void remove_debugfs_entries(struct smb349_struct *smb349_chg)
  377. {
  378. if (smb349_chg->dent)
  379. debugfs_remove_recursive(smb349_chg->dent);
  380. }
  381. static int smb349_hwinit(struct smb349_struct *smb349_chg)
  382. {
  383. int ret;
  384. ret = smb349_write_reg(smb349_chg->client, CMD_A_REG,
  385. VOLATILE_W_PERM_BIT);
  386. if (ret) {
  387. pr_err("Failed to set VOLATILE_W_PERM_BIT rc=%d\n", ret);
  388. return ret;
  389. }
  390. ret = smb349_masked_write(smb349_chg->client, CHG_CTRL_REG,
  391. CURR_TERM_END_CHG_BIT, CURR_TERM_END_CHG_BIT);
  392. if (ret) {
  393. pr_err("Failed to set CURR_TERM_END_CHG_BIT rc=%d\n", ret);
  394. return ret;
  395. }
  396. ret = chg_current_set(smb349_chg);
  397. if (ret) {
  398. pr_err("Failed to set FAST_CHG_CURRENT rc=%d\n", ret);
  399. return ret;
  400. }
  401. return 0;
  402. }
  403. static int smb349_stop_charging(struct smb349_struct *smb349_chg)
  404. {
  405. unsigned long flags;
  406. int rc = 0;
  407. spin_lock_irqsave(&smb349_chg->lock, flags);
  408. pr_debug("stop charging %d\n", smb349_chg->charging);
  409. smb349_chg->charging = 0;
  410. spin_unlock_irqrestore(&smb349_chg->lock, flags);
  411. if (smb349_chg->charging)
  412. rc = schedule_work(&smb349_chg->chg_work);
  413. return rc;
  414. }
  415. static int smb349_start_charging(struct smb349_struct *smb349_chg)
  416. {
  417. unsigned long flags;
  418. int rc;
  419. rc = 0;
  420. spin_lock_irqsave(&smb349_chg->lock, flags);
  421. pr_debug("start charging %d\n", smb349_chg->charging);
  422. smb349_chg->charging = 1;
  423. spin_unlock_irqrestore(&smb349_chg->lock, flags);
  424. if (!smb349_chg->charging)
  425. rc = schedule_work(&smb349_chg->chg_work);
  426. return rc;
  427. }
  428. static int pm_power_set_property(struct power_supply *psy,
  429. enum power_supply_property psp,
  430. const union power_supply_propval *val)
  431. {
  432. struct smb349_struct *smb349_chg = container_of(psy,
  433. struct smb349_struct,
  434. dc_psy);
  435. switch (psp) {
  436. case POWER_SUPPLY_PROP_ONLINE:
  437. if (val->intval) {
  438. smb349_chg->present = val->intval;
  439. } else {
  440. smb349_chg->present = 0;
  441. if (smb349_chg->charging)
  442. return smb349_stop_charging(smb349_chg);
  443. }
  444. break;
  445. case POWER_SUPPLY_PROP_CURRENT_MAX:
  446. if (val->intval) {
  447. if (smb349_chg->chg_current_ma != val->intval)
  448. return -EINVAL;
  449. }
  450. break;
  451. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  452. if (val->intval && smb349_chg->present) {
  453. if (val->intval == POWER_SUPPLY_CHARGE_TYPE_FAST)
  454. return smb349_start_charging(smb349_chg);
  455. if (val->intval == POWER_SUPPLY_CHARGE_TYPE_NONE)
  456. return smb349_stop_charging(smb349_chg);
  457. } else {
  458. return -EINVAL;
  459. }
  460. break;
  461. default:
  462. return -EINVAL;
  463. }
  464. power_supply_changed(&smb349_chg->dc_psy);
  465. return 0;
  466. }
  467. static void chg_worker(struct work_struct *work)
  468. {
  469. struct smb349_struct *smb349_chg = container_of(work,
  470. struct smb349_struct, chg_work);
  471. int ret = 0;
  472. gpio_set_value_cansleep(smb349_chg->en_n_gpio, smb349_chg->charging);
  473. /*
  474. * Write non-default values, charger chip reloads from
  475. * non-volatile memory if it was in suspend mode
  476. *
  477. */
  478. if (smb349_chg->charging)
  479. ret = smb349_hwinit(smb349_chg);
  480. if (ret)
  481. pr_err("Failed to re-initilaze registers\n");
  482. power_supply_changed(&smb349_chg->dc_psy);
  483. }
  484. static int __devinit smb349_init_ext_chg(struct smb349_struct *smb349_chg)
  485. {
  486. int ret;
  487. smb349_chg->dc_psy.name = "dc";
  488. smb349_chg->dc_psy.type = POWER_SUPPLY_TYPE_MAINS;
  489. smb349_chg->dc_psy.supplied_to = pm_power_supplied_to;
  490. smb349_chg->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to);
  491. smb349_chg->dc_psy.properties = pm_power_props;
  492. smb349_chg->dc_psy.num_properties = ARRAY_SIZE(pm_power_props);
  493. smb349_chg->dc_psy.get_property = pm_power_get_property;
  494. smb349_chg->dc_psy.set_property = pm_power_set_property;
  495. ret = power_supply_register(&smb349_chg->client->dev,
  496. &smb349_chg->dc_psy);
  497. if (ret) {
  498. pr_err("failed to register power_supply. ret=%d.\n", ret);
  499. return ret;
  500. }
  501. return 0;
  502. }
  503. static int __devinit smb349_probe(struct i2c_client *client,
  504. const struct i2c_device_id *id)
  505. {
  506. const struct smb349_platform_data *pdata;
  507. struct smb349_struct *smb349_chg;
  508. int ret = 0;
  509. pdata = client->dev.platform_data;
  510. if (pdata == NULL) {
  511. dev_err(&client->dev, "%s no platform data\n", __func__);
  512. ret = -EINVAL;
  513. goto out;
  514. }
  515. if (!i2c_check_functionality(client->adapter,
  516. I2C_FUNC_SMBUS_BYTE_DATA)) {
  517. ret = -EIO;
  518. goto out;
  519. }
  520. smb349_chg = kzalloc(sizeof(*smb349_chg), GFP_KERNEL);
  521. if (!smb349_chg) {
  522. ret = -ENOMEM;
  523. goto out;
  524. }
  525. smb349_chg->client = client;
  526. smb349_chg->chg_current_ma = pdata->chg_current_ma;
  527. ret = gpio_request(pdata->chg_susp_gpio, "smb349_suspend");
  528. if (ret) {
  529. dev_err(&client->dev, "%s gpio_request failed for %d ret=%d\n",
  530. __func__, pdata->chg_susp_gpio, ret);
  531. goto free_smb349_chg;
  532. }
  533. smb349_chg->chg_susp_gpio = pdata->chg_susp_gpio;
  534. ret = gpio_request(pdata->en_n_gpio, "smb349_charger_enable");
  535. if (ret) {
  536. dev_err(&client->dev, "%s gpio_request failed for %d ret=%d\n",
  537. __func__, pdata->en_n_gpio, ret);
  538. goto chg_susp_gpio_fail;
  539. }
  540. smb349_chg->en_n_gpio = pdata->en_n_gpio;
  541. i2c_set_clientdata(client, smb349_chg);
  542. ret = smb349_hwinit(smb349_chg);
  543. if (ret)
  544. goto free_smb349_chg;
  545. ret = smb349_init_ext_chg(smb349_chg);
  546. if (ret)
  547. goto chg_en_gpio_fail;
  548. the_smb349_chg = smb349_chg;
  549. spin_lock_init(&smb349_chg->lock);
  550. create_debugfs_entries(smb349_chg);
  551. INIT_WORK(&smb349_chg->chg_work, chg_worker);
  552. pr_info("OK connector present = %d\n", smb349_chg->present);
  553. return 0;
  554. chg_en_gpio_fail:
  555. gpio_free(smb349_chg->en_n_gpio);
  556. chg_susp_gpio_fail:
  557. gpio_free(smb349_chg->chg_susp_gpio);
  558. free_smb349_chg:
  559. kfree(smb349_chg);
  560. out:
  561. return ret;
  562. }
  563. static int __devexit smb349_remove(struct i2c_client *client)
  564. {
  565. const struct smb349_platform_data *pdata;
  566. struct smb349_struct *smb349_chg = i2c_get_clientdata(client);
  567. flush_work(&smb349_chg->chg_work);
  568. pdata = client->dev.platform_data;
  569. power_supply_unregister(&smb349_chg->dc_psy);
  570. gpio_free(pdata->en_n_gpio);
  571. gpio_free(pdata->chg_susp_gpio);
  572. remove_debugfs_entries(smb349_chg);
  573. kfree(smb349_chg);
  574. return 0;
  575. }
  576. static int smb349_suspend(struct device *dev)
  577. {
  578. struct smb349_struct *smb349_chg = dev_get_drvdata(dev);
  579. pr_debug("suspend\n");
  580. if (smb349_chg->charging)
  581. return -EBUSY;
  582. return 0;
  583. }
  584. static int smb349_resume(struct device *dev)
  585. {
  586. pr_debug("resume\n");
  587. return 0;
  588. }
  589. static const struct dev_pm_ops smb349_pm_ops = {
  590. .suspend = smb349_suspend,
  591. .resume = smb349_resume,
  592. };
  593. static const struct i2c_device_id smb349_id[] = {
  594. {SMB349_NAME, 0},
  595. {},
  596. };
  597. MODULE_DEVICE_TABLE(i2c, smb349_id);
  598. static struct i2c_driver smb349_driver = {
  599. .driver = {
  600. .name = SMB349_NAME,
  601. .owner = THIS_MODULE,
  602. .pm = &smb349_pm_ops,
  603. },
  604. .probe = smb349_probe,
  605. .remove = __devexit_p(smb349_remove),
  606. .id_table = smb349_id,
  607. };
  608. static int __init smb349_init(void)
  609. {
  610. return i2c_add_driver(&smb349_driver);
  611. }
  612. module_init(smb349_init);
  613. static void __exit smb349_exit(void)
  614. {
  615. return i2c_del_driver(&smb349_driver);
  616. }
  617. module_exit(smb349_exit);
  618. MODULE_DESCRIPTION("Driver for SMB349 charger chip");
  619. MODULE_LICENSE("GPL v2");
  620. MODULE_ALIAS("i2c:" SMB349_NAME);