isl9519q.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /* Copyright (c) 2010-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/msm-charger.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c/isl9519.h>
  26. #include <linux/msm_adc.h>
  27. #include <linux/spinlock.h>
  28. #define CHG_CURRENT_REG 0x14
  29. #define MAX_SYS_VOLTAGE_REG 0x15
  30. #define CONTROL_REG 0x3D
  31. #define MIN_SYS_VOLTAGE_REG 0x3E
  32. #define INPUT_CURRENT_REG 0x3F
  33. #define MANUFACTURER_ID_REG 0xFE
  34. #define DEVICE_ID_REG 0xFF
  35. #define TRCKL_CHG_STATUS_BIT 0x80
  36. #define ISL9519_CHG_PERIOD_SEC 150
  37. struct isl9519q_struct {
  38. struct i2c_client *client;
  39. struct delayed_work charge_work;
  40. int present;
  41. int batt_present;
  42. bool charging;
  43. int chgcurrent;
  44. int term_current;
  45. int input_current;
  46. int max_system_voltage;
  47. int min_system_voltage;
  48. int valid_n_gpio;
  49. struct dentry *dent;
  50. struct msm_hardware_charger adapter_hw_chg;
  51. int suspended;
  52. int charge_at_resume;
  53. struct power_supply dc_psy;
  54. spinlock_t lock;
  55. bool notify_by_pmic;
  56. bool trickle;
  57. };
  58. static struct isl9519q_struct *the_isl_chg;
  59. static int isl9519q_read_reg(struct i2c_client *client, int reg,
  60. u16 *val)
  61. {
  62. int ret;
  63. struct isl9519q_struct *isl_chg;
  64. isl_chg = i2c_get_clientdata(client);
  65. ret = i2c_smbus_read_word_data(isl_chg->client, reg);
  66. if (ret < 0) {
  67. dev_err(&isl_chg->client->dev,
  68. "i2c read fail: can't read from %02x: %d\n", reg, ret);
  69. return -EAGAIN;
  70. } else {
  71. *val = ret;
  72. }
  73. pr_debug("reg=0x%x.val=0x%x.\n", reg, *val);
  74. return 0;
  75. }
  76. static int isl9519q_write_reg(struct i2c_client *client, int reg,
  77. u16 val)
  78. {
  79. int ret;
  80. struct isl9519q_struct *isl_chg;
  81. pr_debug("reg=0x%x.val=0x%x.\n", reg, val);
  82. isl_chg = i2c_get_clientdata(client);
  83. ret = i2c_smbus_write_word_data(isl_chg->client, reg, val);
  84. if (ret < 0) {
  85. dev_err(&isl_chg->client->dev,
  86. "i2c write fail: can't write %02x to %02x: %d\n",
  87. val, reg, ret);
  88. return -EAGAIN;
  89. }
  90. return 0;
  91. }
  92. /**
  93. * Read charge-current via ADC.
  94. *
  95. * The ISL CCMON (charge-current-monitor) pin is connected to
  96. * the PMIC MPP#X pin.
  97. * This not required when notify_by_pmic is used where the PMIC
  98. * uses BMS to notify the ISL on charging-done / charge-resume.
  99. */
  100. static int isl_read_adc(int channel, int *mv_reading)
  101. {
  102. int ret;
  103. void *h;
  104. struct adc_chan_result adc_chan_result;
  105. struct completion conv_complete_evt;
  106. pr_debug("called for %d\n", channel);
  107. ret = adc_channel_open(channel, &h);
  108. if (ret) {
  109. pr_err("couldnt open channel %d ret=%d\n", channel, ret);
  110. goto out;
  111. }
  112. init_completion(&conv_complete_evt);
  113. ret = adc_channel_request_conv(h, &conv_complete_evt);
  114. if (ret) {
  115. pr_err("couldnt request conv channel %d ret=%d\n",
  116. channel, ret);
  117. goto out;
  118. }
  119. ret = wait_for_completion_interruptible(&conv_complete_evt);
  120. if (ret) {
  121. pr_err("wait interrupted channel %d ret=%d\n", channel, ret);
  122. goto out;
  123. }
  124. ret = adc_channel_read_result(h, &adc_chan_result);
  125. if (ret) {
  126. pr_err("couldnt read result channel %d ret=%d\n",
  127. channel, ret);
  128. goto out;
  129. }
  130. ret = adc_channel_close(h);
  131. if (ret)
  132. pr_err("couldnt close channel %d ret=%d\n", channel, ret);
  133. if (mv_reading)
  134. *mv_reading = (int)adc_chan_result.measurement;
  135. pr_debug("done for %d\n", channel);
  136. return adc_chan_result.physical;
  137. out:
  138. *mv_reading = 0;
  139. pr_debug("done with error for %d\n", channel);
  140. return -EINVAL;
  141. }
  142. static bool is_trickle_charging(struct isl9519q_struct *isl_chg)
  143. {
  144. u16 ctrl = 0;
  145. int ret;
  146. ret = isl9519q_read_reg(isl_chg->client, CONTROL_REG, &ctrl);
  147. if (!ret) {
  148. pr_debug("control_reg=0x%x.\n", ctrl);
  149. } else {
  150. dev_err(&isl_chg->client->dev,
  151. "%s couldnt read cntrl reg\n", __func__);
  152. }
  153. if (ctrl & TRCKL_CHG_STATUS_BIT)
  154. return true;
  155. return false;
  156. }
  157. static void isl_adapter_check_ichg(struct isl9519q_struct *isl_chg)
  158. {
  159. int ichg; /* isl charger current */
  160. int mv_reading = 0;
  161. ichg = isl_read_adc(CHANNEL_ADC_BATT_AMON, &mv_reading);
  162. dev_dbg(&isl_chg->client->dev, "%s mv_reading=%d\n",
  163. __func__, mv_reading);
  164. dev_dbg(&isl_chg->client->dev, "%s isl_charger_current=%d\n",
  165. __func__, ichg);
  166. if (ichg >= 0 && ichg <= isl_chg->term_current)
  167. msm_charger_notify_event(&isl_chg->adapter_hw_chg,
  168. CHG_DONE_EVENT);
  169. isl_chg->trickle = is_trickle_charging(isl_chg);
  170. if (isl_chg->trickle)
  171. msm_charger_notify_event(&isl_chg->adapter_hw_chg,
  172. CHG_BATT_BEGIN_FAST_CHARGING);
  173. }
  174. /**
  175. * isl9519q_worker
  176. *
  177. * Periodic task required to kick the ISL HW watchdog to keep
  178. * charging.
  179. *
  180. * @isl9519_work: work context.
  181. */
  182. static void isl9519q_worker(struct work_struct *isl9519_work)
  183. {
  184. struct isl9519q_struct *isl_chg;
  185. isl_chg = container_of(isl9519_work, struct isl9519q_struct,
  186. charge_work.work);
  187. dev_dbg(&isl_chg->client->dev, "%s\n", __func__);
  188. if (!isl_chg->charging) {
  189. pr_debug("stop charging.\n");
  190. isl9519q_write_reg(isl_chg->client, CHG_CURRENT_REG, 0);
  191. return; /* Stop periodic worker */
  192. }
  193. /* Kick the dog by writting to CHG_CURRENT_REG */
  194. isl9519q_write_reg(isl_chg->client, CHG_CURRENT_REG,
  195. isl_chg->chgcurrent);
  196. if (isl_chg->notify_by_pmic)
  197. isl_chg->trickle = is_trickle_charging(isl_chg);
  198. else
  199. isl_adapter_check_ichg(isl_chg);
  200. schedule_delayed_work(&isl_chg->charge_work,
  201. (ISL9519_CHG_PERIOD_SEC * HZ));
  202. }
  203. static int isl9519q_start_charging(struct isl9519q_struct *isl_chg,
  204. int chg_voltage, int chg_current)
  205. {
  206. pr_debug("\n");
  207. if (isl_chg->charging) {
  208. pr_warn("already charging.\n");
  209. return 0;
  210. }
  211. if (isl_chg->suspended) {
  212. pr_warn("suspended - can't start charging.\n");
  213. isl_chg->charge_at_resume = 1;
  214. return 0;
  215. }
  216. dev_dbg(&isl_chg->client->dev,
  217. "%s starting timed work.period=%d seconds.\n",
  218. __func__, (int) ISL9519_CHG_PERIOD_SEC);
  219. /*
  220. * The ISL will start charging from the worker context.
  221. * This API might be called from interrupt context.
  222. */
  223. schedule_delayed_work(&isl_chg->charge_work, 1);
  224. isl_chg->charging = true;
  225. return 0;
  226. }
  227. static int isl9519q_stop_charging(struct isl9519q_struct *isl_chg)
  228. {
  229. pr_debug("\n");
  230. if (!(isl_chg->charging)) {
  231. pr_warn("already not charging.\n");
  232. return 0;
  233. }
  234. if (isl_chg->suspended) {
  235. isl_chg->charge_at_resume = 0;
  236. return 0;
  237. }
  238. dev_dbg(&isl_chg->client->dev, "%s\n", __func__);
  239. isl_chg->charging = false;
  240. isl_chg->trickle = false;
  241. /*
  242. * The ISL will stop charging from the worker context.
  243. * This API might be called from interrupt context.
  244. */
  245. schedule_delayed_work(&isl_chg->charge_work, 1);
  246. return 0;
  247. }
  248. static int isl_adapter_start_charging(struct msm_hardware_charger *hw_chg,
  249. int chg_voltage, int chg_current)
  250. {
  251. int rc;
  252. struct isl9519q_struct *isl_chg;
  253. isl_chg = container_of(hw_chg, struct isl9519q_struct, adapter_hw_chg);
  254. rc = isl9519q_start_charging(isl_chg, chg_voltage, chg_current);
  255. return rc;
  256. }
  257. static int isl_adapter_stop_charging(struct msm_hardware_charger *hw_chg)
  258. {
  259. int rc;
  260. struct isl9519q_struct *isl_chg;
  261. isl_chg = container_of(hw_chg, struct isl9519q_struct, adapter_hw_chg);
  262. rc = isl9519q_stop_charging(isl_chg);
  263. return rc;
  264. }
  265. static int isl9519q_charging_switched(struct msm_hardware_charger *hw_chg)
  266. {
  267. struct isl9519q_struct *isl_chg;
  268. isl_chg = container_of(hw_chg, struct isl9519q_struct, adapter_hw_chg);
  269. dev_dbg(&isl_chg->client->dev, "%s\n", __func__);
  270. return 0;
  271. }
  272. static irqreturn_t isl_valid_handler(int irq, void *dev_id)
  273. {
  274. int val;
  275. struct isl9519q_struct *isl_chg;
  276. struct i2c_client *client = dev_id;
  277. isl_chg = i2c_get_clientdata(client);
  278. val = gpio_get_value_cansleep(isl_chg->valid_n_gpio);
  279. if (val < 0) {
  280. dev_err(&isl_chg->client->dev,
  281. "%s gpio_get_value failed for %d ret=%d\n", __func__,
  282. isl_chg->valid_n_gpio, val);
  283. goto err;
  284. }
  285. dev_dbg(&isl_chg->client->dev, "%s val=%d\n", __func__, val);
  286. if (val) {
  287. if (isl_chg->present == 1) {
  288. msm_charger_notify_event(&isl_chg->adapter_hw_chg,
  289. CHG_REMOVED_EVENT);
  290. isl_chg->present = 0;
  291. }
  292. } else {
  293. if (isl_chg->present == 0) {
  294. msm_charger_notify_event(&isl_chg->adapter_hw_chg,
  295. CHG_INSERTED_EVENT);
  296. isl_chg->present = 1;
  297. }
  298. }
  299. err:
  300. return IRQ_HANDLED;
  301. }
  302. static enum power_supply_property pm_power_props[] = {
  303. POWER_SUPPLY_PROP_ONLINE,
  304. POWER_SUPPLY_PROP_CURRENT_MAX,
  305. POWER_SUPPLY_PROP_CHARGE_TYPE,
  306. };
  307. static char *pm_power_supplied_to[] = {
  308. "battery",
  309. };
  310. static int get_prop_charge_type(struct isl9519q_struct *isl_chg)
  311. {
  312. if (!isl_chg->present)
  313. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  314. if (isl_chg->trickle)
  315. return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  316. if (isl_chg->charging)
  317. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  318. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  319. }
  320. static int pm_power_get_property(struct power_supply *psy,
  321. enum power_supply_property psp,
  322. union power_supply_propval *val)
  323. {
  324. struct isl9519q_struct *isl_chg = container_of(psy,
  325. struct isl9519q_struct,
  326. dc_psy);
  327. switch (psp) {
  328. case POWER_SUPPLY_PROP_CURRENT_MAX:
  329. val->intval = isl_chg->chgcurrent;
  330. break;
  331. case POWER_SUPPLY_PROP_ONLINE:
  332. val->intval = (int)isl_chg->present;
  333. break;
  334. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  335. val->intval = get_prop_charge_type(isl_chg);
  336. break;
  337. default:
  338. return -EINVAL;
  339. }
  340. return 0;
  341. }
  342. static int pm_power_set_property(struct power_supply *psy,
  343. enum power_supply_property psp,
  344. const union power_supply_propval *val)
  345. {
  346. struct isl9519q_struct *isl_chg = container_of(psy,
  347. struct isl9519q_struct,
  348. dc_psy);
  349. unsigned long flags;
  350. int rc;
  351. switch (psp) {
  352. case POWER_SUPPLY_PROP_ONLINE:
  353. if (val->intval) {
  354. isl_chg->present = val->intval;
  355. } else {
  356. isl_chg->present = 0;
  357. if (isl_chg->charging)
  358. goto stop_charging;
  359. }
  360. break;
  361. case POWER_SUPPLY_PROP_CURRENT_MAX:
  362. if (val->intval) {
  363. if (isl_chg->chgcurrent != val->intval)
  364. return -EINVAL;
  365. }
  366. break;
  367. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  368. if (val->intval && isl_chg->present) {
  369. if (val->intval == POWER_SUPPLY_CHARGE_TYPE_FAST)
  370. goto start_charging;
  371. if (val->intval == POWER_SUPPLY_CHARGE_TYPE_NONE)
  372. goto stop_charging;
  373. } else {
  374. return -EINVAL;
  375. }
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. power_supply_changed(&isl_chg->dc_psy);
  381. return 0;
  382. start_charging:
  383. spin_lock_irqsave(&isl_chg->lock, flags);
  384. rc = isl9519q_start_charging(isl_chg, 0, isl_chg->chgcurrent);
  385. if (rc)
  386. pr_err("Failed to start charging rc=%d\n", rc);
  387. spin_unlock_irqrestore(&isl_chg->lock, flags);
  388. power_supply_changed(&isl_chg->dc_psy);
  389. return rc;
  390. stop_charging:
  391. spin_lock_irqsave(&isl_chg->lock, flags);
  392. rc = isl9519q_stop_charging(isl_chg);
  393. if (rc)
  394. pr_err("Failed to start charging rc=%d\n", rc);
  395. spin_unlock_irqrestore(&isl_chg->lock, flags);
  396. power_supply_changed(&isl_chg->dc_psy);
  397. return rc;
  398. }
  399. #define MAX_VOLTAGE_REG_MASK 0x3FF0
  400. #define MIN_VOLTAGE_REG_MASK 0x3F00
  401. #define DEFAULT_MAX_VOLTAGE_REG_VALUE 0x1070
  402. #define DEFAULT_MIN_VOLTAGE_REG_VALUE 0x0D00
  403. static int __devinit isl9519q_init_adapter(struct isl9519q_struct *isl_chg)
  404. {
  405. int ret;
  406. struct i2c_client *client = isl_chg->client;
  407. struct isl_platform_data *pdata = client->dev.platform_data;
  408. isl_chg->adapter_hw_chg.type = CHG_TYPE_AC;
  409. isl_chg->adapter_hw_chg.rating = 2;
  410. isl_chg->adapter_hw_chg.name = "isl-adapter";
  411. isl_chg->adapter_hw_chg.start_charging = isl_adapter_start_charging;
  412. isl_chg->adapter_hw_chg.stop_charging = isl_adapter_stop_charging;
  413. isl_chg->adapter_hw_chg.charging_switched = isl9519q_charging_switched;
  414. ret = gpio_request(pdata->valid_n_gpio, "isl_charger_valid");
  415. if (ret) {
  416. dev_err(&client->dev, "%s gpio_request failed "
  417. "for %d ret=%d\n",
  418. __func__, pdata->valid_n_gpio, ret);
  419. goto out;
  420. }
  421. ret = msm_charger_register(&isl_chg->adapter_hw_chg);
  422. if (ret) {
  423. dev_err(&client->dev,
  424. "%s msm_charger_register failed for ret =%d\n",
  425. __func__, ret);
  426. goto free_gpio;
  427. }
  428. ret = request_threaded_irq(client->irq, NULL,
  429. isl_valid_handler,
  430. IRQF_TRIGGER_FALLING |
  431. IRQF_TRIGGER_RISING,
  432. "isl_charger_valid", client);
  433. if (ret) {
  434. dev_err(&client->dev,
  435. "%s request_threaded_irq failed "
  436. "for %d ret =%d\n",
  437. __func__, client->irq, ret);
  438. goto unregister;
  439. }
  440. irq_set_irq_wake(client->irq, 1);
  441. ret = gpio_get_value_cansleep(isl_chg->valid_n_gpio);
  442. if (ret < 0) {
  443. dev_err(&client->dev,
  444. "%s gpio_get_value failed for %d ret=%d\n",
  445. __func__, pdata->valid_n_gpio, ret);
  446. /* assume absent */
  447. ret = 1;
  448. }
  449. if (!ret) {
  450. msm_charger_notify_event(&isl_chg->adapter_hw_chg,
  451. CHG_INSERTED_EVENT);
  452. isl_chg->present = 1;
  453. }
  454. return 0;
  455. unregister:
  456. msm_charger_unregister(&isl_chg->adapter_hw_chg);
  457. free_gpio:
  458. gpio_free(pdata->valid_n_gpio);
  459. out:
  460. return ret;
  461. }
  462. static int __devinit isl9519q_init_ext_chg(struct isl9519q_struct *isl_chg)
  463. {
  464. int ret;
  465. isl_chg->dc_psy.name = "dc";
  466. isl_chg->dc_psy.type = POWER_SUPPLY_TYPE_MAINS;
  467. isl_chg->dc_psy.supplied_to = pm_power_supplied_to;
  468. isl_chg->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to);
  469. isl_chg->dc_psy.properties = pm_power_props;
  470. isl_chg->dc_psy.num_properties = ARRAY_SIZE(pm_power_props);
  471. isl_chg->dc_psy.get_property = pm_power_get_property;
  472. isl_chg->dc_psy.set_property = pm_power_set_property;
  473. ret = power_supply_register(&isl_chg->client->dev, &isl_chg->dc_psy);
  474. if (ret) {
  475. pr_err("failed to register dc charger.ret=%d.\n", ret);
  476. return ret;
  477. }
  478. return 0;
  479. }
  480. static int set_reg(void *data, u64 val)
  481. {
  482. int addr = (int)data;
  483. int ret;
  484. u16 temp;
  485. temp = (u16) val;
  486. ret = isl9519q_write_reg(the_isl_chg->client, addr, temp);
  487. if (ret) {
  488. pr_err("isl9519q_write_reg to %x value =%d errored = %d\n",
  489. addr, temp, ret);
  490. return -EAGAIN;
  491. }
  492. return 0;
  493. }
  494. static int get_reg(void *data, u64 *val)
  495. {
  496. int addr = (int)data;
  497. int ret;
  498. u16 temp;
  499. ret = isl9519q_read_reg(the_isl_chg->client, addr, &temp);
  500. if (ret) {
  501. pr_err("isl9519q_read_reg to %x value =%d errored = %d\n",
  502. addr, temp, ret);
  503. return -EAGAIN;
  504. }
  505. *val = temp;
  506. return 0;
  507. }
  508. DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
  509. static void create_debugfs_entries(struct isl9519q_struct *isl_chg)
  510. {
  511. isl_chg->dent = debugfs_create_dir("isl9519q", NULL);
  512. if (IS_ERR(isl_chg->dent)) {
  513. pr_err("isl9519q driver couldn't create debugfs dir\n");
  514. return;
  515. }
  516. debugfs_create_file("CHG_CURRENT_REG", 0644, isl_chg->dent,
  517. (void *) CHG_CURRENT_REG, &reg_fops);
  518. debugfs_create_file("MAX_SYS_VOLTAGE_REG", 0644, isl_chg->dent,
  519. (void *) MAX_SYS_VOLTAGE_REG, &reg_fops);
  520. debugfs_create_file("CONTROL_REG", 0644, isl_chg->dent,
  521. (void *) CONTROL_REG, &reg_fops);
  522. debugfs_create_file("MIN_SYS_VOLTAGE_REG", 0644, isl_chg->dent,
  523. (void *) MIN_SYS_VOLTAGE_REG, &reg_fops);
  524. debugfs_create_file("INPUT_CURRENT_REG", 0644, isl_chg->dent,
  525. (void *) INPUT_CURRENT_REG, &reg_fops);
  526. debugfs_create_file("MANUFACTURER_ID_REG", 0644, isl_chg->dent,
  527. (void *) MANUFACTURER_ID_REG, &reg_fops);
  528. debugfs_create_file("DEVICE_ID_REG", 0644, isl_chg->dent,
  529. (void *) DEVICE_ID_REG, &reg_fops);
  530. }
  531. static void remove_debugfs_entries(struct isl9519q_struct *isl_chg)
  532. {
  533. if (isl_chg->dent)
  534. debugfs_remove_recursive(isl_chg->dent);
  535. }
  536. static int __devinit isl9519q_hwinit(struct isl9519q_struct *isl_chg)
  537. {
  538. int ret;
  539. ret = isl9519q_write_reg(isl_chg->client, MAX_SYS_VOLTAGE_REG,
  540. isl_chg->max_system_voltage);
  541. if (ret) {
  542. pr_err("Failed to set MAX_SYS_VOLTAGE rc=%d\n", ret);
  543. return ret;
  544. }
  545. ret = isl9519q_write_reg(isl_chg->client, MIN_SYS_VOLTAGE_REG,
  546. isl_chg->min_system_voltage);
  547. if (ret) {
  548. pr_err("Failed to set MIN_SYS_VOLTAGE rc=%d\n", ret);
  549. return ret;
  550. }
  551. if (isl_chg->input_current) {
  552. ret = isl9519q_write_reg(isl_chg->client,
  553. INPUT_CURRENT_REG,
  554. isl_chg->input_current);
  555. if (ret) {
  556. pr_err("Failed to set INPUT_CURRENT rc=%d\n", ret);
  557. return ret;
  558. }
  559. }
  560. return 0;
  561. }
  562. static int __devinit isl9519q_probe(struct i2c_client *client,
  563. const struct i2c_device_id *id)
  564. {
  565. struct isl_platform_data *pdata;
  566. struct isl9519q_struct *isl_chg;
  567. int ret;
  568. ret = 0;
  569. pdata = client->dev.platform_data;
  570. pr_debug("\n");
  571. if (pdata == NULL) {
  572. dev_err(&client->dev, "%s no platform data\n", __func__);
  573. ret = -EINVAL;
  574. goto out;
  575. }
  576. if (!i2c_check_functionality(client->adapter,
  577. I2C_FUNC_SMBUS_WORD_DATA)) {
  578. ret = -EIO;
  579. goto out;
  580. }
  581. isl_chg = kzalloc(sizeof(*isl_chg), GFP_KERNEL);
  582. if (!isl_chg) {
  583. ret = -ENOMEM;
  584. goto out;
  585. }
  586. spin_lock_init(&isl_chg->lock);
  587. INIT_DELAYED_WORK(&isl_chg->charge_work, isl9519q_worker);
  588. isl_chg->client = client;
  589. isl_chg->chgcurrent = pdata->chgcurrent;
  590. isl_chg->term_current = pdata->term_current;
  591. isl_chg->input_current = pdata->input_current;
  592. isl_chg->max_system_voltage = pdata->max_system_voltage;
  593. isl_chg->min_system_voltage = pdata->min_system_voltage;
  594. isl_chg->valid_n_gpio = pdata->valid_n_gpio;
  595. /* h/w ignores lower 7 bits of charging current and input current */
  596. isl_chg->chgcurrent &= ~0x7F;
  597. isl_chg->input_current &= ~0x7F;
  598. /**
  599. * ISL is Notified by PMIC to start/stop charging, rather than
  600. * handling interrupt from ISL for End-Of-Chargring, and
  601. * monitoring the charge-current periodically. The valid_n_gpio
  602. * is also not used, dc-present is detected by PMIC.
  603. */
  604. isl_chg->notify_by_pmic = (client->irq == 0);
  605. i2c_set_clientdata(client, isl_chg);
  606. if (pdata->chg_detection_config) {
  607. ret = pdata->chg_detection_config();
  608. if (ret) {
  609. dev_err(&client->dev, "%s valid config failed ret=%d\n",
  610. __func__, ret);
  611. goto free_isl_chg;
  612. }
  613. }
  614. isl_chg->max_system_voltage &= MAX_VOLTAGE_REG_MASK;
  615. isl_chg->min_system_voltage &= MIN_VOLTAGE_REG_MASK;
  616. if (isl_chg->max_system_voltage == 0)
  617. isl_chg->max_system_voltage = DEFAULT_MAX_VOLTAGE_REG_VALUE;
  618. if (isl_chg->min_system_voltage == 0)
  619. isl_chg->min_system_voltage = DEFAULT_MIN_VOLTAGE_REG_VALUE;
  620. ret = isl9519q_hwinit(isl_chg);
  621. if (ret)
  622. goto free_isl_chg;
  623. if (isl_chg->notify_by_pmic)
  624. ret = isl9519q_init_ext_chg(isl_chg);
  625. else
  626. ret = isl9519q_init_adapter(isl_chg);
  627. if (ret)
  628. goto free_isl_chg;
  629. the_isl_chg = isl_chg;
  630. create_debugfs_entries(isl_chg);
  631. pr_info("OK.\n");
  632. return 0;
  633. free_isl_chg:
  634. kfree(isl_chg);
  635. out:
  636. return ret;
  637. }
  638. static int __devexit isl9519q_remove(struct i2c_client *client)
  639. {
  640. struct isl_platform_data *pdata;
  641. struct isl9519q_struct *isl_chg = i2c_get_clientdata(client);
  642. pdata = client->dev.platform_data;
  643. gpio_free(pdata->valid_n_gpio);
  644. free_irq(client->irq, client);
  645. cancel_delayed_work_sync(&isl_chg->charge_work);
  646. if (isl_chg->notify_by_pmic) {
  647. power_supply_unregister(&isl_chg->dc_psy);
  648. } else {
  649. msm_charger_notify_event(&isl_chg->adapter_hw_chg,
  650. CHG_REMOVED_EVENT);
  651. msm_charger_unregister(&isl_chg->adapter_hw_chg);
  652. }
  653. remove_debugfs_entries(isl_chg);
  654. the_isl_chg = NULL;
  655. kfree(isl_chg);
  656. return 0;
  657. }
  658. static const struct i2c_device_id isl9519q_id[] = {
  659. {"isl9519q", 0},
  660. {},
  661. };
  662. #ifdef CONFIG_PM
  663. static int isl9519q_suspend(struct device *dev)
  664. {
  665. struct isl9519q_struct *isl_chg = dev_get_drvdata(dev);
  666. dev_dbg(&isl_chg->client->dev, "%s\n", __func__);
  667. /*
  668. * do not suspend while we are charging
  669. * because we need to periodically update the register
  670. * for charging to proceed
  671. */
  672. if (isl_chg->charging)
  673. return -EBUSY;
  674. isl_chg->suspended = 1;
  675. return 0;
  676. }
  677. static int isl9519q_resume(struct device *dev)
  678. {
  679. struct isl9519q_struct *isl_chg = dev_get_drvdata(dev);
  680. dev_dbg(&isl_chg->client->dev, "%s\n", __func__);
  681. isl_chg->suspended = 0;
  682. if (isl_chg->charge_at_resume) {
  683. isl_chg->charge_at_resume = 0;
  684. isl9519q_start_charging(isl_chg, 0, 0);
  685. }
  686. return 0;
  687. }
  688. static const struct dev_pm_ops isl9519q_pm_ops = {
  689. .suspend = isl9519q_suspend,
  690. .resume = isl9519q_resume,
  691. };
  692. #endif
  693. static struct i2c_driver isl9519q_driver = {
  694. .driver = {
  695. .name = "isl9519q",
  696. .owner = THIS_MODULE,
  697. #ifdef CONFIG_PM
  698. .pm = &isl9519q_pm_ops,
  699. #endif
  700. },
  701. .probe = isl9519q_probe,
  702. .remove = __devexit_p(isl9519q_remove),
  703. .id_table = isl9519q_id,
  704. };
  705. static int __init isl9519q_init(void)
  706. {
  707. return i2c_add_driver(&isl9519q_driver);
  708. }
  709. late_initcall_sync(isl9519q_init);
  710. static void __exit isl9519q_exit(void)
  711. {
  712. return i2c_del_driver(&isl9519q_driver);
  713. }
  714. module_exit(isl9519q_exit);
  715. MODULE_AUTHOR("Abhijeet Dharmapurikar <adharmap@codeaurora.org>");
  716. MODULE_DESCRIPTION("Driver for ISL9519Q Charger chip");
  717. MODULE_LICENSE("GPL v2");