sec_charger.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * sec_charger.c
  3. * Samsung Mobile Charger Driver
  4. *
  5. * Copyright (C) 2012 Samsung Electronics
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define DEBUG
  13. #include <linux/battery/sec_charger.h>
  14. #include <linux/of_gpio.h>
  15. static struct device_attribute sec_charger_attrs[] = {
  16. SEC_CHARGER_ATTR(reg),
  17. SEC_CHARGER_ATTR(data),
  18. SEC_CHARGER_ATTR(regs),
  19. };
  20. static enum power_supply_property sec_charger_props[] = {
  21. POWER_SUPPLY_PROP_STATUS,
  22. POWER_SUPPLY_PROP_CHARGE_TYPE,
  23. POWER_SUPPLY_PROP_HEALTH,
  24. POWER_SUPPLY_PROP_ONLINE,
  25. POWER_SUPPLY_PROP_CURRENT_MAX,
  26. POWER_SUPPLY_PROP_CURRENT_AVG,
  27. POWER_SUPPLY_PROP_CURRENT_NOW,
  28. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  29. };
  30. static int sec_chg_get_property(struct power_supply *psy,
  31. enum power_supply_property psp,
  32. union power_supply_propval *val)
  33. {
  34. struct sec_charger_info *charger =
  35. container_of(psy, struct sec_charger_info, psy_chg);
  36. switch (psp) {
  37. case POWER_SUPPLY_PROP_CURRENT_MAX: /* input current limit set */
  38. val->intval = charger->charging_current_max;
  39. break;
  40. case POWER_SUPPLY_PROP_ONLINE:
  41. case POWER_SUPPLY_PROP_STATUS:
  42. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  43. case POWER_SUPPLY_PROP_HEALTH:
  44. case POWER_SUPPLY_PROP_CURRENT_AVG: /* charging current */
  45. /* calculated input current limit value */
  46. case POWER_SUPPLY_PROP_CURRENT_NOW:
  47. if (!sec_hal_chg_get_property(charger->client, psp, val))
  48. return -EINVAL;
  49. break;
  50. default:
  51. return -EINVAL;
  52. }
  53. return 0;
  54. }
  55. static int sec_chg_set_property(struct power_supply *psy,
  56. enum power_supply_property psp,
  57. const union power_supply_propval *val)
  58. {
  59. struct sec_charger_info *charger =
  60. container_of(psy, struct sec_charger_info, psy_chg);
  61. union power_supply_propval input_value;
  62. switch (psp) {
  63. case POWER_SUPPLY_PROP_STATUS:
  64. charger->status = val->intval;
  65. break;
  66. /* val->intval : type */
  67. case POWER_SUPPLY_PROP_ONLINE:
  68. charger->cable_type = val->intval;
  69. if (val->intval == POWER_SUPPLY_TYPE_BATTERY)
  70. charger->is_charging = false;
  71. else
  72. charger->is_charging = true;
  73. /* current setting */
  74. if (!(charger->pdata->cable_source_type &
  75. SEC_BATTERY_CABLE_SOURCE_EXTENDED)) {
  76. charger->charging_current_max =
  77. charger->pdata->charging_current[
  78. val->intval].input_current_limit;
  79. charger->charging_current =
  80. charger->pdata->charging_current[
  81. val->intval].fast_charging_current;
  82. }
  83. if (!sec_hal_chg_set_property(charger->client, psp, val))
  84. return -EINVAL;
  85. break;
  86. /* val->intval : input current limit set */
  87. case POWER_SUPPLY_PROP_CURRENT_MAX:
  88. charger->charging_current_max = val->intval;
  89. /* to control charging current,
  90. * use input current limit and set charging current as much as possible
  91. * so we only control input current limit to control charge current
  92. */
  93. case POWER_SUPPLY_PROP_CURRENT_NOW:
  94. if (!sec_hal_chg_set_property(charger->client, psp, val))
  95. return -EINVAL;
  96. break;
  97. /* val->intval : charging current */
  98. case POWER_SUPPLY_PROP_CURRENT_AVG:
  99. charger->charging_current = val->intval;
  100. if (!sec_hal_chg_set_property(charger->client, psp, val))
  101. return -EINVAL;
  102. break;
  103. /* val->intval : SIOP level (%)
  104. * SIOP charging current setting
  105. */
  106. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  107. /* change val as charging current by SIOP level
  108. * do NOT change initial charging current setting
  109. */
  110. input_value.intval =
  111. charger->charging_current * val->intval / 100;
  112. /* charging current should be over than USB charging current */
  113. if (charger->pdata->chg_functions_setting &
  114. SEC_CHARGER_MINIMUM_SIOP_CHARGING_CURRENT) {
  115. if (input_value.intval > 0 &&
  116. input_value.intval <
  117. charger->pdata->charging_current[
  118. POWER_SUPPLY_TYPE_USB].fast_charging_current)
  119. input_value.intval =
  120. charger->pdata->charging_current[
  121. POWER_SUPPLY_TYPE_USB].fast_charging_current;
  122. }
  123. /* set charging current as new value */
  124. if (!sec_hal_chg_set_property(charger->client,
  125. POWER_SUPPLY_PROP_CURRENT_AVG, &input_value))
  126. return -EINVAL;
  127. break;
  128. default:
  129. return -EINVAL;
  130. }
  131. return 0;
  132. }
  133. static void sec_chg_isr_work(struct work_struct *work)
  134. {
  135. struct sec_charger_info *charger =
  136. container_of(work, struct sec_charger_info, isr_work.work);
  137. union power_supply_propval val;
  138. int full_check_type;
  139. dev_info(&charger->client->dev,
  140. "%s: Charger Interrupt\n", __func__);
  141. psy_do_property("battery", get,
  142. POWER_SUPPLY_PROP_CHARGE_NOW, val);
  143. if (val.intval == SEC_BATTERY_CHARGING_1ST)
  144. full_check_type = charger->pdata->full_check_type;
  145. else
  146. full_check_type = charger->pdata->full_check_type_2nd;
  147. if (full_check_type == SEC_BATTERY_FULLCHARGED_CHGINT) {
  148. if (!sec_hal_chg_get_property(charger->client,
  149. POWER_SUPPLY_PROP_STATUS, &val))
  150. return;
  151. switch (val.intval) {
  152. case POWER_SUPPLY_STATUS_DISCHARGING:
  153. dev_err(&charger->client->dev,
  154. "%s: Interrupted but Discharging\n", __func__);
  155. break;
  156. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  157. dev_err(&charger->client->dev,
  158. "%s: Interrupted but NOT Charging\n", __func__);
  159. break;
  160. case POWER_SUPPLY_STATUS_FULL:
  161. dev_info(&charger->client->dev,
  162. "%s: Interrupted by Full\n", __func__);
  163. psy_do_property("battery", set,
  164. POWER_SUPPLY_PROP_STATUS, val);
  165. break;
  166. case POWER_SUPPLY_STATUS_CHARGING:
  167. dev_err(&charger->client->dev,
  168. "%s: Interrupted but Charging\n", __func__);
  169. break;
  170. case POWER_SUPPLY_STATUS_UNKNOWN:
  171. default:
  172. dev_err(&charger->client->dev,
  173. "%s: Invalid Charger Status\n", __func__);
  174. break;
  175. }
  176. }
  177. if (charger->pdata->ovp_uvlo_check_type ==
  178. SEC_BATTERY_OVP_UVLO_CHGINT) {
  179. if (!sec_hal_chg_get_property(charger->client,
  180. POWER_SUPPLY_PROP_HEALTH, &val))
  181. return;
  182. switch (val.intval) {
  183. case POWER_SUPPLY_HEALTH_OVERHEAT:
  184. case POWER_SUPPLY_HEALTH_COLD:
  185. dev_err(&charger->client->dev,
  186. "%s: Interrupted but Hot/Cold\n", __func__);
  187. break;
  188. case POWER_SUPPLY_HEALTH_DEAD:
  189. dev_err(&charger->client->dev,
  190. "%s: Interrupted but Dead\n", __func__);
  191. break;
  192. case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
  193. case POWER_SUPPLY_HEALTH_UNDERVOLTAGE:
  194. dev_info(&charger->client->dev,
  195. "%s: Interrupted by OVP/UVLO\n", __func__);
  196. psy_do_property("battery", set,
  197. POWER_SUPPLY_PROP_HEALTH, val);
  198. break;
  199. case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
  200. dev_err(&charger->client->dev,
  201. "%s: Interrupted but Unspec\n", __func__);
  202. break;
  203. case POWER_SUPPLY_HEALTH_GOOD:
  204. dev_err(&charger->client->dev,
  205. "%s: Interrupted but Good\n", __func__);
  206. break;
  207. case POWER_SUPPLY_HEALTH_UNKNOWN:
  208. default:
  209. dev_err(&charger->client->dev,
  210. "%s: Invalid Charger Health\n", __func__);
  211. break;
  212. }
  213. }
  214. if (charger->pdata->cable_check_type & SEC_BATTERY_CABLE_CHECK_CHGINT) {
  215. if (!sec_hal_chg_get_property(charger->client,
  216. POWER_SUPPLY_PROP_ONLINE, &val))
  217. return;
  218. /* use SEC_BATTERY_CABLE_SOURCE_EXTERNAL for cable_source_type
  219. * charger would call battery driver to set ONLINE property
  220. * check battery driver loaded or not
  221. */
  222. if (get_power_supply_by_name("battery")) {
  223. psy_do_property("battery", set,
  224. POWER_SUPPLY_PROP_ONLINE, val);
  225. } else
  226. charger->pdata->check_cable_result_callback(val.intval);
  227. }
  228. }
  229. static irqreturn_t sec_chg_irq_thread(int irq, void *irq_data)
  230. {
  231. struct sec_charger_info *charger = irq_data;
  232. schedule_delayed_work(&charger->isr_work, 0);
  233. return IRQ_HANDLED;
  234. }
  235. static int sec_chg_create_attrs(struct device *dev)
  236. {
  237. int i, rc;
  238. for (i = 0; i < ARRAY_SIZE(sec_charger_attrs); i++) {
  239. rc = device_create_file(dev, &sec_charger_attrs[i]);
  240. if (rc)
  241. goto create_attrs_failed;
  242. }
  243. goto create_attrs_succeed;
  244. create_attrs_failed:
  245. dev_err(dev, "%s: failed (%d)\n", __func__, rc);
  246. while (i--)
  247. device_remove_file(dev, &sec_charger_attrs[i]);
  248. create_attrs_succeed:
  249. return rc;
  250. }
  251. ssize_t sec_chg_show_attrs(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. const ptrdiff_t offset = attr - sec_charger_attrs;
  255. int i = 0;
  256. switch (offset) {
  257. case CHG_REG:
  258. case CHG_DATA:
  259. case CHG_REGS:
  260. i = sec_hal_chg_show_attrs(dev, offset, buf);
  261. break;
  262. default:
  263. i = -EINVAL;
  264. break;
  265. }
  266. return i;
  267. }
  268. ssize_t sec_chg_store_attrs(struct device *dev,
  269. struct device_attribute *attr,
  270. const char *buf, size_t count)
  271. {
  272. const ptrdiff_t offset = attr - sec_charger_attrs;
  273. int ret = 0;
  274. switch (offset) {
  275. case CHG_REG:
  276. case CHG_DATA:
  277. ret = sec_hal_chg_store_attrs(dev, offset, buf, count);
  278. break;
  279. default:
  280. ret = -EINVAL;
  281. break;
  282. }
  283. return ret;
  284. }
  285. #ifdef CONFIG_OF
  286. static int smb358_parse_dt(struct sec_charger_info *charger)
  287. {
  288. struct device_node *np = of_find_node_by_name(NULL, "charger");
  289. sec_battery_platform_data_t *pdata = charger->pdata;
  290. int ret = 0;
  291. int i, len;
  292. const u32 *p;
  293. if (np == NULL) {
  294. pr_err("%s np NULL\n", __func__);
  295. } else {
  296. ret = of_property_read_u32(np, "battery,chg_float_voltage",
  297. &pdata->chg_float_voltage);
  298. ret = of_property_read_u32(np, "battery,ovp_uvlo_check_type",
  299. &pdata->ovp_uvlo_check_type);
  300. ret = of_property_read_u32(np, "battery,full_check_type",
  301. &pdata->full_check_type);
  302. p = of_get_property(np, "battery,input_current_limit", &len);
  303. len = len / sizeof(u32);
  304. pdata->charging_current = kzalloc(sizeof(sec_charging_current_t) * len,
  305. GFP_KERNEL);
  306. for(i = 0; i < len; i++)
  307. pdata->charging_current[i].input_current_limit = be32_to_cpup(((__be32 *)p) + i);
  308. p = of_get_property(np, "battery,fast_charging_current", &len);
  309. len = len / sizeof(u32);
  310. for(i = 0; i < len; i++)
  311. pdata->charging_current[i].fast_charging_current = be32_to_cpup(((__be32 *)p) + i);
  312. p = of_get_property(np, "battery,full_check_current_1st", &len);
  313. len = len / sizeof(u32);
  314. for(i = 0; i < len; i++)
  315. pdata->charging_current[i].full_check_current_1st = be32_to_cpup(((__be32 *)p) + i);
  316. p = of_get_property(np, "battery,full_check_current_2nd", &len);
  317. len = len / sizeof(u32);
  318. for(i = 0; i < len; i++)
  319. pdata->charging_current[i].full_check_current_2nd = be32_to_cpup(((__be32 *)p) + i);
  320. }
  321. return ret;
  322. }
  323. #else
  324. static int smb358_parse_dt(struct max77803_charger_data *charger)
  325. {
  326. return 0;
  327. }
  328. #endif
  329. static int __devinit sec_charger_probe(
  330. struct i2c_client *client,
  331. const struct i2c_device_id *id)
  332. {
  333. struct i2c_adapter *adapter =
  334. to_i2c_adapter(client->dev.parent);
  335. struct sec_charger_info *charger;
  336. int ret = 0;
  337. dev_info(&client->dev,
  338. "%s: SEC Charger Driver Loading\n", __func__);
  339. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  340. return -EIO;
  341. charger = kzalloc(sizeof(*charger), GFP_KERNEL);
  342. if (!charger)
  343. return -ENOMEM;
  344. charger->client = client;
  345. if (client->dev.of_node) {
  346. void * pdata = kzalloc(sizeof(sec_battery_platform_data_t), GFP_KERNEL);
  347. if (!pdata)
  348. goto err_free1;
  349. charger->pdata = pdata;
  350. if (smb358_parse_dt(charger))
  351. dev_err(&client->dev,
  352. "%s : Failed to get charger int\n", __func__);
  353. } else
  354. charger->pdata = client->dev.platform_data;
  355. i2c_set_clientdata(client, charger);
  356. charger->psy_chg.name = "sec-charger";
  357. charger->psy_chg.type = POWER_SUPPLY_TYPE_UNKNOWN;
  358. charger->psy_chg.get_property = sec_chg_get_property;
  359. charger->psy_chg.set_property = sec_chg_set_property;
  360. charger->psy_chg.properties = sec_charger_props;
  361. charger->psy_chg.num_properties = ARRAY_SIZE(sec_charger_props);
  362. if (!charger->pdata->chg_gpio_init()) {
  363. dev_err(&client->dev,
  364. "%s: Failed to Initialize GPIO\n", __func__);
  365. goto err_free;
  366. }
  367. if (!sec_hal_chg_init(charger->client)) {
  368. dev_err(&client->dev,
  369. "%s: Failed to Initialize Charger\n", __func__);
  370. goto err_free;
  371. }
  372. ret = power_supply_register(&client->dev, &charger->psy_chg);
  373. if (ret) {
  374. dev_err(&client->dev,
  375. "%s: Failed to Register psy_chg\n", __func__);
  376. goto err_free;
  377. }
  378. if (charger->pdata->chg_irq) {
  379. INIT_DELAYED_WORK_DEFERRABLE(
  380. &charger->isr_work, sec_chg_isr_work);
  381. ret = request_threaded_irq(charger->pdata->chg_irq,
  382. NULL, sec_chg_irq_thread,
  383. charger->pdata->chg_irq_attr,
  384. "charger-irq", charger);
  385. if (ret) {
  386. dev_err(&client->dev,
  387. "%s: Failed to Reqeust IRQ\n", __func__);
  388. goto err_supply_unreg;
  389. }
  390. ret = enable_irq_wake(charger->pdata->chg_irq);
  391. if (ret < 0)
  392. dev_err(&client->dev,
  393. "%s: Failed to Enable Wakeup Source(%d)\n",
  394. __func__, ret);
  395. }
  396. ret = sec_chg_create_attrs(charger->psy_chg.dev);
  397. if (ret) {
  398. dev_err(&client->dev,
  399. "%s : Failed to create_attrs\n", __func__);
  400. goto err_req_irq;
  401. }
  402. dev_dbg(&client->dev,
  403. "%s: SEC Charger Driver Loaded\n", __func__);
  404. return 0;
  405. err_req_irq:
  406. if (charger->pdata->chg_irq)
  407. free_irq(charger->pdata->chg_irq, charger);
  408. err_supply_unreg:
  409. power_supply_unregister(&charger->psy_chg);
  410. err_free:
  411. kfree(charger->pdata);
  412. err_free1:
  413. kfree(charger);
  414. return ret;
  415. }
  416. static int __devexit sec_charger_remove(
  417. struct i2c_client *client)
  418. {
  419. return 0;
  420. }
  421. static int sec_charger_suspend(struct i2c_client *client,
  422. pm_message_t state)
  423. {
  424. if (!sec_hal_chg_suspend(client))
  425. dev_err(&client->dev,
  426. "%s: Failed to Suspend Charger\n", __func__);
  427. return 0;
  428. }
  429. static int sec_charger_resume(struct i2c_client *client)
  430. {
  431. if (!sec_hal_chg_resume(client))
  432. dev_err(&client->dev,
  433. "%s: Failed to Resume Charger\n", __func__);
  434. return 0;
  435. }
  436. static void sec_charger_shutdown(struct i2c_client *client)
  437. {
  438. #if defined(CONFIG_CHARGER_BQ24260)
  439. sec_hal_chg_shutdown(client);
  440. #endif
  441. }
  442. static const struct i2c_device_id sec_charger_id[] = {
  443. {"sec-charger", 0},
  444. {}
  445. };
  446. MODULE_DEVICE_TABLE(i2c, sec_charger_id);
  447. static struct of_device_id charger_i2c_match_table[] = {
  448. { .compatible = "sec-charger,i2c", },
  449. { },
  450. };
  451. MODULE_DEVICE_TABLE(i2c, charger_i2c_match_table);
  452. static struct i2c_driver sec_charger_driver = {
  453. .driver = {
  454. .name = "sec-charger",
  455. .owner = THIS_MODULE,
  456. .of_match_table = charger_i2c_match_table,
  457. },
  458. .probe = sec_charger_probe,
  459. .remove = __devexit_p(sec_charger_remove),
  460. .suspend = sec_charger_suspend,
  461. .resume = sec_charger_resume,
  462. .shutdown = sec_charger_shutdown,
  463. .id_table = sec_charger_id,
  464. };
  465. static int __init sec_charger_init(void)
  466. {
  467. return i2c_add_driver(&sec_charger_driver);
  468. }
  469. static void __exit sec_charger_exit(void)
  470. {
  471. i2c_del_driver(&sec_charger_driver);
  472. }
  473. module_init(sec_charger_init);
  474. module_exit(sec_charger_exit);
  475. MODULE_DESCRIPTION("Samsung Charger Driver");
  476. MODULE_AUTHOR("Samsung Electronics");
  477. MODULE_LICENSE("GPL");