smb137b.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /* Copyright (c) 2010-2011 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. #include <linux/i2c.h>
  14. #include <linux/gpio.h>
  15. #include <linux/errno.h>
  16. #include <linux/delay.h>
  17. #include <linux/module.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c/smb137b.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/msm-charger.h>
  25. #define SMB137B_MASK(BITS, POS) ((unsigned char)(((1 << BITS) - 1) << POS))
  26. #define CHG_CURRENT_REG 0x00
  27. #define FAST_CHG_CURRENT_MASK SMB137B_MASK(3, 5)
  28. #define PRE_CHG_CURRENT_MASK SMB137B_MASK(2, 3)
  29. #define TERM_CHG_CURRENT_MASK SMB137B_MASK(2, 1)
  30. #define INPUT_CURRENT_LIMIT_REG 0x01
  31. #define IN_CURRENT_MASK SMB137B_MASK(3, 5)
  32. #define IN_CURRENT_LIMIT_EN_BIT BIT(2)
  33. #define IN_CURRENT_DET_THRESH_MASK SMB137B_MASK(2, 0)
  34. #define FLOAT_VOLTAGE_REG 0x02
  35. #define STAT_OUT_POLARITY_BIT BIT(7)
  36. #define FLOAT_VOLTAGE_MASK SMB137B_MASK(7, 0)
  37. #define CONTROL_A_REG 0x03
  38. #define AUTO_RECHARGE_DIS_BIT BIT(7)
  39. #define CURR_CYCLE_TERM_BIT BIT(6)
  40. #define PRE_TO_FAST_V_MASK SMB137B_MASK(3, 3)
  41. #define TEMP_BEHAV_BIT BIT(2)
  42. #define THERM_NTC_CURR_MODE_BIT BIT(1)
  43. #define THERM_NTC_47KOHM_BIT BIT(0)
  44. #define CONTROL_B_REG 0x04
  45. #define STAT_OUTPUT_MODE_MASK SMB137B_MASK(2, 6)
  46. #define BATT_OV_ENDS_CYCLE_BIT BIT(5)
  47. #define AUTO_PRE_TO_FAST_DIS_BIT BIT(4)
  48. #define SAFETY_TIMER_EN_BIT BIT(3)
  49. #define OTG_LBR_WD_EN_BIT BIT(2)
  50. #define CHG_WD_TIMER_EN_BIT BIT(1)
  51. #define IRQ_OP_MASK BIT(0)
  52. #define PIN_CTRL_REG 0x05
  53. #define AUTO_CHG_EN_BIT BIT(7)
  54. #define AUTO_LBR_EN_BIT BIT(6)
  55. #define OTG_LBR_BIT BIT(5)
  56. #define I2C_PIN_BIT BIT(4)
  57. #define PIN_EN_CTRL_MASK SMB137B_MASK(2, 2)
  58. #define OTG_LBR_PIN_CTRL_MASK SMB137B_MASK(2, 0)
  59. #define OTG_LBR_CTRL_REG 0x06
  60. #define BATT_MISSING_DET_EN_BIT BIT(7)
  61. #define AUTO_RECHARGE_THRESH_MASK BIT(6)
  62. #define USB_DP_DN_DET_EN_MASK BIT(5)
  63. #define OTG_LBR_BATT_CURRENT_LIMIT_MASK SMB137B_MASK(2, 3)
  64. #define OTG_LBR_UVLO_THRESH_MASK SMB137B_MASK(3, 0)
  65. #define FAULT_INTR_REG 0x07
  66. #define SAFETY_TIMER_EXP_MASK SMB137B_MASK(1, 7)
  67. #define BATT_TEMP_UNSAFE_MASK SMB137B_MASK(1, 6)
  68. #define INPUT_OVLO_IVLO_MASK SMB137B_MASK(1, 5)
  69. #define BATT_OVLO_MASK SMB137B_MASK(1, 4)
  70. #define INTERNAL_OVER_TEMP_MASK SMB137B_MASK(1, 2)
  71. #define ENTER_TAPER_CHG_MASK SMB137B_MASK(1, 1)
  72. #define CHG_MASK SMB137B_MASK(1, 0)
  73. #define CELL_TEMP_MON_REG 0x08
  74. #define THERMISTOR_CURR_MASK SMB137B_MASK(2, 6)
  75. #define LOW_TEMP_CHG_INHIBIT_MASK SMB137B_MASK(3, 3)
  76. #define HIGH_TEMP_CHG_INHIBIT_MASK SMB137B_MASK(3, 0)
  77. #define SAFETY_TIMER_THERMAL_SHUTDOWN_REG 0x09
  78. #define DCIN_OVLO_SEL_MASK SMB137B_MASK(2, 7)
  79. #define RELOAD_EN_INPUT_VOLTAGE_MASK SMB137B_MASK(1, 6)
  80. #define THERM_SHUTDN_EN_MASK SMB137B_MASK(1, 5)
  81. #define STANDBY_WD_TIMER_EN_MASK SMB137B_MASK(1, 4)
  82. #define COMPLETE_CHG_TMOUT_MASK SMB137B_MASK(2, 2)
  83. #define PRE_CHG_TMOUT_MASK SMB137B_MASK(2, 0)
  84. #define VSYS_REG 0x0A
  85. #define VSYS_MASK SMB137B_MASK(3, 4)
  86. #define IRQ_RESET_REG 0x30
  87. #define COMMAND_A_REG 0x31
  88. #define VOLATILE_REGS_WRITE_PERM_BIT BIT(7)
  89. #define POR_BIT BIT(6)
  90. #define FAST_CHG_SETTINGS_BIT BIT(5)
  91. #define BATT_CHG_EN_BIT BIT(4)
  92. #define USBIN_MODE_500_BIT BIT(3)
  93. #define USBIN_MODE_HCMODE_BIT BIT(2)
  94. #define OTG_LBR_EN_BIT BIT(1)
  95. #define STAT_OE_BIT BIT(0)
  96. #define STATUS_A_REG 0x32
  97. #define INTERNAL_TEMP_IRQ_STAT BIT(4)
  98. #define DCIN_OV_IRQ_STAT BIT(3)
  99. #define DCIN_UV_IRQ_STAT BIT(2)
  100. #define USBIN_OV_IRQ_STAT BIT(1)
  101. #define USBIN_UV_IRQ_STAT BIT(0)
  102. #define STATUS_B_REG 0x33
  103. #define USB_PIN_STAT BIT(7)
  104. #define USB51_MODE_STAT BIT(6)
  105. #define USB51_HC_MODE_STAT BIT(5)
  106. #define INTERNAL_TEMP_LIMIT_B_STAT BIT(4)
  107. #define DC_IN_OV_STAT BIT(3)
  108. #define DC_IN_UV_STAT BIT(2)
  109. #define USB_IN_OV_STAT BIT(1)
  110. #define USB_IN_UV_STAT BIT(0)
  111. #define STATUS_C_REG 0x34
  112. #define AUTO_IN_CURR_LIMIT_MASK SMB137B_MASK(4, 4)
  113. #define AUTO_IN_CURR_LIMIT_STAT BIT(3)
  114. #define AUTO_SOURCE_DET_COMP_STAT_MASK SMB137B_MASK(2, 1)
  115. #define AUTO_SOURCE_DET_RESULT_STAT BIT(0)
  116. #define STATUS_D_REG 0x35
  117. #define VBATT_LESS_THAN_VSYS_STAT BIT(7)
  118. #define USB_FAIL_STAT BIT(6)
  119. #define BATT_TEMP_STAT_MASK SMB137B_MASK(2, 4)
  120. #define INTERNAL_TEMP_LIMIT_STAT BIT(2)
  121. #define OTG_LBR_MODE_EN_STAT BIT(1)
  122. #define OTG_LBR_VBATT_UVLO_STAT BIT(0)
  123. #define STATUS_E_REG 0x36
  124. #define CHARGE_CYCLE_COUNT_STAT BIT(7)
  125. #define CHARGER_TERM_STAT BIT(6)
  126. #define SAFETY_TIMER_STAT_MASK SMB137B_MASK(2, 4)
  127. #define CHARGER_ERROR_STAT BIT(3)
  128. #define CHARGING_STAT_E SMB137B_MASK(2, 1)
  129. #define CHARGING_EN BIT(0)
  130. #define STATUS_F_REG 0x37
  131. #define WD_IRQ_ACTIVE_STAT BIT(7)
  132. #define OTG_OVERCURRENT_STAT BIT(6)
  133. #define BATT_PRESENT_STAT BIT(4)
  134. #define BATT_OV_LATCHED_STAT BIT(3)
  135. #define CHARGER_OVLO_STAT BIT(2)
  136. #define CHARGER_UVLO_STAT BIT(1)
  137. #define BATT_LOW_STAT BIT(0)
  138. #define STATUS_G_REG 0x38
  139. #define CHARGE_TIMEOUT_IRQ_STAT BIT(7)
  140. #define PRECHARGE_TIMEOUT_IRQ_STAT BIT(6)
  141. #define BATT_HOT_IRQ_STAT BIT(5)
  142. #define BATT_COLD_IRQ_STAT BIT(4)
  143. #define BATT_OV_IRQ_STAT BIT(3)
  144. #define TAPER_CHG_IRQ_STAT BIT(2)
  145. #define FAST_CHG_IRQ_STAT BIT(1)
  146. #define CHARGING_IRQ_STAT BIT(0)
  147. #define STATUS_H_REG 0x39
  148. #define CHARGE_TIMEOUT_STAT BIT(7)
  149. #define PRECHARGE_TIMEOUT_STAT BIT(6)
  150. #define BATT_HOT_STAT BIT(5)
  151. #define BATT_COLD_STAT BIT(4)
  152. #define BATT_OV_STAT BIT(3)
  153. #define TAPER_CHG_STAT BIT(2)
  154. #define FAST_CHG_STAT BIT(1)
  155. #define CHARGING_STAT_H BIT(0)
  156. #define DEV_ID_REG 0x3B
  157. #define COMMAND_B_REG 0x3C
  158. #define THERM_NTC_CURR_VERRIDE BIT(7)
  159. #define SMB137B_CHG_PERIOD ((HZ) * 150)
  160. #define INPUT_CURRENT_REG_DEFAULT 0xE1
  161. #define INPUT_CURRENT_REG_MIN 0x01
  162. #define COMMAND_A_REG_DEFAULT 0xA0
  163. #define COMMAND_A_REG_OTG_MODE 0xA2
  164. #define PIN_CTRL_REG_DEFAULT 0x00
  165. #define PIN_CTRL_REG_CHG_OFF 0x04
  166. #define FAST_CHG_E_STATUS 0x2
  167. #define SMB137B_DEFAULT_BATT_RATING 950
  168. struct smb137b_data {
  169. struct i2c_client *client;
  170. struct delayed_work charge_work;
  171. bool charging;
  172. int chgcurrent;
  173. int cur_charging_mode;
  174. int max_system_voltage;
  175. int min_system_voltage;
  176. int valid_n_gpio;
  177. int batt_status;
  178. int batt_chg_type;
  179. int batt_present;
  180. int min_design;
  181. int max_design;
  182. int batt_mah_rating;
  183. int usb_status;
  184. u8 dev_id_reg;
  185. struct msm_hardware_charger adapter_hw_chg;
  186. };
  187. static unsigned int disabled;
  188. static DEFINE_MUTEX(init_lock);
  189. static unsigned int init_otg_power;
  190. enum charger_stat {
  191. SMB137B_ABSENT,
  192. SMB137B_PRESENT,
  193. SMB137B_ENUMERATED,
  194. };
  195. static struct smb137b_data *usb_smb137b_chg;
  196. static int smb137b_read_reg(struct i2c_client *client, int reg,
  197. u8 *val)
  198. {
  199. s32 ret;
  200. struct smb137b_data *smb137b_chg;
  201. smb137b_chg = i2c_get_clientdata(client);
  202. ret = i2c_smbus_read_byte_data(smb137b_chg->client, reg);
  203. if (ret < 0) {
  204. dev_err(&smb137b_chg->client->dev,
  205. "i2c read fail: can't read from %02x: %d\n", reg, ret);
  206. return ret;
  207. } else
  208. *val = ret;
  209. return 0;
  210. }
  211. static int smb137b_write_reg(struct i2c_client *client, int reg,
  212. u8 val)
  213. {
  214. s32 ret;
  215. struct smb137b_data *smb137b_chg;
  216. smb137b_chg = i2c_get_clientdata(client);
  217. ret = i2c_smbus_write_byte_data(smb137b_chg->client, reg, val);
  218. if (ret < 0) {
  219. dev_err(&smb137b_chg->client->dev,
  220. "i2c write fail: can't write %02x to %02x: %d\n",
  221. val, reg, ret);
  222. return ret;
  223. }
  224. return 0;
  225. }
  226. static ssize_t id_reg_show(struct device *dev, struct device_attribute *attr,
  227. char *buf)
  228. {
  229. struct smb137b_data *smb137b_chg;
  230. smb137b_chg = i2c_get_clientdata(to_i2c_client(dev));
  231. return sprintf(buf, "%02x\n", smb137b_chg->dev_id_reg);
  232. }
  233. static DEVICE_ATTR(id_reg, S_IRUGO | S_IWUSR, id_reg_show, NULL);
  234. #ifdef DEBUG
  235. static void smb137b_dbg_print_status_regs(struct smb137b_data *smb137b_chg)
  236. {
  237. int ret;
  238. u8 temp;
  239. ret = smb137b_read_reg(smb137b_chg->client, STATUS_A_REG, &temp);
  240. dev_dbg(&smb137b_chg->client->dev, "%s A=0x%x\n", __func__, temp);
  241. ret = smb137b_read_reg(smb137b_chg->client, STATUS_B_REG, &temp);
  242. dev_dbg(&smb137b_chg->client->dev, "%s B=0x%x\n", __func__, temp);
  243. ret = smb137b_read_reg(smb137b_chg->client, STATUS_C_REG, &temp);
  244. dev_dbg(&smb137b_chg->client->dev, "%s C=0x%x\n", __func__, temp);
  245. ret = smb137b_read_reg(smb137b_chg->client, STATUS_D_REG, &temp);
  246. dev_dbg(&smb137b_chg->client->dev, "%s D=0x%x\n", __func__, temp);
  247. ret = smb137b_read_reg(smb137b_chg->client, STATUS_E_REG, &temp);
  248. dev_dbg(&smb137b_chg->client->dev, "%s E=0x%x\n", __func__, temp);
  249. ret = smb137b_read_reg(smb137b_chg->client, STATUS_F_REG, &temp);
  250. dev_dbg(&smb137b_chg->client->dev, "%s F=0x%x\n", __func__, temp);
  251. ret = smb137b_read_reg(smb137b_chg->client, STATUS_G_REG, &temp);
  252. dev_dbg(&smb137b_chg->client->dev, "%s G=0x%x\n", __func__, temp);
  253. ret = smb137b_read_reg(smb137b_chg->client, STATUS_H_REG, &temp);
  254. dev_dbg(&smb137b_chg->client->dev, "%s H=0x%x\n", __func__, temp);
  255. }
  256. #else
  257. static void smb137b_dbg_print_status_regs(struct smb137b_data *smb137b_chg)
  258. {
  259. }
  260. #endif
  261. static int smb137b_start_charging(struct msm_hardware_charger *hw_chg,
  262. int chg_voltage, int chg_current)
  263. {
  264. int cmd_val = COMMAND_A_REG_DEFAULT;
  265. u8 temp = 0;
  266. int ret = 0;
  267. struct smb137b_data *smb137b_chg;
  268. smb137b_chg = container_of(hw_chg, struct smb137b_data, adapter_hw_chg);
  269. if (disabled) {
  270. dev_err(&smb137b_chg->client->dev,
  271. "%s called when disabled\n", __func__);
  272. goto out;
  273. }
  274. if (smb137b_chg->charging == true
  275. && smb137b_chg->chgcurrent == chg_current)
  276. /* we are already charging with the same current*/
  277. dev_err(&smb137b_chg->client->dev,
  278. "%s charge with same current %d called again\n",
  279. __func__, chg_current);
  280. dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__);
  281. if (chg_current < 500)
  282. cmd_val &= ~USBIN_MODE_500_BIT;
  283. else if (chg_current == 500)
  284. cmd_val |= USBIN_MODE_500_BIT;
  285. else
  286. cmd_val |= USBIN_MODE_HCMODE_BIT;
  287. smb137b_chg->chgcurrent = chg_current;
  288. smb137b_chg->cur_charging_mode = cmd_val;
  289. /* Due to non-volatile reload feature,always enable volatile
  290. * mirror writes before modifying any 00h~09h control register.
  291. * Current mode needs to be programmed according to what's detected
  292. * Otherwise default 100mA mode might cause VOUTL drop and fail
  293. * the system in case of dead battery.
  294. */
  295. ret = smb137b_write_reg(smb137b_chg->client,
  296. COMMAND_A_REG, cmd_val);
  297. if (ret) {
  298. dev_err(&smb137b_chg->client->dev,
  299. "%s couldn't write to command_reg\n", __func__);
  300. goto out;
  301. }
  302. ret = smb137b_write_reg(smb137b_chg->client,
  303. PIN_CTRL_REG, PIN_CTRL_REG_DEFAULT);
  304. if (ret) {
  305. dev_err(&smb137b_chg->client->dev,
  306. "%s couldn't write to pin ctrl reg\n", __func__);
  307. goto out;
  308. }
  309. smb137b_chg->charging = true;
  310. smb137b_chg->batt_status = POWER_SUPPLY_STATUS_CHARGING;
  311. smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  312. ret = smb137b_read_reg(smb137b_chg->client, STATUS_E_REG, &temp);
  313. if (ret) {
  314. dev_err(&smb137b_chg->client->dev,
  315. "%s couldn't read status e reg %d\n", __func__, ret);
  316. } else {
  317. if (temp & CHARGER_ERROR_STAT) {
  318. dev_err(&smb137b_chg->client->dev,
  319. "%s chg error E=0x%x\n", __func__, temp);
  320. smb137b_dbg_print_status_regs(smb137b_chg);
  321. }
  322. if (((temp & CHARGING_STAT_E) >> 1) >= FAST_CHG_E_STATUS)
  323. smb137b_chg->batt_chg_type
  324. = POWER_SUPPLY_CHARGE_TYPE_FAST;
  325. }
  326. /*schedule charge_work to keep track of battery charging state*/
  327. schedule_delayed_work(&smb137b_chg->charge_work, SMB137B_CHG_PERIOD);
  328. out:
  329. return ret;
  330. }
  331. static int smb137b_stop_charging(struct msm_hardware_charger *hw_chg)
  332. {
  333. int ret = 0;
  334. struct smb137b_data *smb137b_chg;
  335. smb137b_chg = container_of(hw_chg, struct smb137b_data, adapter_hw_chg);
  336. dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__);
  337. if (smb137b_chg->charging == false)
  338. return 0;
  339. smb137b_chg->charging = false;
  340. smb137b_chg->batt_status = POWER_SUPPLY_STATUS_DISCHARGING;
  341. smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
  342. ret = smb137b_write_reg(smb137b_chg->client, COMMAND_A_REG,
  343. smb137b_chg->cur_charging_mode);
  344. if (ret) {
  345. dev_err(&smb137b_chg->client->dev,
  346. "%s couldn't write to command_reg\n", __func__);
  347. goto out;
  348. }
  349. ret = smb137b_write_reg(smb137b_chg->client,
  350. PIN_CTRL_REG, PIN_CTRL_REG_CHG_OFF);
  351. if (ret)
  352. dev_err(&smb137b_chg->client->dev,
  353. "%s couldn't write to pin ctrl reg\n", __func__);
  354. out:
  355. return ret;
  356. }
  357. static int smb137b_charger_switch(struct msm_hardware_charger *hw_chg)
  358. {
  359. struct smb137b_data *smb137b_chg;
  360. smb137b_chg = container_of(hw_chg, struct smb137b_data, adapter_hw_chg);
  361. dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__);
  362. return 0;
  363. }
  364. static irqreturn_t smb137b_valid_handler(int irq, void *dev_id)
  365. {
  366. int val;
  367. struct smb137b_data *smb137b_chg;
  368. struct i2c_client *client = dev_id;
  369. smb137b_chg = i2c_get_clientdata(client);
  370. pr_debug("%s Cable Detected USB inserted\n", __func__);
  371. /*extra delay needed to allow CABLE_DET_N settling down and debounce
  372. before trying to sample its correct value*/
  373. usleep_range(1000, 1200);
  374. val = gpio_get_value_cansleep(smb137b_chg->valid_n_gpio);
  375. if (val < 0) {
  376. dev_err(&smb137b_chg->client->dev,
  377. "%s gpio_get_value failed for %d ret=%d\n", __func__,
  378. smb137b_chg->valid_n_gpio, val);
  379. goto err;
  380. }
  381. dev_dbg(&smb137b_chg->client->dev, "%s val=%d\n", __func__, val);
  382. if (val) {
  383. if (smb137b_chg->usb_status != SMB137B_ABSENT) {
  384. smb137b_chg->usb_status = SMB137B_ABSENT;
  385. msm_charger_notify_event(&smb137b_chg->adapter_hw_chg,
  386. CHG_REMOVED_EVENT);
  387. }
  388. } else {
  389. if (smb137b_chg->usb_status == SMB137B_ABSENT) {
  390. smb137b_chg->usb_status = SMB137B_PRESENT;
  391. msm_charger_notify_event(&smb137b_chg->adapter_hw_chg,
  392. CHG_INSERTED_EVENT);
  393. }
  394. }
  395. err:
  396. return IRQ_HANDLED;
  397. }
  398. #ifdef CONFIG_DEBUG_FS
  399. static struct dentry *dent;
  400. static int debug_fs_otg;
  401. static int otg_get(void *data, u64 *value)
  402. {
  403. *value = debug_fs_otg;
  404. return 0;
  405. }
  406. static int otg_set(void *data, u64 value)
  407. {
  408. smb137b_otg_power(debug_fs_otg);
  409. return 0;
  410. }
  411. DEFINE_SIMPLE_ATTRIBUTE(smb137b_otg_fops, otg_get, otg_set, "%llu\n");
  412. static void smb137b_create_debugfs_entries(struct smb137b_data *smb137b_chg)
  413. {
  414. dent = debugfs_create_dir("smb137b", NULL);
  415. if (dent) {
  416. debugfs_create_file("otg", 0644, dent, NULL, &smb137b_otg_fops);
  417. }
  418. }
  419. static void smb137b_destroy_debugfs_entries(void)
  420. {
  421. if (dent)
  422. debugfs_remove_recursive(dent);
  423. }
  424. #else
  425. static void smb137b_create_debugfs_entries(struct smb137b_data *smb137b_chg)
  426. {
  427. }
  428. static void smb137b_destroy_debugfs_entries(void)
  429. {
  430. }
  431. #endif
  432. static int set_disable_status_param(const char *val, struct kernel_param *kp)
  433. {
  434. int ret;
  435. ret = param_set_int(val, kp);
  436. if (ret)
  437. return ret;
  438. if (usb_smb137b_chg && disabled)
  439. msm_charger_notify_event(&usb_smb137b_chg->adapter_hw_chg,
  440. CHG_DONE_EVENT);
  441. pr_debug("%s disabled =%d\n", __func__, disabled);
  442. return 0;
  443. }
  444. module_param_call(disabled, set_disable_status_param, param_get_uint,
  445. &disabled, 0644);
  446. static void smb137b_charge_sm(struct work_struct *smb137b_work)
  447. {
  448. int ret;
  449. struct smb137b_data *smb137b_chg;
  450. u8 temp = 0;
  451. int notify_batt_changed = 0;
  452. smb137b_chg = container_of(smb137b_work, struct smb137b_data,
  453. charge_work.work);
  454. /*if not charging, exit smb137b charging state transition*/
  455. if (!smb137b_chg->charging)
  456. return;
  457. dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__);
  458. ret = smb137b_read_reg(smb137b_chg->client, STATUS_F_REG, &temp);
  459. if (ret) {
  460. dev_err(&smb137b_chg->client->dev,
  461. "%s couldn't read status f reg %d\n", __func__, ret);
  462. goto out;
  463. }
  464. if (smb137b_chg->batt_present != !(temp & BATT_PRESENT_STAT)) {
  465. smb137b_chg->batt_present = !(temp & BATT_PRESENT_STAT);
  466. notify_batt_changed = 1;
  467. }
  468. if (!smb137b_chg->batt_present)
  469. smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
  470. if (!smb137b_chg->batt_present && smb137b_chg->charging)
  471. msm_charger_notify_event(&smb137b_chg->adapter_hw_chg,
  472. CHG_DONE_EVENT);
  473. if (smb137b_chg->batt_present
  474. && smb137b_chg->charging
  475. && smb137b_chg->batt_chg_type
  476. != POWER_SUPPLY_CHARGE_TYPE_FAST) {
  477. ret = smb137b_read_reg(smb137b_chg->client,
  478. STATUS_E_REG, &temp);
  479. if (ret) {
  480. dev_err(&smb137b_chg->client->dev,
  481. "%s couldn't read cntrl reg\n", __func__);
  482. goto out;
  483. } else {
  484. if (temp & CHARGER_ERROR_STAT) {
  485. dev_err(&smb137b_chg->client->dev,
  486. "%s error E=0x%x\n", __func__, temp);
  487. smb137b_dbg_print_status_regs(smb137b_chg);
  488. }
  489. if (((temp & CHARGING_STAT_E) >> 1)
  490. >= FAST_CHG_E_STATUS) {
  491. smb137b_chg->batt_chg_type
  492. = POWER_SUPPLY_CHARGE_TYPE_FAST;
  493. notify_batt_changed = 1;
  494. msm_charger_notify_event(
  495. &smb137b_chg->adapter_hw_chg,
  496. CHG_BATT_BEGIN_FAST_CHARGING);
  497. } else {
  498. smb137b_chg->batt_chg_type
  499. = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  500. }
  501. }
  502. }
  503. out:
  504. schedule_delayed_work(&smb137b_chg->charge_work,
  505. SMB137B_CHG_PERIOD);
  506. }
  507. static void __smb137b_otg_power(int on)
  508. {
  509. int ret;
  510. if (on) {
  511. ret = smb137b_write_reg(usb_smb137b_chg->client,
  512. PIN_CTRL_REG, PIN_CTRL_REG_CHG_OFF);
  513. if (ret) {
  514. pr_err("%s turning off charging in pin_ctrl err=%d\n",
  515. __func__, ret);
  516. /*
  517. * don't change the command register if charging in
  518. * pin control cannot be turned off
  519. */
  520. return;
  521. }
  522. ret = smb137b_write_reg(usb_smb137b_chg->client,
  523. COMMAND_A_REG, COMMAND_A_REG_OTG_MODE);
  524. if (ret)
  525. pr_err("%s failed turning on OTG mode ret=%d\n",
  526. __func__, ret);
  527. } else {
  528. ret = smb137b_write_reg(usb_smb137b_chg->client,
  529. COMMAND_A_REG, COMMAND_A_REG_DEFAULT);
  530. if (ret)
  531. pr_err("%s failed turning off OTG mode ret=%d\n",
  532. __func__, ret);
  533. ret = smb137b_write_reg(usb_smb137b_chg->client,
  534. PIN_CTRL_REG, PIN_CTRL_REG_DEFAULT);
  535. if (ret)
  536. pr_err("%s failed writing to pn_ctrl ret=%d\n",
  537. __func__, ret);
  538. }
  539. }
  540. static int __devinit smb137b_probe(struct i2c_client *client,
  541. const struct i2c_device_id *id)
  542. {
  543. const struct smb137b_platform_data *pdata;
  544. struct smb137b_data *smb137b_chg;
  545. int ret = 0;
  546. pdata = client->dev.platform_data;
  547. if (pdata == NULL) {
  548. dev_err(&client->dev, "%s no platform data\n", __func__);
  549. ret = -EINVAL;
  550. goto out;
  551. }
  552. if (!i2c_check_functionality(client->adapter,
  553. I2C_FUNC_SMBUS_BYTE_DATA)) {
  554. ret = -EIO;
  555. goto out;
  556. }
  557. smb137b_chg = kzalloc(sizeof(*smb137b_chg), GFP_KERNEL);
  558. if (!smb137b_chg) {
  559. ret = -ENOMEM;
  560. goto out;
  561. }
  562. INIT_DELAYED_WORK(&smb137b_chg->charge_work, smb137b_charge_sm);
  563. smb137b_chg->client = client;
  564. smb137b_chg->valid_n_gpio = pdata->valid_n_gpio;
  565. smb137b_chg->batt_mah_rating = pdata->batt_mah_rating;
  566. if (smb137b_chg->batt_mah_rating == 0)
  567. smb137b_chg->batt_mah_rating = SMB137B_DEFAULT_BATT_RATING;
  568. /*It supports USB-WALL charger and PC USB charger */
  569. smb137b_chg->adapter_hw_chg.type = CHG_TYPE_USB;
  570. smb137b_chg->adapter_hw_chg.rating = pdata->batt_mah_rating;
  571. smb137b_chg->adapter_hw_chg.name = "smb137b-charger";
  572. smb137b_chg->adapter_hw_chg.start_charging = smb137b_start_charging;
  573. smb137b_chg->adapter_hw_chg.stop_charging = smb137b_stop_charging;
  574. smb137b_chg->adapter_hw_chg.charging_switched = smb137b_charger_switch;
  575. if (pdata->chg_detection_config)
  576. ret = pdata->chg_detection_config();
  577. if (ret) {
  578. dev_err(&client->dev, "%s valid config failed ret=%d\n",
  579. __func__, ret);
  580. goto free_smb137b_chg;
  581. }
  582. ret = gpio_request(pdata->valid_n_gpio, "smb137b_charger_valid");
  583. if (ret) {
  584. dev_err(&client->dev, "%s gpio_request failed for %d ret=%d\n",
  585. __func__, pdata->valid_n_gpio, ret);
  586. goto free_smb137b_chg;
  587. }
  588. i2c_set_clientdata(client, smb137b_chg);
  589. ret = msm_charger_register(&smb137b_chg->adapter_hw_chg);
  590. if (ret) {
  591. dev_err(&client->dev, "%s msm_charger_register\
  592. failed for ret=%d\n", __func__, ret);
  593. goto free_valid_gpio;
  594. }
  595. ret = irq_set_irq_wake(client->irq, 1);
  596. if (ret) {
  597. dev_err(&client->dev, "%s failed for irq_set_irq_wake %d ret =%d\n",
  598. __func__, client->irq, ret);
  599. goto unregister_charger;
  600. }
  601. ret = request_threaded_irq(client->irq, NULL,
  602. smb137b_valid_handler,
  603. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  604. "smb137b_charger_valid", client);
  605. if (ret) {
  606. dev_err(&client->dev,
  607. "%s request_threaded_irq failed for %d ret =%d\n",
  608. __func__, client->irq, ret);
  609. goto disable_irq_wake;
  610. }
  611. ret = gpio_get_value_cansleep(smb137b_chg->valid_n_gpio);
  612. if (ret < 0) {
  613. dev_err(&client->dev,
  614. "%s gpio_get_value failed for %d ret=%d\n", __func__,
  615. pdata->valid_n_gpio, ret);
  616. /* assume absent */
  617. ret = 1;
  618. }
  619. if (!ret) {
  620. msm_charger_notify_event(&smb137b_chg->adapter_hw_chg,
  621. CHG_INSERTED_EVENT);
  622. smb137b_chg->usb_status = SMB137B_PRESENT;
  623. }
  624. ret = smb137b_read_reg(smb137b_chg->client, DEV_ID_REG,
  625. &smb137b_chg->dev_id_reg);
  626. ret = device_create_file(&smb137b_chg->client->dev, &dev_attr_id_reg);
  627. /* TODO read min_design and max_design from chip registers */
  628. smb137b_chg->min_design = 3200;
  629. smb137b_chg->max_design = 4200;
  630. smb137b_chg->batt_status = POWER_SUPPLY_STATUS_DISCHARGING;
  631. smb137b_chg->batt_chg_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
  632. device_init_wakeup(&client->dev, 1);
  633. mutex_lock(&init_lock);
  634. usb_smb137b_chg = smb137b_chg;
  635. if (init_otg_power)
  636. __smb137b_otg_power(init_otg_power);
  637. mutex_unlock(&init_lock);
  638. smb137b_create_debugfs_entries(smb137b_chg);
  639. dev_dbg(&client->dev,
  640. "%s OK device_id = %x chg_state=%d\n", __func__,
  641. smb137b_chg->dev_id_reg, smb137b_chg->usb_status);
  642. return 0;
  643. disable_irq_wake:
  644. irq_set_irq_wake(client->irq, 0);
  645. unregister_charger:
  646. msm_charger_unregister(&smb137b_chg->adapter_hw_chg);
  647. free_valid_gpio:
  648. gpio_free(pdata->valid_n_gpio);
  649. free_smb137b_chg:
  650. kfree(smb137b_chg);
  651. out:
  652. return ret;
  653. }
  654. void smb137b_otg_power(int on)
  655. {
  656. pr_debug("%s Enter on=%d\n", __func__, on);
  657. mutex_lock(&init_lock);
  658. if (!usb_smb137b_chg) {
  659. init_otg_power = !!on;
  660. pr_warning("%s called when not initialized\n", __func__);
  661. mutex_unlock(&init_lock);
  662. return;
  663. }
  664. __smb137b_otg_power(on);
  665. mutex_unlock(&init_lock);
  666. }
  667. static int __devexit smb137b_remove(struct i2c_client *client)
  668. {
  669. const struct smb137b_platform_data *pdata;
  670. struct smb137b_data *smb137b_chg = i2c_get_clientdata(client);
  671. pdata = client->dev.platform_data;
  672. device_init_wakeup(&client->dev, 0);
  673. irq_set_irq_wake(client->irq, 0);
  674. free_irq(client->irq, client);
  675. gpio_free(pdata->valid_n_gpio);
  676. cancel_delayed_work_sync(&smb137b_chg->charge_work);
  677. msm_charger_notify_event(&smb137b_chg->adapter_hw_chg,
  678. CHG_REMOVED_EVENT);
  679. msm_charger_unregister(&smb137b_chg->adapter_hw_chg);
  680. smb137b_destroy_debugfs_entries();
  681. kfree(smb137b_chg);
  682. return 0;
  683. }
  684. #ifdef CONFIG_PM
  685. static int smb137b_suspend(struct device *dev)
  686. {
  687. struct smb137b_data *smb137b_chg = dev_get_drvdata(dev);
  688. dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__);
  689. if (smb137b_chg->charging)
  690. return -EBUSY;
  691. return 0;
  692. }
  693. static int smb137b_resume(struct device *dev)
  694. {
  695. struct smb137b_data *smb137b_chg = dev_get_drvdata(dev);
  696. dev_dbg(&smb137b_chg->client->dev, "%s\n", __func__);
  697. return 0;
  698. }
  699. static const struct dev_pm_ops smb137b_pm_ops = {
  700. .suspend = smb137b_suspend,
  701. .resume = smb137b_resume,
  702. };
  703. #endif
  704. static const struct i2c_device_id smb137b_id[] = {
  705. {"smb137b", 0},
  706. {},
  707. };
  708. MODULE_DEVICE_TABLE(i2c, smb137b_id);
  709. static struct i2c_driver smb137b_driver = {
  710. .driver = {
  711. .name = "smb137b",
  712. .owner = THIS_MODULE,
  713. #ifdef CONFIG_PM
  714. .pm = &smb137b_pm_ops,
  715. #endif
  716. },
  717. .probe = smb137b_probe,
  718. .remove = __devexit_p(smb137b_remove),
  719. .id_table = smb137b_id,
  720. };
  721. static int __init smb137b_init(void)
  722. {
  723. return i2c_add_driver(&smb137b_driver);
  724. }
  725. module_init(smb137b_init);
  726. static void __exit smb137b_exit(void)
  727. {
  728. return i2c_del_driver(&smb137b_driver);
  729. }
  730. module_exit(smb137b_exit);
  731. MODULE_AUTHOR("Abhijeet Dharmapurikar <adharmap@codeaurora.org>");
  732. MODULE_DESCRIPTION("Driver for SMB137B Charger chip");
  733. MODULE_LICENSE("GPL v2");
  734. MODULE_ALIAS("i2c:smb137b");