smb137c-charger.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/slab.h>
  20. struct smb137c_chip {
  21. struct i2c_client *client;
  22. struct power_supply psy;
  23. struct power_supply *usb_psy;
  24. struct mutex lock;
  25. int charge_current_limit_ua;
  26. int input_current_limit_ua;
  27. int term_current_ua;
  28. bool charging_enabled;
  29. bool otg_mode_enabled;
  30. bool charging_allowed;
  31. bool usb_suspend_enabled;
  32. };
  33. struct input_current_config {
  34. int current_limit_ua;
  35. u8 cmd_b_reg;
  36. u8 var_func_reg;
  37. u8 input_cur_reg;
  38. };
  39. struct term_current_config {
  40. int term_current_ua;
  41. u8 charge_cur_reg;
  42. };
  43. #define INPUT_CURRENT(_current_limit_ua, _cmd_b_reg, _var_func_reg, \
  44. _input_cur_reg) \
  45. { \
  46. .current_limit_ua = _current_limit_ua, \
  47. .cmd_b_reg = _cmd_b_reg, \
  48. .var_func_reg = _var_func_reg, \
  49. .input_cur_reg = _input_cur_reg, \
  50. }
  51. #define CHARGE_CURRENT_REG 0x00
  52. #define CHARGE_CURRENT_FAST_CHG_MASK 0xE0
  53. #define CHARGE_CURRENT_FAST_CHG_SHIFT 5
  54. #define CHARGE_CURRENT_PRE_CHG_MASK 0x18
  55. #define CHARGE_CURRENT_PRE_CHG_SHIFT 3
  56. #define CHARGE_CURRENT_TERM_CUR_MASK 0x06
  57. #define INPUT_CURRENT_REG 0x01
  58. #define INPUT_CURRENT_LIMIT_MASK 0xE0
  59. #define FLOAT_VOLTAGE_REG 0x02
  60. #define FLOAT_VOLTAGE_MASK 0x7F
  61. #define FLOAT_VOLTAGE_SHIFT 0
  62. #define CTRL_A_REG 0x03
  63. #define CTRL_A_AUTO_RECHARGE_MASK 0x80
  64. #define CTRL_A_AUTO_RECHARGE_ENABLED 0x00
  65. #define CTRL_A_AUTO_RECHARGE_DISABLED 0x80
  66. #define CTRL_A_TERM_CUR_MASK 0x40
  67. #define CTRL_A_TERM_CUR_ENABLED 0x00
  68. #define CTRL_A_TERM_CUR_DISABLED 0x40
  69. #define CTRL_A_THRESH_VOLTAGE_MASK 0x38
  70. #define CTRL_A_THRESH_VOLTAGE_SHIFT 3
  71. #define CTRL_A_VOUTL_MASK 0x02
  72. #define CTRL_A_VOUTL_4250MV 0x00
  73. #define CTRL_A_VOUTL_4460MV 0x02
  74. #define CTRL_A_THERM_MONITOR_MASK 0x01
  75. #define CTRL_A_THERM_MONITOR_ENABLED 0x01
  76. #define CTRL_A_THERM_MONITOR_DISABLED 0x00
  77. #define PIN_CTRL_REG 0x05
  78. #define PIN_CTRL_DEAD_BATT_CHG_MASK 0x80
  79. #define PIN_CTRL_DEAD_BATT_CHG_ENABLED 0x80
  80. #define PIN_CTRL_DEAD_BATT_CHG_DISABLED 0x00
  81. #define PIN_CTRL_OTG_LBR_MASK 0x20
  82. #define PIN_CTRL_OTG 0x00
  83. #define PIN_CTRL_LBR 0x20
  84. #define PIN_CTRL_USB_CUR_LIMIT_MASK 0x10
  85. #define PIN_CTRL_USB_CUR_LIMIT_REG 0x00
  86. #define PIN_CTRL_USB_CUR_LIMIT_PIN 0x10
  87. #define PIN_CTRL_CHG_EN_MASK 0x0C
  88. #define PIN_CTRL_CHG_EN_REG_LOW 0x00
  89. #define PIN_CTRL_CHG_EN_REG_HIGH 0x04
  90. #define PIN_CTRL_CHG_EN_PIN_LOW 0x08
  91. #define PIN_CTRL_CHG_EN_PIN_HIGH 0x0C
  92. #define PIN_CTRL_OTG_CTRL_MASK 0x02
  93. #define PIN_CTRL_OTG_CTRL_REG 0x00
  94. #define PIN_CTRL_OTG_CTRL_PIN 0x02
  95. #define OTG_CTRL_REG 0x06
  96. #define OTG_CTRL_BMD_MASK 0x80
  97. #define OTG_CTRL_BMD_ENABLED 0x80
  98. #define OTG_CTRL_BMD_DISABLED 0x00
  99. #define OTG_CTRL_AUTO_RECHARGE_MASK 0x40
  100. #define OTG_CTRL_AUTO_RECHARGE_75MV 0x00
  101. #define OTG_CTRL_AUTO_RECHARGE_120MV 0x40
  102. #define TEMP_MON_REG 0x08
  103. #define TEMP_MON_THERM_CURRENT_MASK 0xC0
  104. #define TEMP_MON_THERM_CURRENT_SHIFT 6
  105. #define TEMP_MON_TEMP_LOW_MASK 0x38
  106. #define TEMP_MON_TEMP_LOW_SHIFT 3
  107. #define TEMP_MON_TEMP_HIGH_MASK 0x07
  108. #define TEMP_MON_TEMP_HIGH_SHIFT 0
  109. #define SAFETY_TIMER_REG 0x09
  110. #define SAFETY_TIMER_RELOAD_MASK 0x40
  111. #define SAFETY_TIMER_RELOAD_ENABLED 0x40
  112. #define SAFETY_TIMER_RELOAD_DISABLED 0x00
  113. #define SAFETY_TIMER_CHG_TIMEOUT_MASK 0x0C
  114. #define SAFETY_TIMER_CHG_TIMEOUT_SHIFT 2
  115. #define SAFETY_TIMER_PRE_CHG_TIME_MASK 0x03
  116. #define SAFETY_TIMER_PRE_CHG_TIME_SHIFT 0
  117. #define VAR_FUNC_REG 0x0C
  118. #define VAR_FUNC_USB_MODE_MASK 0x80
  119. #define VAR_FUNC_USB_SUSPEND_CTRL_MASK 0x20
  120. #define VAR_FUNC_USB_SUSPEND_CTRL_REG 0x00
  121. #define VAR_FUNC_USB_SUSPEND_CTRL_PIN 0x20
  122. #define VAR_FUNC_BMD_MASK 0x0C
  123. #define VAR_FUNC_BMD_DISABLED 0x00
  124. #define VAR_FUNC_BMD_ALGO_PERIODIC 0x04
  125. #define VAR_FUNC_BMD_ALGO 0x08
  126. #define VAR_FUNC_BMD_THERM 0x0C
  127. #define CMD_A_REG 0x30
  128. #define CMD_A_VOLATILE_WRITE_MASK 0x80
  129. #define CMD_A_VOLATILE_WRITE_ALLOW 0x80
  130. #define CMD_A_VOLATILE_WRITE_DISALLOW 0x00
  131. #define CMD_A_FAST_CHG_MASK 0x40
  132. #define CMD_A_FAST_CHG_ALLOW 0x40
  133. #define CMD_A_FAST_CHG_DISALLOW 0x00
  134. #define CMD_A_OTG_MASK 0x10
  135. #define CMD_A_OTG_ENABLED 0x10
  136. #define CMD_A_OTG_DISABLED 0x00
  137. #define CMD_A_USB_SUSPEND_MASK 0x04
  138. #define CMD_A_USB_SUSPEND_DISABLED 0x00
  139. #define CMD_A_USB_SUSPEND_ENABLED 0x04
  140. #define CMD_A_CHARGING_MASK 0x02
  141. #define CMD_A_CHARGING_ENABLED 0x00
  142. #define CMD_A_CHARGING_DISABLED 0x02
  143. #define CMD_B_REG 0x31
  144. #define CMD_B_USB_MODE_MASK 0x03
  145. #define DEV_ID_REG 0x33
  146. #define DEV_ID_PART_MASK 0x80
  147. #define DEV_ID_PART_SMB137C 0x00
  148. #define DEV_ID_GUI_REV_MASK 0x70
  149. #define DEV_ID_GUI_REV_SHIFT 4
  150. #define DEV_ID_SILICON_REV_MASK 0x0F
  151. #define DEV_ID_SILICON_REV_SHIFT 0
  152. #define IRQ_STAT_A_REG 0x35
  153. #define IRQ_STAT_A_BATT_HOT 0x40
  154. #define IRQ_STAT_A_BATT_COLD 0x10
  155. #define IRQ_STAT_B_REG 0x36
  156. #define IRQ_STAT_B_BATT_OVERVOLT 0x40
  157. #define IRQ_STAT_B_BATT_MISSING 0x10
  158. #define IRQ_STAT_B_BATT_UNDERVOLT 0x04
  159. #define STAT_C_REG 0x3D
  160. #define STAT_C_CHG_ERROR 0x40
  161. #define STAT_C_VBATT_LEVEL_BELOW_2P1V 0x10
  162. #define STAT_C_CHG_STAT_MASK 0x06
  163. #define STAT_C_CHG_STAT_SHIFT 1
  164. #define STAT_C_CHG_ENABLED 0x01
  165. /* Charge status register values */
  166. enum smb137c_charge_status {
  167. CHARGE_STAT_NO_CHG = 0,
  168. CHARGE_STAT_PRE_CHG = 1,
  169. CHARGE_STAT_FAST_CHG = 2,
  170. CHARGE_STAT_TAPER_CHG = 3,
  171. };
  172. #define PRE_CHARGE_CURRENT_MIN_UA 50000
  173. #define PRE_CHARGE_CURRENT_MAX_UA 200000
  174. #define PRE_CHARGE_CURRENT_STEP_UA 50000
  175. #define FLOAT_VOLTAGE_MIN_UV 3460000
  176. #define FLOAT_VOLTAGE_MAX_UV 4730000
  177. #define FLOAT_VOLTAGE_STEP_UV 10000
  178. #define PRE_CHG_THRESH_VOLTAGE_MIN_UV 2400000
  179. #define PRE_CHG_THRESH_VOLTAGE_MAX_UV 3100000
  180. #define PRE_CHG_THRESH_VOLTAGE_STEP_UV 100000
  181. #define USB_MIN_CURRENT_UA 100000
  182. static int smb137c_read_reg(struct smb137c_chip *chip, u8 reg, u8 *val)
  183. {
  184. int rc;
  185. rc = i2c_smbus_read_byte_data(chip->client, reg);
  186. if (rc < 0) {
  187. pr_err("i2c_smbus_read_byte_data failed. reg=0x%02X, rc=%d\n",
  188. reg, rc);
  189. } else {
  190. *val = rc;
  191. rc = 0;
  192. pr_debug("read(0x%02X)=0x%02X\n", reg, *val);
  193. }
  194. return rc;
  195. }
  196. static int smb137c_write_reg(struct smb137c_chip *chip, u8 reg, u8 val)
  197. {
  198. int rc;
  199. rc = i2c_smbus_write_byte_data(chip->client, reg, val);
  200. if (rc < 0)
  201. pr_err("i2c_smbus_write_byte_data failed. reg=0x%02X, rc=%d\n",
  202. reg, rc);
  203. else
  204. pr_debug("write(0x%02X)=0x%02X\n", reg, val);
  205. return rc;
  206. }
  207. static int smb137c_masked_write_reg(struct smb137c_chip *chip, u8 reg, u8 mask,
  208. u8 val)
  209. {
  210. u8 reg_val;
  211. int rc;
  212. pr_debug("masked write(0x%02X), mask=0x%02X, value=0x%02X\n", reg, mask,
  213. val);
  214. rc = smb137c_read_reg(chip, reg, &reg_val);
  215. if (rc < 0)
  216. return rc;
  217. val = (reg_val & ~mask) | (val & mask);
  218. if (val != reg_val)
  219. rc = smb137c_write_reg(chip, reg, val);
  220. return rc;
  221. }
  222. static int smb137c_enable_charging(struct smb137c_chip *chip)
  223. {
  224. int rc = 0;
  225. chip->charging_allowed = true;
  226. if (!chip->charging_enabled && chip->charge_current_limit_ua > 0) {
  227. rc = smb137c_masked_write_reg(chip, CMD_A_REG,
  228. CMD_A_CHARGING_MASK, CMD_A_CHARGING_ENABLED);
  229. if (!rc)
  230. chip->charging_enabled = true;
  231. }
  232. if (!rc)
  233. dev_dbg(&chip->client->dev, "%s\n", __func__);
  234. return rc;
  235. }
  236. static int smb137c_disable_charging(struct smb137c_chip *chip)
  237. {
  238. int rc = 0;
  239. chip->charging_allowed = false;
  240. if (chip->charging_enabled) {
  241. rc = smb137c_masked_write_reg(chip, CMD_A_REG,
  242. CMD_A_CHARGING_MASK, CMD_A_CHARGING_DISABLED);
  243. if (!rc)
  244. chip->charging_enabled = false;
  245. }
  246. if (!rc)
  247. dev_dbg(&chip->client->dev, "%s\n", __func__);
  248. return rc;
  249. }
  250. static int smb137c_enable_otg_mode(struct smb137c_chip *chip)
  251. {
  252. int rc = 0;
  253. if (!chip->otg_mode_enabled) {
  254. rc = smb137c_masked_write_reg(chip, CMD_A_REG, CMD_A_OTG_MASK,
  255. CMD_A_OTG_ENABLED);
  256. if (!rc)
  257. chip->otg_mode_enabled = true;
  258. }
  259. if (!rc)
  260. dev_dbg(&chip->client->dev, "%s\n", __func__);
  261. return rc;
  262. }
  263. static int smb137c_disable_otg_mode(struct smb137c_chip *chip)
  264. {
  265. int rc = 0;
  266. if (chip->otg_mode_enabled) {
  267. rc = smb137c_masked_write_reg(chip, CMD_A_REG, CMD_A_OTG_MASK,
  268. CMD_A_OTG_DISABLED);
  269. if (!rc)
  270. chip->otg_mode_enabled = false;
  271. }
  272. if (!rc)
  273. dev_dbg(&chip->client->dev, "%s\n", __func__);
  274. return rc;
  275. }
  276. static int smb137c_enable_usb_suspend(struct smb137c_chip *chip)
  277. {
  278. int rc = 0;
  279. if (!chip->usb_suspend_enabled) {
  280. rc = smb137c_masked_write_reg(chip, CMD_A_REG,
  281. CMD_A_USB_SUSPEND_MASK, CMD_A_USB_SUSPEND_ENABLED);
  282. if (!rc)
  283. chip->usb_suspend_enabled = true;
  284. }
  285. if (!rc)
  286. dev_dbg(&chip->client->dev, "%s\n", __func__);
  287. return rc;
  288. }
  289. static int smb137c_disable_usb_suspend(struct smb137c_chip *chip)
  290. {
  291. int rc = 0;
  292. if (chip->input_current_limit_ua > 0 && chip->usb_suspend_enabled) {
  293. rc = smb137c_masked_write_reg(chip, CMD_A_REG,
  294. CMD_A_USB_SUSPEND_MASK, CMD_A_USB_SUSPEND_DISABLED);
  295. if (!rc)
  296. chip->usb_suspend_enabled = false;
  297. }
  298. if (!rc)
  299. dev_dbg(&chip->client->dev, "%s\n", __func__);
  300. return rc;
  301. }
  302. static struct input_current_config supported_input_current[] = {
  303. INPUT_CURRENT(100000, 0x00, 0x00, 0x00),
  304. INPUT_CURRENT(150000, 0x00, 0x80, 0x00),
  305. INPUT_CURRENT(500000, 0x02, 0x00, 0x00),
  306. INPUT_CURRENT(700000, 0x01, 0x00, 0x00),
  307. INPUT_CURRENT(800000, 0x01, 0x00, 0x20),
  308. INPUT_CURRENT(900000, 0x01, 0x00, 0x40),
  309. INPUT_CURRENT(1000000, 0x01, 0x00, 0x60),
  310. INPUT_CURRENT(1100000, 0x01, 0x00, 0x80),
  311. INPUT_CURRENT(1200000, 0x01, 0x00, 0xA0),
  312. INPUT_CURRENT(1300000, 0x01, 0x00, 0xC0),
  313. INPUT_CURRENT(1500000, 0x01, 0x00, 0xE0),
  314. };
  315. static int smb137c_set_usb_input_current_limit(struct smb137c_chip *chip,
  316. int current_limit_ua)
  317. {
  318. struct input_current_config *config = NULL;
  319. int rc = 0;
  320. int i;
  321. for (i = ARRAY_SIZE(supported_input_current) - 1; i >= 0; i--) {
  322. if (current_limit_ua
  323. >= supported_input_current[i].current_limit_ua) {
  324. config = &supported_input_current[i];
  325. break;
  326. }
  327. }
  328. if (config) {
  329. if (chip->input_current_limit_ua != config->current_limit_ua) {
  330. rc = smb137c_masked_write_reg(chip, INPUT_CURRENT_REG,
  331. INPUT_CURRENT_LIMIT_MASK, config->input_cur_reg);
  332. if (rc)
  333. return rc;
  334. rc = smb137c_masked_write_reg(chip, VAR_FUNC_REG,
  335. VAR_FUNC_USB_MODE_MASK, config->var_func_reg);
  336. if (rc)
  337. return rc;
  338. rc = smb137c_masked_write_reg(chip, CMD_B_REG,
  339. CMD_B_USB_MODE_MASK, config->cmd_b_reg);
  340. if (rc)
  341. return rc;
  342. chip->input_current_limit_ua = config->current_limit_ua;
  343. }
  344. rc = smb137c_disable_usb_suspend(chip);
  345. } else {
  346. chip->input_current_limit_ua = 0;
  347. rc = smb137c_enable_usb_suspend(chip);
  348. }
  349. if (!rc)
  350. dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
  351. chip->input_current_limit_ua);
  352. return rc;
  353. }
  354. static int fast_charge_current_ua[] = {
  355. 500000,
  356. 650000,
  357. 750000,
  358. 850000,
  359. 950000,
  360. 1100000,
  361. 1300000,
  362. 1500000,
  363. };
  364. static int smb137c_set_charge_current_limit(struct smb137c_chip *chip,
  365. int current_limit_ua)
  366. {
  367. int fast_charge_limit_ua = 0;
  368. int rc = 0;
  369. u8 val = 0;
  370. int i;
  371. for (i = ARRAY_SIZE(fast_charge_current_ua) - 1; i >= 0; i--) {
  372. if (current_limit_ua >= fast_charge_current_ua[i]) {
  373. val = i << CHARGE_CURRENT_FAST_CHG_SHIFT;
  374. fast_charge_limit_ua = fast_charge_current_ua[i];
  375. break;
  376. }
  377. }
  378. if (fast_charge_limit_ua
  379. && chip->charge_current_limit_ua != fast_charge_limit_ua)
  380. rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
  381. CHARGE_CURRENT_FAST_CHG_MASK, val);
  382. else if (fast_charge_limit_ua == 0)
  383. rc = smb137c_disable_charging(chip);
  384. chip->charge_current_limit_ua = fast_charge_limit_ua;
  385. if (!rc)
  386. dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
  387. fast_charge_limit_ua);
  388. return rc;
  389. }
  390. static int smb137c_get_charge_current_limit(struct smb137c_chip *chip)
  391. {
  392. int fast_charge_limit_ua = 0;
  393. u8 val = 0;
  394. int rc, i;
  395. rc = smb137c_read_reg(chip, CHARGE_CURRENT_REG, &val);
  396. if (rc)
  397. return rc;
  398. i = (val & CHARGE_CURRENT_FAST_CHG_MASK)
  399. >> CHARGE_CURRENT_FAST_CHG_SHIFT;
  400. if (i >= 0 && i < ARRAY_SIZE(fast_charge_current_ua))
  401. fast_charge_limit_ua = fast_charge_current_ua[i];
  402. dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
  403. fast_charge_limit_ua);
  404. return fast_charge_limit_ua;
  405. }
  406. static struct term_current_config term_current_ua[] = {
  407. { 35000, 0x06},
  408. { 50000, 0x00},
  409. {100000, 0x02},
  410. {150000, 0x04},
  411. };
  412. static int smb137c_set_term_current(struct smb137c_chip *chip,
  413. int current_limit_ua)
  414. {
  415. int term_current_limit_ua = 0;
  416. int rc = 0;
  417. u8 val = 0;
  418. int i;
  419. for (i = ARRAY_SIZE(term_current_ua) - 1; i >= 0; i--) {
  420. if (current_limit_ua >= term_current_ua[i].term_current_ua) {
  421. val = term_current_ua[i].charge_cur_reg;
  422. term_current_limit_ua
  423. = term_current_ua[i].term_current_ua;
  424. break;
  425. }
  426. }
  427. if (term_current_limit_ua) {
  428. rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
  429. CHARGE_CURRENT_TERM_CUR_MASK, val);
  430. if (rc)
  431. return rc;
  432. rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
  433. CTRL_A_TERM_CUR_MASK, CTRL_A_TERM_CUR_ENABLED);
  434. } else {
  435. rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
  436. CTRL_A_TERM_CUR_MASK, CTRL_A_TERM_CUR_DISABLED);
  437. }
  438. if (!rc)
  439. dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
  440. term_current_limit_ua);
  441. return rc;
  442. }
  443. static int smb137c_set_pre_charge_current_limit(struct smb137c_chip *chip,
  444. int current_limit_ua)
  445. {
  446. int setpoint, rc;
  447. u8 val;
  448. if (current_limit_ua < PRE_CHARGE_CURRENT_MIN_UA ||
  449. current_limit_ua > PRE_CHARGE_CURRENT_MAX_UA) {
  450. dev_err(&chip->client->dev, "%s: current limit out of bounds: %d\n",
  451. __func__, current_limit_ua);
  452. return -EINVAL;
  453. }
  454. setpoint = (current_limit_ua - PRE_CHARGE_CURRENT_MIN_UA)
  455. / PRE_CHARGE_CURRENT_STEP_UA;
  456. val = setpoint << CHARGE_CURRENT_PRE_CHG_SHIFT;
  457. rc = smb137c_masked_write_reg(chip, CHARGE_CURRENT_REG,
  458. CHARGE_CURRENT_PRE_CHG_MASK, val);
  459. if (!rc)
  460. dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
  461. setpoint * PRE_CHARGE_CURRENT_STEP_UA
  462. + PRE_CHARGE_CURRENT_MIN_UA);
  463. return rc;
  464. }
  465. static int smb137c_set_float_voltage(struct smb137c_chip *chip, int voltage_uv)
  466. {
  467. int setpoint, rc;
  468. u8 val;
  469. if (voltage_uv < FLOAT_VOLTAGE_MIN_UV ||
  470. voltage_uv > FLOAT_VOLTAGE_MAX_UV) {
  471. dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
  472. __func__, voltage_uv);
  473. return -EINVAL;
  474. }
  475. setpoint = (voltage_uv - FLOAT_VOLTAGE_MIN_UV) / FLOAT_VOLTAGE_STEP_UV;
  476. val = setpoint << FLOAT_VOLTAGE_SHIFT;
  477. rc = smb137c_masked_write_reg(chip, FLOAT_VOLTAGE_REG,
  478. FLOAT_VOLTAGE_MASK, val);
  479. if (!rc)
  480. dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
  481. setpoint * FLOAT_VOLTAGE_STEP_UV + FLOAT_VOLTAGE_MIN_UV);
  482. return rc;
  483. }
  484. static int smb137c_set_pre_charge_threshold_voltage(struct smb137c_chip *chip,
  485. int voltage_uv)
  486. {
  487. int setpoint, rc;
  488. u8 val;
  489. if (voltage_uv < PRE_CHG_THRESH_VOLTAGE_MIN_UV ||
  490. voltage_uv > PRE_CHG_THRESH_VOLTAGE_MAX_UV) {
  491. dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
  492. __func__, voltage_uv);
  493. return -EINVAL;
  494. }
  495. setpoint = (voltage_uv - PRE_CHG_THRESH_VOLTAGE_MIN_UV)
  496. / PRE_CHG_THRESH_VOLTAGE_STEP_UV;
  497. val = setpoint << CTRL_A_THRESH_VOLTAGE_SHIFT;
  498. rc = smb137c_masked_write_reg(chip, CTRL_A_REG,
  499. CTRL_A_THRESH_VOLTAGE_MASK, val);
  500. if (!rc)
  501. dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
  502. setpoint * PRE_CHG_THRESH_VOLTAGE_STEP_UV
  503. + PRE_CHG_THRESH_VOLTAGE_MIN_UV);
  504. return rc;
  505. }
  506. static int smb137c_set_recharge_threshold_voltage(struct smb137c_chip *chip,
  507. int voltage_uv)
  508. {
  509. int rc;
  510. u8 val;
  511. if (voltage_uv == 75000) {
  512. val = OTG_CTRL_AUTO_RECHARGE_75MV;
  513. } else if (voltage_uv == 120000) {
  514. val = OTG_CTRL_AUTO_RECHARGE_120MV;
  515. } else {
  516. dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
  517. __func__, voltage_uv);
  518. return -EINVAL;
  519. }
  520. rc = smb137c_masked_write_reg(chip, OTG_CTRL_REG,
  521. OTG_CTRL_AUTO_RECHARGE_MASK, val);
  522. if (!rc)
  523. dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
  524. voltage_uv);
  525. return rc;
  526. }
  527. static int smb137c_set_system_voltage(struct smb137c_chip *chip, int voltage_uv)
  528. {
  529. int rc;
  530. u8 val;
  531. if (voltage_uv == 4250000) {
  532. val = CTRL_A_VOUTL_4250MV;
  533. } else if (voltage_uv == 4460000) {
  534. val = CTRL_A_VOUTL_4460MV;
  535. } else {
  536. dev_err(&chip->client->dev, "%s: voltage out of bounds: %d\n",
  537. __func__, voltage_uv);
  538. return -EINVAL;
  539. }
  540. rc = smb137c_masked_write_reg(chip, CTRL_A_REG, CTRL_A_VOUTL_MASK, val);
  541. if (!rc)
  542. dev_dbg(&chip->client->dev, "%s: voltage=%d uV\n", __func__,
  543. voltage_uv);
  544. return rc;
  545. }
  546. static int charging_timeout[] = {
  547. 382,
  548. 764,
  549. 1527,
  550. };
  551. static int smb137c_set_charging_timeout(struct smb137c_chip *chip, int timeout)
  552. {
  553. int timeout_chosen = 0;
  554. u8 val = 3 << SAFETY_TIMER_CHG_TIMEOUT_SHIFT;
  555. int rc, i;
  556. for (i = ARRAY_SIZE(charging_timeout) - 1; i >= 0; i--) {
  557. if (timeout >= charging_timeout[i]) {
  558. val = i << SAFETY_TIMER_CHG_TIMEOUT_SHIFT;
  559. timeout_chosen = charging_timeout[i];
  560. break;
  561. }
  562. }
  563. rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
  564. SAFETY_TIMER_CHG_TIMEOUT_MASK, val);
  565. if (!rc)
  566. dev_dbg(&chip->client->dev, "%s: timeout=%d min\n", __func__,
  567. timeout_chosen);
  568. return rc;
  569. }
  570. static int pre_charge_timeout[] = {
  571. 48,
  572. 95,
  573. 191,
  574. };
  575. static int smb137c_set_pre_charge_timeout(struct smb137c_chip *chip,
  576. int timeout)
  577. {
  578. int timeout_chosen = 0;
  579. u8 val = 3 << SAFETY_TIMER_PRE_CHG_TIME_SHIFT;
  580. int rc, i;
  581. for (i = ARRAY_SIZE(pre_charge_timeout) - 1; i >= 0; i--) {
  582. if (timeout >= pre_charge_timeout[i]) {
  583. val = i << SAFETY_TIMER_PRE_CHG_TIME_SHIFT;
  584. timeout_chosen = pre_charge_timeout[i];
  585. break;
  586. }
  587. }
  588. rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
  589. SAFETY_TIMER_PRE_CHG_TIME_MASK, val);
  590. if (!rc)
  591. dev_dbg(&chip->client->dev, "%s: timeout=%d min\n", __func__,
  592. timeout_chosen);
  593. return rc;
  594. }
  595. static int thermistor_current[] = {
  596. 100,
  597. 40,
  598. 20,
  599. 10,
  600. };
  601. static int smb137c_set_thermistor_current(struct smb137c_chip *chip,
  602. int current_ua)
  603. {
  604. bool found = false;
  605. u8 val = 0;
  606. int rc, i;
  607. for (i = 0; i < ARRAY_SIZE(thermistor_current); i++) {
  608. if (current_ua == thermistor_current[i]) {
  609. found = true;
  610. val = i << TEMP_MON_THERM_CURRENT_SHIFT;
  611. }
  612. }
  613. if (!found) {
  614. dev_err(&chip->client->dev, "%s: current out of bounds: %d\n",
  615. __func__, current_ua);
  616. return -EINVAL;
  617. }
  618. rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
  619. TEMP_MON_THERM_CURRENT_MASK, val);
  620. if (!rc)
  621. dev_dbg(&chip->client->dev, "%s: current=%d uA\n", __func__,
  622. current_ua);
  623. return 0;
  624. }
  625. static int smb137c_set_temperature_low_limit(struct smb137c_chip *chip,
  626. int value)
  627. {
  628. int rc;
  629. if (value < 0 || value > 7) {
  630. dev_err(&chip->client->dev, "%s: temperature value out of bounds: %d\n",
  631. __func__, value);
  632. return -EINVAL;
  633. }
  634. rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
  635. TEMP_MON_TEMP_LOW_MASK, value << TEMP_MON_TEMP_LOW_SHIFT);
  636. if (!rc)
  637. dev_dbg(&chip->client->dev, "%s: temperature value=%d\n",
  638. __func__, value);
  639. return rc;
  640. }
  641. static int smb137c_set_temperature_high_limit(struct smb137c_chip *chip,
  642. int value)
  643. {
  644. int rc;
  645. if (value < 0 || value > 7) {
  646. dev_err(&chip->client->dev, "%s: temperature value out of bounds: %d\n",
  647. __func__, value);
  648. return -EINVAL;
  649. }
  650. rc = smb137c_masked_write_reg(chip, TEMP_MON_REG,
  651. TEMP_MON_TEMP_HIGH_MASK, value << TEMP_MON_TEMP_HIGH_SHIFT);
  652. if (!rc)
  653. dev_dbg(&chip->client->dev, "%s: temperature value=%d\n",
  654. __func__, value);
  655. return rc;
  656. }
  657. static int charge_status_type_map[] = {
  658. [CHARGE_STAT_NO_CHG] = POWER_SUPPLY_CHARGE_TYPE_NONE,
  659. [CHARGE_STAT_PRE_CHG] = POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
  660. [CHARGE_STAT_FAST_CHG] = POWER_SUPPLY_CHARGE_TYPE_FAST,
  661. [CHARGE_STAT_TAPER_CHG] = POWER_SUPPLY_CHARGE_TYPE_FAST,
  662. };
  663. static const char * const charge_status_name[] = {
  664. [CHARGE_STAT_NO_CHG] = "none",
  665. [CHARGE_STAT_PRE_CHG] = "pre-charge",
  666. [CHARGE_STAT_FAST_CHG] = "fast-charge",
  667. [CHARGE_STAT_TAPER_CHG] = "taper-charge",
  668. };
  669. static int smb137c_get_property_status(struct smb137c_chip *chip)
  670. {
  671. int status = POWER_SUPPLY_STATUS_DISCHARGING;
  672. enum smb137c_charge_status charging_status;
  673. bool charging_enabled;
  674. bool charging_error;
  675. int rc;
  676. u8 val;
  677. rc = smb137c_read_reg(chip, STAT_C_REG, &val);
  678. if (rc)
  679. return POWER_SUPPLY_STATUS_UNKNOWN;
  680. charging_enabled = val & STAT_C_CHG_ENABLED;
  681. charging_error = val & STAT_C_CHG_ERROR;
  682. charging_status = (val & STAT_C_CHG_STAT_MASK) >> STAT_C_CHG_STAT_SHIFT;
  683. if (charging_enabled && !charging_error
  684. && charging_status != CHARGE_STAT_NO_CHG)
  685. status = POWER_SUPPLY_STATUS_CHARGING;
  686. dev_dbg(&chip->client->dev, "%s: status=%s\n", __func__,
  687. (status == POWER_SUPPLY_STATUS_CHARGING ? "charging"
  688. : "discharging"));
  689. return status;
  690. }
  691. static int smb137c_get_property_battery_present(struct smb137c_chip *chip)
  692. {
  693. int rc;
  694. u8 val;
  695. rc = smb137c_read_reg(chip, IRQ_STAT_B_REG, &val);
  696. if (rc || (val & IRQ_STAT_B_BATT_MISSING))
  697. return 0;
  698. /* Treat battery voltage less than 2.1 V as battery not present. */
  699. rc = smb137c_read_reg(chip, STAT_C_REG, &val);
  700. if (rc || (val & STAT_C_VBATT_LEVEL_BELOW_2P1V))
  701. return 0;
  702. return 1;
  703. }
  704. static int smb137c_get_property_battery_health(struct smb137c_chip *chip)
  705. {
  706. int rc;
  707. u8 val;
  708. /* The health of a disconnected battery is unknown. */
  709. if (!smb137c_get_property_battery_present(chip))
  710. return POWER_SUPPLY_HEALTH_UNKNOWN;
  711. rc = smb137c_read_reg(chip, IRQ_STAT_B_REG, &val);
  712. if (rc)
  713. return POWER_SUPPLY_HEALTH_UNKNOWN;
  714. if (val & IRQ_STAT_B_BATT_OVERVOLT)
  715. return POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  716. else if (val & IRQ_STAT_B_BATT_UNDERVOLT)
  717. return POWER_SUPPLY_HEALTH_DEAD;
  718. rc = smb137c_read_reg(chip, IRQ_STAT_A_REG, &val);
  719. if (rc)
  720. return POWER_SUPPLY_HEALTH_UNKNOWN;
  721. if (val & IRQ_STAT_A_BATT_HOT)
  722. return POWER_SUPPLY_HEALTH_OVERHEAT;
  723. else if (val & IRQ_STAT_A_BATT_COLD)
  724. return POWER_SUPPLY_HEALTH_COLD;
  725. return POWER_SUPPLY_HEALTH_GOOD;
  726. }
  727. static int smb137c_get_property_charge_type(struct smb137c_chip *chip)
  728. {
  729. enum smb137c_charge_status status;
  730. int charge_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
  731. bool charging_enabled;
  732. bool charging_error;
  733. int rc;
  734. u8 val;
  735. rc = smb137c_read_reg(chip, STAT_C_REG, &val);
  736. if (rc)
  737. return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  738. charging_enabled = val & STAT_C_CHG_ENABLED;
  739. charging_error = val & STAT_C_CHG_ERROR;
  740. status = (val & STAT_C_CHG_STAT_MASK) >> STAT_C_CHG_STAT_SHIFT;
  741. if (!charging_enabled) {
  742. dev_dbg(&chip->client->dev, "%s: not charging\n", __func__);
  743. } else if (charging_error) {
  744. dev_warn(&chip->client->dev, "%s: charger error detected\n",
  745. __func__);
  746. } else {
  747. charge_type = charge_status_type_map[status];
  748. }
  749. dev_dbg(&chip->client->dev, "%s: charging status=%s\n", __func__,
  750. charge_status_name[status]);
  751. return charge_type;
  752. }
  753. static enum power_supply_property smb137c_power_properties[] = {
  754. POWER_SUPPLY_PROP_STATUS,
  755. POWER_SUPPLY_PROP_HEALTH,
  756. POWER_SUPPLY_PROP_PRESENT,
  757. POWER_SUPPLY_PROP_CHARGING_ENABLED,
  758. POWER_SUPPLY_PROP_CHARGE_TYPE,
  759. POWER_SUPPLY_PROP_CURRENT_MAX,
  760. POWER_SUPPLY_PROP_TECHNOLOGY,
  761. POWER_SUPPLY_PROP_MODEL_NAME,
  762. POWER_SUPPLY_PROP_MANUFACTURER,
  763. };
  764. static int smb137c_property_is_writeable(struct power_supply *psy,
  765. enum power_supply_property psp)
  766. {
  767. switch (psp) {
  768. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  769. case POWER_SUPPLY_PROP_CURRENT_MAX:
  770. return 1;
  771. default:
  772. break;
  773. }
  774. return 0;
  775. }
  776. static int smb137c_power_set_property(struct power_supply *psy,
  777. enum power_supply_property psp,
  778. const union power_supply_propval *val)
  779. {
  780. struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
  781. mutex_lock(&chip->lock);
  782. switch (psp) {
  783. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  784. if (val->intval)
  785. smb137c_enable_charging(chip);
  786. else
  787. smb137c_disable_charging(chip);
  788. break;
  789. case POWER_SUPPLY_PROP_CURRENT_MAX:
  790. smb137c_set_charge_current_limit(chip, val->intval);
  791. break;
  792. default:
  793. mutex_unlock(&chip->lock);
  794. return -EINVAL;
  795. }
  796. mutex_unlock(&chip->lock);
  797. power_supply_changed(&chip->psy);
  798. return 0;
  799. }
  800. static int smb137c_power_get_property(struct power_supply *psy,
  801. enum power_supply_property psp,
  802. union power_supply_propval *val)
  803. {
  804. struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
  805. mutex_lock(&chip->lock);
  806. switch (psp) {
  807. case POWER_SUPPLY_PROP_STATUS:
  808. val->intval = smb137c_get_property_status(chip);
  809. break;
  810. case POWER_SUPPLY_PROP_HEALTH:
  811. val->intval = smb137c_get_property_battery_health(chip);
  812. break;
  813. case POWER_SUPPLY_PROP_PRESENT:
  814. val->intval = smb137c_get_property_battery_present(chip);
  815. break;
  816. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  817. val->intval = chip->charging_enabled;
  818. break;
  819. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  820. val->intval = smb137c_get_property_charge_type(chip);
  821. break;
  822. case POWER_SUPPLY_PROP_CURRENT_MAX:
  823. val->intval = chip->charge_current_limit_ua;
  824. break;
  825. case POWER_SUPPLY_PROP_TECHNOLOGY:
  826. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  827. break;
  828. case POWER_SUPPLY_PROP_MODEL_NAME:
  829. val->strval = "SMB137C";
  830. break;
  831. case POWER_SUPPLY_PROP_MANUFACTURER:
  832. val->strval = "Summit Microelectronics";
  833. break;
  834. default:
  835. mutex_unlock(&chip->lock);
  836. return -EINVAL;
  837. }
  838. mutex_unlock(&chip->lock);
  839. return 0;
  840. }
  841. static void smb137c_external_power_changed(struct power_supply *psy)
  842. {
  843. struct smb137c_chip *chip = container_of(psy, struct smb137c_chip, psy);
  844. union power_supply_propval prop = {0,};
  845. int scope = POWER_SUPPLY_SCOPE_DEVICE;
  846. int current_limit = 0;
  847. int online = 0;
  848. int rc;
  849. mutex_lock(&chip->lock);
  850. dev_dbg(&chip->client->dev, "%s: start\n", __func__);
  851. rc = chip->usb_psy->get_property(chip->usb_psy,
  852. POWER_SUPPLY_PROP_ONLINE, &prop);
  853. if (rc)
  854. dev_err(&chip->client->dev, "%s: could not read USB online property, rc=%d\n",
  855. __func__, rc);
  856. else
  857. online = prop.intval;
  858. rc = chip->usb_psy->get_property(chip->usb_psy, POWER_SUPPLY_PROP_SCOPE,
  859. &prop);
  860. if (rc)
  861. dev_err(&chip->client->dev, "%s: could not read USB scope property, rc=%d\n",
  862. __func__, rc);
  863. else
  864. scope = prop.intval;
  865. rc = chip->usb_psy->get_property(chip->usb_psy,
  866. POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
  867. if (rc)
  868. dev_err(&chip->client->dev, "%s: could not read USB current_max property, rc=%d\n",
  869. __func__, rc);
  870. else
  871. current_limit = prop.intval;
  872. if (scope == POWER_SUPPLY_SCOPE_SYSTEM) {
  873. /* USB host mode */
  874. smb137c_disable_charging(chip);
  875. smb137c_enable_otg_mode(chip);
  876. } else if (online) {
  877. /* USB online in device mode */
  878. smb137c_set_usb_input_current_limit(chip, current_limit);
  879. smb137c_enable_charging(chip);
  880. smb137c_disable_otg_mode(chip);
  881. } else {
  882. /* USB offline */
  883. smb137c_disable_charging(chip);
  884. smb137c_disable_otg_mode(chip);
  885. smb137c_set_usb_input_current_limit(chip,
  886. min(current_limit, USB_MIN_CURRENT_UA));
  887. }
  888. dev_dbg(&chip->client->dev, "%s: end\n", __func__);
  889. mutex_unlock(&chip->lock);
  890. power_supply_changed(&chip->psy);
  891. }
  892. static int __devinit smb137c_set_register_defaults(struct smb137c_chip *chip)
  893. {
  894. int rc;
  895. u8 val, mask;
  896. /* Allow volatile register writes. */
  897. rc = smb137c_masked_write_reg(chip, CMD_A_REG,
  898. CMD_A_VOLATILE_WRITE_MASK, CMD_A_VOLATILE_WRITE_ALLOW);
  899. if (rc)
  900. return rc;
  901. /* Do not reset register values on USB reinsertion. */
  902. rc = smb137c_masked_write_reg(chip, SAFETY_TIMER_REG,
  903. SAFETY_TIMER_RELOAD_MASK, SAFETY_TIMER_RELOAD_DISABLED);
  904. if (rc)
  905. return rc;
  906. /* Set various default control parameters. */
  907. val = PIN_CTRL_DEAD_BATT_CHG_ENABLED | PIN_CTRL_OTG
  908. | PIN_CTRL_USB_CUR_LIMIT_REG | PIN_CTRL_CHG_EN_REG_LOW
  909. | PIN_CTRL_OTG_CTRL_REG;
  910. mask = PIN_CTRL_DEAD_BATT_CHG_MASK | PIN_CTRL_OTG_LBR_MASK
  911. | PIN_CTRL_USB_CUR_LIMIT_MASK | PIN_CTRL_CHG_EN_MASK
  912. | PIN_CTRL_OTG_CTRL_MASK;
  913. rc = smb137c_masked_write_reg(chip, PIN_CTRL_REG, mask, val);
  914. if (rc)
  915. return rc;
  916. /* Disable charging, disable OTG mode, and allow fast-charge current. */
  917. val = CMD_A_CHARGING_DISABLED | CMD_A_OTG_DISABLED
  918. | CMD_A_FAST_CHG_ALLOW;
  919. mask = CMD_A_CHARGING_MASK | CMD_A_OTG_MASK | CMD_A_FAST_CHG_MASK;
  920. rc = smb137c_masked_write_reg(chip, CMD_A_REG, mask, val);
  921. if (rc)
  922. return rc;
  923. /* Enable auto recharging and full-time THERM monitor. */
  924. val = CTRL_A_AUTO_RECHARGE_ENABLED | CTRL_A_THERM_MONITOR_ENABLED;
  925. mask = CTRL_A_AUTO_RECHARGE_MASK | CTRL_A_THERM_MONITOR_MASK;
  926. rc = smb137c_masked_write_reg(chip, CTRL_A_REG, mask, val);
  927. if (rc)
  928. return rc;
  929. /* Use register value instead of pin to control USB suspend. */
  930. rc = smb137c_masked_write_reg(chip, VAR_FUNC_REG,
  931. VAR_FUNC_USB_SUSPEND_CTRL_MASK, VAR_FUNC_USB_SUSPEND_CTRL_REG);
  932. if (rc)
  933. return rc;
  934. return rc;
  935. }
  936. static int __devinit smb137c_apply_dt_configs(struct smb137c_chip *chip)
  937. {
  938. struct device *dev = &chip->client->dev;
  939. struct device_node *node = chip->client->dev.of_node;
  940. int ret, current_ma, voltage_mv, timeout, value;
  941. int rc = 0;
  942. /*
  943. * All device tree parameters are optional so it is ok if read calls
  944. * fail.
  945. */
  946. ret = of_property_read_u32(node, "summit,chg-current-ma", &current_ma);
  947. if (ret == 0) {
  948. rc = smb137c_set_charge_current_limit(chip, current_ma * 1000);
  949. if (rc) {
  950. dev_err(dev, "%s: Failed to set charge current, rc=%d\n",
  951. __func__, rc);
  952. return rc;
  953. }
  954. } else {
  955. chip->charge_current_limit_ua
  956. = smb137c_get_charge_current_limit(chip);
  957. rc = chip->charge_current_limit_ua;
  958. if (rc < 0) {
  959. dev_err(dev, "%s: Failed to get charge current, rc=%d\n",
  960. __func__, rc);
  961. return rc;
  962. }
  963. }
  964. ret = of_property_read_u32(node, "summit,term-current-ma", &current_ma);
  965. if (ret == 0) {
  966. rc = smb137c_set_term_current(chip, current_ma * 1000);
  967. if (rc) {
  968. dev_err(dev, "%s: Failed to set termination current, rc=%d\n",
  969. __func__, rc);
  970. return rc;
  971. }
  972. }
  973. ret = of_property_read_u32(node, "summit,pre-chg-current-ma",
  974. &current_ma);
  975. if (ret == 0) {
  976. rc = smb137c_set_pre_charge_current_limit(chip,
  977. current_ma * 1000);
  978. if (rc) {
  979. dev_err(dev, "%s: Failed to set pre-charge current limit, rc=%d\n",
  980. __func__, rc);
  981. return rc;
  982. }
  983. }
  984. ret = of_property_read_u32(node, "summit,float-voltage-mv",
  985. &voltage_mv);
  986. if (ret == 0) {
  987. rc = smb137c_set_float_voltage(chip, voltage_mv * 1000);
  988. if (rc) {
  989. dev_err(dev, "%s: Failed to set float voltage, rc=%d\n",
  990. __func__, rc);
  991. return rc;
  992. }
  993. }
  994. ret = of_property_read_u32(node, "summit,thresh-voltage-mv",
  995. &voltage_mv);
  996. if (ret == 0) {
  997. rc = smb137c_set_pre_charge_threshold_voltage(chip,
  998. voltage_mv * 1000);
  999. if (rc) {
  1000. dev_err(dev, "%s: Failed to set fast-charge threshold voltage, rc=%d\n",
  1001. __func__, rc);
  1002. return rc;
  1003. }
  1004. }
  1005. ret = of_property_read_u32(node, "summit,recharge-thresh-mv",
  1006. &voltage_mv);
  1007. if (ret == 0) {
  1008. rc = smb137c_set_recharge_threshold_voltage(chip,
  1009. voltage_mv * 1000);
  1010. if (rc) {
  1011. dev_err(dev, "%s: Failed to set recharge threshold voltage, rc=%d\n",
  1012. __func__, rc);
  1013. return rc;
  1014. }
  1015. }
  1016. ret = of_property_read_u32(node, "summit,system-voltage-mv",
  1017. &voltage_mv);
  1018. if (ret == 0) {
  1019. rc = smb137c_set_system_voltage(chip, voltage_mv * 1000);
  1020. if (rc) {
  1021. dev_err(dev, "%s: Failed to set system voltage, rc=%d\n",
  1022. __func__, rc);
  1023. return rc;
  1024. }
  1025. }
  1026. ret = of_property_read_u32(node, "summit,charging-timeout", &timeout);
  1027. if (ret == 0) {
  1028. rc = smb137c_set_charging_timeout(chip, timeout);
  1029. if (rc) {
  1030. dev_err(dev, "%s: Failed to set charging timeout, rc=%d\n",
  1031. __func__, rc);
  1032. return rc;
  1033. }
  1034. }
  1035. ret = of_property_read_u32(node, "summit,pre-charge-timeout", &timeout);
  1036. if (ret == 0) {
  1037. rc = smb137c_set_pre_charge_timeout(chip, timeout);
  1038. if (rc) {
  1039. dev_err(dev, "%s: Failed to set pre-charge timeout, rc=%d\n",
  1040. __func__, rc);
  1041. return rc;
  1042. }
  1043. }
  1044. ret = of_property_read_u32(node, "summit,therm-current-ua", &value);
  1045. if (ret == 0) {
  1046. rc = smb137c_set_thermistor_current(chip, value);
  1047. if (rc) {
  1048. dev_err(dev, "%s: Failed to set thermistor current, rc=%d\n",
  1049. __func__, rc);
  1050. return rc;
  1051. }
  1052. }
  1053. ret = of_property_read_u32(node, "summit,temperature-min", &value);
  1054. if (ret == 0) {
  1055. rc = smb137c_set_temperature_low_limit(chip, value);
  1056. if (rc) {
  1057. dev_err(dev, "%s: Failed to set low temperature limit, rc=%d\n",
  1058. __func__, rc);
  1059. return rc;
  1060. }
  1061. }
  1062. ret = of_property_read_u32(node, "summit,temperature-max", &value);
  1063. if (ret == 0) {
  1064. rc = smb137c_set_temperature_high_limit(chip, value);
  1065. if (rc) {
  1066. dev_err(dev, "%s: Failed to set high temperature limit, rc=%d\n",
  1067. __func__, rc);
  1068. return rc;
  1069. }
  1070. }
  1071. return rc;
  1072. }
  1073. static int __devinit smb137c_probe(struct i2c_client *client,
  1074. const struct i2c_device_id *id)
  1075. {
  1076. struct smb137c_chip *chip;
  1077. struct device *dev = &client->dev;
  1078. struct device_node *node = client->dev.of_node;
  1079. int rc = 0;
  1080. int gui_rev, silicon_rev;
  1081. u8 dev_id;
  1082. if (!node) {
  1083. dev_err(dev, "%s: device tree information missing\n", __func__);
  1084. return -ENODEV;
  1085. }
  1086. if (!i2c_check_functionality(client->adapter,
  1087. I2C_FUNC_SMBUS_BYTE_DATA)) {
  1088. dev_err(dev, "%s: SMBUS_BYTE_DATA unsupported\n", __func__);
  1089. return -EIO;
  1090. }
  1091. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  1092. if (!chip) {
  1093. dev_err(dev, "%s: devm_kzalloc failed\n", __func__);
  1094. return -ENOMEM;
  1095. }
  1096. mutex_init(&chip->lock);
  1097. chip->client = client;
  1098. i2c_set_clientdata(client, chip);
  1099. chip->usb_psy = power_supply_get_by_name("usb");
  1100. if (!chip->usb_psy) {
  1101. dev_dbg(dev, "%s: USB supply not found; deferring charger probe\n",
  1102. __func__);
  1103. return -EPROBE_DEFER;
  1104. }
  1105. rc = smb137c_read_reg(chip, DEV_ID_REG, &dev_id);
  1106. if (rc)
  1107. return rc;
  1108. if ((dev_id & DEV_ID_PART_MASK) != DEV_ID_PART_SMB137C) {
  1109. dev_err(dev, "%s: invalid device ID=0x%02X\n", __func__,
  1110. dev_id);
  1111. return -ENODEV;
  1112. }
  1113. gui_rev = (dev_id & DEV_ID_GUI_REV_MASK) >> DEV_ID_GUI_REV_SHIFT;
  1114. silicon_rev = (dev_id & DEV_ID_SILICON_REV_MASK)
  1115. >> DEV_ID_SILICON_REV_SHIFT;
  1116. rc = smb137c_set_register_defaults(chip);
  1117. if (rc)
  1118. return rc;
  1119. rc = smb137c_apply_dt_configs(chip);
  1120. if (rc)
  1121. return rc;
  1122. chip->psy.name = "battery";
  1123. chip->psy.type = POWER_SUPPLY_TYPE_BATTERY;
  1124. chip->psy.properties = smb137c_power_properties;
  1125. chip->psy.num_properties = ARRAY_SIZE(smb137c_power_properties);
  1126. chip->psy.get_property = smb137c_power_get_property;
  1127. chip->psy.set_property = smb137c_power_set_property;
  1128. chip->psy.property_is_writeable = smb137c_property_is_writeable;
  1129. chip->psy.external_power_changed = smb137c_external_power_changed;
  1130. rc = power_supply_register(dev, &chip->psy);
  1131. if (rc < 0) {
  1132. dev_err(dev, "%s: power_supply_register failed, rc=%d\n",
  1133. __func__, rc);
  1134. return rc;
  1135. }
  1136. smb137c_external_power_changed(&chip->psy);
  1137. dev_info(dev, "%s: SMB137C charger probed successfully, gui_rev=%d, silicon_rev=%d\n",
  1138. __func__, gui_rev, silicon_rev);
  1139. return rc;
  1140. }
  1141. static int __devexit smb137c_remove(struct i2c_client *client)
  1142. {
  1143. return 0;
  1144. }
  1145. static const struct i2c_device_id smb137c_id[] = {
  1146. { .name = "smb137c", },
  1147. {},
  1148. };
  1149. MODULE_DEVICE_TABLE(i2c, smb137c_id);
  1150. static const struct of_device_id smb137c_match[] = {
  1151. { .compatible = "summit,smb137c", },
  1152. { },
  1153. };
  1154. static struct i2c_driver smb137c_driver = {
  1155. .driver = {
  1156. .name = "smb137c",
  1157. .owner = THIS_MODULE,
  1158. .of_match_table = smb137c_match,
  1159. },
  1160. .probe = smb137c_probe,
  1161. .remove = __devexit_p(smb137c_remove),
  1162. .id_table = smb137c_id,
  1163. };
  1164. static int __init smb137c_init(void)
  1165. {
  1166. return i2c_add_driver(&smb137c_driver);
  1167. }
  1168. module_init(smb137c_init);
  1169. static void __exit smb137c_exit(void)
  1170. {
  1171. return i2c_del_driver(&smb137c_driver);
  1172. }
  1173. module_exit(smb137c_exit);
  1174. MODULE_DESCRIPTION("SMB137C Charger");
  1175. MODULE_LICENSE("GPL v2");
  1176. MODULE_ALIAS("i2c:smb137c");