smb347-charger.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * Summit Microelectronics SMB347 Battery Charger Driver
  3. *
  4. * Copyright (C) 2011, Intel Corporation
  5. *
  6. * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com>
  7. * Mika Westerberg <mika.westerberg@linux.intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/gpio.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/i2c.h>
  20. #include <linux/mutex.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/power/smb347-charger.h>
  23. #include <linux/regmap.h>
  24. /*
  25. * Configuration registers. These are mirrored to volatile RAM and can be
  26. * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
  27. * reloaded from non-volatile registers after POR.
  28. */
  29. #define CFG_CHARGE_CURRENT 0x00
  30. #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
  31. #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
  32. #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
  33. #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
  34. #define CFG_CHARGE_CURRENT_TC_MASK 0x07
  35. #define CFG_CURRENT_LIMIT 0x01
  36. #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
  37. #define CFG_CURRENT_LIMIT_DC_SHIFT 4
  38. #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
  39. #define CFG_FLOAT_VOLTAGE 0x03
  40. #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f
  41. #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
  42. #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
  43. #define CFG_STAT 0x05
  44. #define CFG_STAT_DISABLED BIT(5)
  45. #define CFG_STAT_ACTIVE_HIGH BIT(7)
  46. #define CFG_PIN 0x06
  47. #define CFG_PIN_EN_CTRL_MASK 0x60
  48. #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
  49. #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
  50. #define CFG_PIN_EN_APSD_IRQ BIT(1)
  51. #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
  52. #define CFG_THERM 0x07
  53. #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
  54. #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
  55. #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
  56. #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
  57. #define CFG_THERM_MONITOR_DISABLED BIT(4)
  58. #define CFG_SYSOK 0x08
  59. #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
  60. #define CFG_OTHER 0x09
  61. #define CFG_OTHER_RID_MASK 0xc0
  62. #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
  63. #define CFG_OTG 0x0a
  64. #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
  65. #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
  66. #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
  67. #define CFG_OTG_CC_COMPENSATION_SHIFT 6
  68. #define CFG_TEMP_LIMIT 0x0b
  69. #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
  70. #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
  71. #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
  72. #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
  73. #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
  74. #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
  75. #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
  76. #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
  77. #define CFG_FAULT_IRQ 0x0c
  78. #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
  79. #define CFG_STATUS_IRQ 0x0d
  80. #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
  81. #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7)
  82. #define CFG_ADDRESS 0x0e
  83. /* Command registers */
  84. #define CMD_A 0x30
  85. #define CMD_A_CHG_ENABLED BIT(1)
  86. #define CMD_A_SUSPEND_ENABLED BIT(2)
  87. #define CMD_A_ALLOW_WRITE BIT(7)
  88. #define CMD_B 0x31
  89. #define CMD_C 0x33
  90. /* Interrupt Status registers */
  91. #define IRQSTAT_A 0x35
  92. #define IRQSTAT_C 0x37
  93. #define IRQSTAT_C_TERMINATION_STAT BIT(0)
  94. #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
  95. #define IRQSTAT_C_TAPER_IRQ BIT(3)
  96. #define IRQSTAT_D 0x38
  97. #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2)
  98. #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3)
  99. #define IRQSTAT_E 0x39
  100. #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
  101. #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
  102. #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
  103. #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
  104. #define IRQSTAT_F 0x3a
  105. /* Status registers */
  106. #define STAT_A 0x3b
  107. #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
  108. #define STAT_B 0x3c
  109. #define STAT_C 0x3d
  110. #define STAT_C_CHG_ENABLED BIT(0)
  111. #define STAT_C_HOLDOFF_STAT BIT(3)
  112. #define STAT_C_CHG_MASK 0x06
  113. #define STAT_C_CHG_SHIFT 1
  114. #define STAT_C_CHG_TERM BIT(5)
  115. #define STAT_C_CHARGER_ERROR BIT(6)
  116. #define STAT_E 0x3f
  117. #define SMB347_MAX_REGISTER 0x3f
  118. /**
  119. * struct smb347_charger - smb347 charger instance
  120. * @lock: protects concurrent access to online variables
  121. * @dev: pointer to device
  122. * @regmap: pointer to driver regmap
  123. * @mains: power_supply instance for AC/DC power
  124. * @usb: power_supply instance for USB power
  125. * @battery: power_supply instance for battery
  126. * @mains_online: is AC/DC input connected
  127. * @usb_online: is USB input connected
  128. * @charging_enabled: is charging enabled
  129. * @pdata: pointer to platform data
  130. */
  131. struct smb347_charger {
  132. struct mutex lock;
  133. struct device *dev;
  134. struct regmap *regmap;
  135. struct power_supply *mains;
  136. struct power_supply *usb;
  137. struct power_supply *battery;
  138. bool mains_online;
  139. bool usb_online;
  140. bool charging_enabled;
  141. const struct smb347_charger_platform_data *pdata;
  142. };
  143. /* Fast charge current in uA */
  144. static const unsigned int fcc_tbl[] = {
  145. 700000,
  146. 900000,
  147. 1200000,
  148. 1500000,
  149. 1800000,
  150. 2000000,
  151. 2200000,
  152. 2500000,
  153. };
  154. /* Pre-charge current in uA */
  155. static const unsigned int pcc_tbl[] = {
  156. 100000,
  157. 150000,
  158. 200000,
  159. 250000,
  160. };
  161. /* Termination current in uA */
  162. static const unsigned int tc_tbl[] = {
  163. 37500,
  164. 50000,
  165. 100000,
  166. 150000,
  167. 200000,
  168. 250000,
  169. 500000,
  170. 600000,
  171. };
  172. /* Input current limit in uA */
  173. static const unsigned int icl_tbl[] = {
  174. 300000,
  175. 500000,
  176. 700000,
  177. 900000,
  178. 1200000,
  179. 1500000,
  180. 1800000,
  181. 2000000,
  182. 2200000,
  183. 2500000,
  184. };
  185. /* Charge current compensation in uA */
  186. static const unsigned int ccc_tbl[] = {
  187. 250000,
  188. 700000,
  189. 900000,
  190. 1200000,
  191. };
  192. /* Convert register value to current using lookup table */
  193. static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
  194. {
  195. if (val >= size)
  196. return -EINVAL;
  197. return tbl[val];
  198. }
  199. /* Convert current to register value using lookup table */
  200. static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
  201. {
  202. size_t i;
  203. for (i = 0; i < size; i++)
  204. if (val < tbl[i])
  205. break;
  206. return i > 0 ? i - 1 : -EINVAL;
  207. }
  208. /**
  209. * smb347_update_ps_status - refreshes the power source status
  210. * @smb: pointer to smb347 charger instance
  211. *
  212. * Function checks whether any power source is connected to the charger and
  213. * updates internal state accordingly. If there is a change to previous state
  214. * function returns %1, otherwise %0 and negative errno in case of errror.
  215. */
  216. static int smb347_update_ps_status(struct smb347_charger *smb)
  217. {
  218. bool usb = false;
  219. bool dc = false;
  220. unsigned int val;
  221. int ret;
  222. ret = regmap_read(smb->regmap, IRQSTAT_E, &val);
  223. if (ret < 0)
  224. return ret;
  225. /*
  226. * Dc and usb are set depending on whether they are enabled in
  227. * platform data _and_ whether corresponding undervoltage is set.
  228. */
  229. if (smb->pdata->use_mains)
  230. dc = !(val & IRQSTAT_E_DCIN_UV_STAT);
  231. if (smb->pdata->use_usb)
  232. usb = !(val & IRQSTAT_E_USBIN_UV_STAT);
  233. mutex_lock(&smb->lock);
  234. ret = smb->mains_online != dc || smb->usb_online != usb;
  235. smb->mains_online = dc;
  236. smb->usb_online = usb;
  237. mutex_unlock(&smb->lock);
  238. return ret;
  239. }
  240. /*
  241. * smb347_is_ps_online - returns whether input power source is connected
  242. * @smb: pointer to smb347 charger instance
  243. *
  244. * Returns %true if input power source is connected. Note that this is
  245. * dependent on what platform has configured for usable power sources. For
  246. * example if USB is disabled, this will return %false even if the USB cable
  247. * is connected.
  248. */
  249. static bool smb347_is_ps_online(struct smb347_charger *smb)
  250. {
  251. bool ret;
  252. mutex_lock(&smb->lock);
  253. ret = smb->usb_online || smb->mains_online;
  254. mutex_unlock(&smb->lock);
  255. return ret;
  256. }
  257. /**
  258. * smb347_charging_status - returns status of charging
  259. * @smb: pointer to smb347 charger instance
  260. *
  261. * Function returns charging status. %0 means no charging is in progress,
  262. * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
  263. */
  264. static int smb347_charging_status(struct smb347_charger *smb)
  265. {
  266. unsigned int val;
  267. int ret;
  268. if (!smb347_is_ps_online(smb))
  269. return 0;
  270. ret = regmap_read(smb->regmap, STAT_C, &val);
  271. if (ret < 0)
  272. return 0;
  273. return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
  274. }
  275. static int smb347_charging_set(struct smb347_charger *smb, bool enable)
  276. {
  277. int ret = 0;
  278. if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
  279. dev_dbg(smb->dev, "charging enable/disable in SW disabled\n");
  280. return 0;
  281. }
  282. mutex_lock(&smb->lock);
  283. if (smb->charging_enabled != enable) {
  284. ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED,
  285. enable ? CMD_A_CHG_ENABLED : 0);
  286. if (!ret)
  287. smb->charging_enabled = enable;
  288. }
  289. mutex_unlock(&smb->lock);
  290. return ret;
  291. }
  292. static inline int smb347_charging_enable(struct smb347_charger *smb)
  293. {
  294. return smb347_charging_set(smb, true);
  295. }
  296. static inline int smb347_charging_disable(struct smb347_charger *smb)
  297. {
  298. return smb347_charging_set(smb, false);
  299. }
  300. static int smb347_start_stop_charging(struct smb347_charger *smb)
  301. {
  302. int ret;
  303. /*
  304. * Depending on whether valid power source is connected or not, we
  305. * disable or enable the charging. We do it manually because it
  306. * depends on how the platform has configured the valid inputs.
  307. */
  308. if (smb347_is_ps_online(smb)) {
  309. ret = smb347_charging_enable(smb);
  310. if (ret < 0)
  311. dev_err(smb->dev, "failed to enable charging\n");
  312. } else {
  313. ret = smb347_charging_disable(smb);
  314. if (ret < 0)
  315. dev_err(smb->dev, "failed to disable charging\n");
  316. }
  317. return ret;
  318. }
  319. static int smb347_set_charge_current(struct smb347_charger *smb)
  320. {
  321. int ret;
  322. if (smb->pdata->max_charge_current) {
  323. ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
  324. smb->pdata->max_charge_current);
  325. if (ret < 0)
  326. return ret;
  327. ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
  328. CFG_CHARGE_CURRENT_FCC_MASK,
  329. ret << CFG_CHARGE_CURRENT_FCC_SHIFT);
  330. if (ret < 0)
  331. return ret;
  332. }
  333. if (smb->pdata->pre_charge_current) {
  334. ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
  335. smb->pdata->pre_charge_current);
  336. if (ret < 0)
  337. return ret;
  338. ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
  339. CFG_CHARGE_CURRENT_PCC_MASK,
  340. ret << CFG_CHARGE_CURRENT_PCC_SHIFT);
  341. if (ret < 0)
  342. return ret;
  343. }
  344. if (smb->pdata->termination_current) {
  345. ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
  346. smb->pdata->termination_current);
  347. if (ret < 0)
  348. return ret;
  349. ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT,
  350. CFG_CHARGE_CURRENT_TC_MASK, ret);
  351. if (ret < 0)
  352. return ret;
  353. }
  354. return 0;
  355. }
  356. static int smb347_set_current_limits(struct smb347_charger *smb)
  357. {
  358. int ret;
  359. if (smb->pdata->mains_current_limit) {
  360. ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
  361. smb->pdata->mains_current_limit);
  362. if (ret < 0)
  363. return ret;
  364. ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
  365. CFG_CURRENT_LIMIT_DC_MASK,
  366. ret << CFG_CURRENT_LIMIT_DC_SHIFT);
  367. if (ret < 0)
  368. return ret;
  369. }
  370. if (smb->pdata->usb_hc_current_limit) {
  371. ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
  372. smb->pdata->usb_hc_current_limit);
  373. if (ret < 0)
  374. return ret;
  375. ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT,
  376. CFG_CURRENT_LIMIT_USB_MASK, ret);
  377. if (ret < 0)
  378. return ret;
  379. }
  380. return 0;
  381. }
  382. static int smb347_set_voltage_limits(struct smb347_charger *smb)
  383. {
  384. int ret;
  385. if (smb->pdata->pre_to_fast_voltage) {
  386. ret = smb->pdata->pre_to_fast_voltage;
  387. /* uV */
  388. ret = clamp_val(ret, 2400000, 3000000) - 2400000;
  389. ret /= 200000;
  390. ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
  391. CFG_FLOAT_VOLTAGE_THRESHOLD_MASK,
  392. ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT);
  393. if (ret < 0)
  394. return ret;
  395. }
  396. if (smb->pdata->max_charge_voltage) {
  397. ret = smb->pdata->max_charge_voltage;
  398. /* uV */
  399. ret = clamp_val(ret, 3500000, 4500000) - 3500000;
  400. ret /= 20000;
  401. ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE,
  402. CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret);
  403. if (ret < 0)
  404. return ret;
  405. }
  406. return 0;
  407. }
  408. static int smb347_set_temp_limits(struct smb347_charger *smb)
  409. {
  410. bool enable_therm_monitor = false;
  411. int ret = 0;
  412. int val;
  413. if (smb->pdata->chip_temp_threshold) {
  414. val = smb->pdata->chip_temp_threshold;
  415. /* degree C */
  416. val = clamp_val(val, 100, 130) - 100;
  417. val /= 10;
  418. ret = regmap_update_bits(smb->regmap, CFG_OTG,
  419. CFG_OTG_TEMP_THRESHOLD_MASK,
  420. val << CFG_OTG_TEMP_THRESHOLD_SHIFT);
  421. if (ret < 0)
  422. return ret;
  423. }
  424. if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  425. val = smb->pdata->soft_cold_temp_limit;
  426. val = clamp_val(val, 0, 15);
  427. val /= 5;
  428. /* this goes from higher to lower so invert the value */
  429. val = ~val & 0x3;
  430. ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
  431. CFG_TEMP_LIMIT_SOFT_COLD_MASK,
  432. val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT);
  433. if (ret < 0)
  434. return ret;
  435. enable_therm_monitor = true;
  436. }
  437. if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  438. val = smb->pdata->soft_hot_temp_limit;
  439. val = clamp_val(val, 40, 55) - 40;
  440. val /= 5;
  441. ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
  442. CFG_TEMP_LIMIT_SOFT_HOT_MASK,
  443. val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT);
  444. if (ret < 0)
  445. return ret;
  446. enable_therm_monitor = true;
  447. }
  448. if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  449. val = smb->pdata->hard_cold_temp_limit;
  450. val = clamp_val(val, -5, 10) + 5;
  451. val /= 5;
  452. /* this goes from higher to lower so invert the value */
  453. val = ~val & 0x3;
  454. ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
  455. CFG_TEMP_LIMIT_HARD_COLD_MASK,
  456. val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT);
  457. if (ret < 0)
  458. return ret;
  459. enable_therm_monitor = true;
  460. }
  461. if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  462. val = smb->pdata->hard_hot_temp_limit;
  463. val = clamp_val(val, 50, 65) - 50;
  464. val /= 5;
  465. ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT,
  466. CFG_TEMP_LIMIT_HARD_HOT_MASK,
  467. val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT);
  468. if (ret < 0)
  469. return ret;
  470. enable_therm_monitor = true;
  471. }
  472. /*
  473. * If any of the temperature limits are set, we also enable the
  474. * thermistor monitoring.
  475. *
  476. * When soft limits are hit, the device will start to compensate
  477. * current and/or voltage depending on the configuration.
  478. *
  479. * When hard limit is hit, the device will suspend charging
  480. * depending on the configuration.
  481. */
  482. if (enable_therm_monitor) {
  483. ret = regmap_update_bits(smb->regmap, CFG_THERM,
  484. CFG_THERM_MONITOR_DISABLED, 0);
  485. if (ret < 0)
  486. return ret;
  487. }
  488. if (smb->pdata->suspend_on_hard_temp_limit) {
  489. ret = regmap_update_bits(smb->regmap, CFG_SYSOK,
  490. CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0);
  491. if (ret < 0)
  492. return ret;
  493. }
  494. if (smb->pdata->soft_temp_limit_compensation !=
  495. SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
  496. val = smb->pdata->soft_temp_limit_compensation & 0x3;
  497. ret = regmap_update_bits(smb->regmap, CFG_THERM,
  498. CFG_THERM_SOFT_HOT_COMPENSATION_MASK,
  499. val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT);
  500. if (ret < 0)
  501. return ret;
  502. ret = regmap_update_bits(smb->regmap, CFG_THERM,
  503. CFG_THERM_SOFT_COLD_COMPENSATION_MASK,
  504. val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT);
  505. if (ret < 0)
  506. return ret;
  507. }
  508. if (smb->pdata->charge_current_compensation) {
  509. val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
  510. smb->pdata->charge_current_compensation);
  511. if (val < 0)
  512. return val;
  513. ret = regmap_update_bits(smb->regmap, CFG_OTG,
  514. CFG_OTG_CC_COMPENSATION_MASK,
  515. (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT);
  516. if (ret < 0)
  517. return ret;
  518. }
  519. return ret;
  520. }
  521. /*
  522. * smb347_set_writable - enables/disables writing to non-volatile registers
  523. * @smb: pointer to smb347 charger instance
  524. *
  525. * You can enable/disable writing to the non-volatile configuration
  526. * registers by calling this function.
  527. *
  528. * Returns %0 on success and negative errno in case of failure.
  529. */
  530. static int smb347_set_writable(struct smb347_charger *smb, bool writable)
  531. {
  532. return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE,
  533. writable ? CMD_A_ALLOW_WRITE : 0);
  534. }
  535. static int smb347_hw_init(struct smb347_charger *smb)
  536. {
  537. unsigned int val;
  538. int ret;
  539. ret = smb347_set_writable(smb, true);
  540. if (ret < 0)
  541. return ret;
  542. /*
  543. * Program the platform specific configuration values to the device
  544. * first.
  545. */
  546. ret = smb347_set_charge_current(smb);
  547. if (ret < 0)
  548. goto fail;
  549. ret = smb347_set_current_limits(smb);
  550. if (ret < 0)
  551. goto fail;
  552. ret = smb347_set_voltage_limits(smb);
  553. if (ret < 0)
  554. goto fail;
  555. ret = smb347_set_temp_limits(smb);
  556. if (ret < 0)
  557. goto fail;
  558. /* If USB charging is disabled we put the USB in suspend mode */
  559. if (!smb->pdata->use_usb) {
  560. ret = regmap_update_bits(smb->regmap, CMD_A,
  561. CMD_A_SUSPEND_ENABLED,
  562. CMD_A_SUSPEND_ENABLED);
  563. if (ret < 0)
  564. goto fail;
  565. }
  566. /*
  567. * If configured by platform data, we enable hardware Auto-OTG
  568. * support for driving VBUS. Otherwise we disable it.
  569. */
  570. ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK,
  571. smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0);
  572. if (ret < 0)
  573. goto fail;
  574. /*
  575. * Make the charging functionality controllable by a write to the
  576. * command register unless pin control is specified in the platform
  577. * data.
  578. */
  579. switch (smb->pdata->enable_control) {
  580. case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
  581. val = CFG_PIN_EN_CTRL_ACTIVE_LOW;
  582. break;
  583. case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
  584. val = CFG_PIN_EN_CTRL_ACTIVE_HIGH;
  585. break;
  586. default:
  587. val = 0;
  588. break;
  589. }
  590. ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK,
  591. val);
  592. if (ret < 0)
  593. goto fail;
  594. /* Disable Automatic Power Source Detection (APSD) interrupt. */
  595. ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0);
  596. if (ret < 0)
  597. goto fail;
  598. ret = smb347_update_ps_status(smb);
  599. if (ret < 0)
  600. goto fail;
  601. ret = smb347_start_stop_charging(smb);
  602. fail:
  603. smb347_set_writable(smb, false);
  604. return ret;
  605. }
  606. static irqreturn_t smb347_interrupt(int irq, void *data)
  607. {
  608. struct smb347_charger *smb = data;
  609. unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e;
  610. bool handled = false;
  611. int ret;
  612. ret = regmap_read(smb->regmap, STAT_C, &stat_c);
  613. if (ret < 0) {
  614. dev_warn(smb->dev, "reading STAT_C failed\n");
  615. return IRQ_NONE;
  616. }
  617. ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c);
  618. if (ret < 0) {
  619. dev_warn(smb->dev, "reading IRQSTAT_C failed\n");
  620. return IRQ_NONE;
  621. }
  622. ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d);
  623. if (ret < 0) {
  624. dev_warn(smb->dev, "reading IRQSTAT_D failed\n");
  625. return IRQ_NONE;
  626. }
  627. ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e);
  628. if (ret < 0) {
  629. dev_warn(smb->dev, "reading IRQSTAT_E failed\n");
  630. return IRQ_NONE;
  631. }
  632. /*
  633. * If we get charger error we report the error back to user.
  634. * If the error is recovered charging will resume again.
  635. */
  636. if (stat_c & STAT_C_CHARGER_ERROR) {
  637. dev_err(smb->dev, "charging stopped due to charger error\n");
  638. power_supply_changed(smb->battery);
  639. handled = true;
  640. }
  641. /*
  642. * If we reached the termination current the battery is charged and
  643. * we can update the status now. Charging is automatically
  644. * disabled by the hardware.
  645. */
  646. if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) {
  647. if (irqstat_c & IRQSTAT_C_TERMINATION_STAT)
  648. power_supply_changed(smb->battery);
  649. dev_dbg(smb->dev, "going to HW maintenance mode\n");
  650. handled = true;
  651. }
  652. /*
  653. * If we got a charger timeout INT that means the charge
  654. * full is not detected with in charge timeout value.
  655. */
  656. if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) {
  657. dev_dbg(smb->dev, "total Charge Timeout INT received\n");
  658. if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT)
  659. dev_warn(smb->dev, "charging stopped due to timeout\n");
  660. power_supply_changed(smb->battery);
  661. handled = true;
  662. }
  663. /*
  664. * If we got an under voltage interrupt it means that AC/USB input
  665. * was connected or disconnected.
  666. */
  667. if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) {
  668. if (smb347_update_ps_status(smb) > 0) {
  669. smb347_start_stop_charging(smb);
  670. if (smb->pdata->use_mains)
  671. power_supply_changed(smb->mains);
  672. if (smb->pdata->use_usb)
  673. power_supply_changed(smb->usb);
  674. }
  675. handled = true;
  676. }
  677. return handled ? IRQ_HANDLED : IRQ_NONE;
  678. }
  679. static int smb347_irq_set(struct smb347_charger *smb, bool enable)
  680. {
  681. int ret;
  682. ret = smb347_set_writable(smb, true);
  683. if (ret < 0)
  684. return ret;
  685. /*
  686. * Enable/disable interrupts for:
  687. * - under voltage
  688. * - termination current reached
  689. * - charger timeout
  690. * - charger error
  691. */
  692. ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff,
  693. enable ? CFG_FAULT_IRQ_DCIN_UV : 0);
  694. if (ret < 0)
  695. goto fail;
  696. ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff,
  697. enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER |
  698. CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0);
  699. if (ret < 0)
  700. goto fail;
  701. ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR,
  702. enable ? CFG_PIN_EN_CHARGER_ERROR : 0);
  703. fail:
  704. smb347_set_writable(smb, false);
  705. return ret;
  706. }
  707. static inline int smb347_irq_enable(struct smb347_charger *smb)
  708. {
  709. return smb347_irq_set(smb, true);
  710. }
  711. static inline int smb347_irq_disable(struct smb347_charger *smb)
  712. {
  713. return smb347_irq_set(smb, false);
  714. }
  715. static int smb347_irq_init(struct smb347_charger *smb,
  716. struct i2c_client *client)
  717. {
  718. const struct smb347_charger_platform_data *pdata = smb->pdata;
  719. int ret, irq = gpio_to_irq(pdata->irq_gpio);
  720. ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name);
  721. if (ret < 0)
  722. goto fail;
  723. ret = request_threaded_irq(irq, NULL, smb347_interrupt,
  724. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  725. client->name, smb);
  726. if (ret < 0)
  727. goto fail_gpio;
  728. ret = smb347_set_writable(smb, true);
  729. if (ret < 0)
  730. goto fail_irq;
  731. /*
  732. * Configure the STAT output to be suitable for interrupts: disable
  733. * all other output (except interrupts) and make it active low.
  734. */
  735. ret = regmap_update_bits(smb->regmap, CFG_STAT,
  736. CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED,
  737. CFG_STAT_DISABLED);
  738. if (ret < 0)
  739. goto fail_readonly;
  740. smb347_set_writable(smb, false);
  741. client->irq = irq;
  742. return 0;
  743. fail_readonly:
  744. smb347_set_writable(smb, false);
  745. fail_irq:
  746. free_irq(irq, smb);
  747. fail_gpio:
  748. gpio_free(pdata->irq_gpio);
  749. fail:
  750. client->irq = 0;
  751. return ret;
  752. }
  753. /*
  754. * Returns the constant charge current programmed
  755. * into the charger in uA.
  756. */
  757. static int get_const_charge_current(struct smb347_charger *smb)
  758. {
  759. int ret, intval;
  760. unsigned int v;
  761. if (!smb347_is_ps_online(smb))
  762. return -ENODATA;
  763. ret = regmap_read(smb->regmap, STAT_B, &v);
  764. if (ret < 0)
  765. return ret;
  766. /*
  767. * The current value is composition of FCC and PCC values
  768. * and we can detect which table to use from bit 5.
  769. */
  770. if (v & 0x20) {
  771. intval = hw_to_current(fcc_tbl, ARRAY_SIZE(fcc_tbl), v & 7);
  772. } else {
  773. v >>= 3;
  774. intval = hw_to_current(pcc_tbl, ARRAY_SIZE(pcc_tbl), v & 7);
  775. }
  776. return intval;
  777. }
  778. /*
  779. * Returns the constant charge voltage programmed
  780. * into the charger in uV.
  781. */
  782. static int get_const_charge_voltage(struct smb347_charger *smb)
  783. {
  784. int ret, intval;
  785. unsigned int v;
  786. if (!smb347_is_ps_online(smb))
  787. return -ENODATA;
  788. ret = regmap_read(smb->regmap, STAT_A, &v);
  789. if (ret < 0)
  790. return ret;
  791. v &= STAT_A_FLOAT_VOLTAGE_MASK;
  792. if (v > 0x3d)
  793. v = 0x3d;
  794. intval = 3500000 + v * 20000;
  795. return intval;
  796. }
  797. static int smb347_mains_get_property(struct power_supply *psy,
  798. enum power_supply_property prop,
  799. union power_supply_propval *val)
  800. {
  801. struct smb347_charger *smb = power_supply_get_drvdata(psy);
  802. int ret;
  803. switch (prop) {
  804. case POWER_SUPPLY_PROP_ONLINE:
  805. val->intval = smb->mains_online;
  806. break;
  807. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  808. ret = get_const_charge_voltage(smb);
  809. if (ret < 0)
  810. return ret;
  811. else
  812. val->intval = ret;
  813. break;
  814. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  815. ret = get_const_charge_current(smb);
  816. if (ret < 0)
  817. return ret;
  818. else
  819. val->intval = ret;
  820. break;
  821. default:
  822. return -EINVAL;
  823. }
  824. return 0;
  825. }
  826. static enum power_supply_property smb347_mains_properties[] = {
  827. POWER_SUPPLY_PROP_ONLINE,
  828. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  829. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  830. };
  831. static int smb347_usb_get_property(struct power_supply *psy,
  832. enum power_supply_property prop,
  833. union power_supply_propval *val)
  834. {
  835. struct smb347_charger *smb = power_supply_get_drvdata(psy);
  836. int ret;
  837. switch (prop) {
  838. case POWER_SUPPLY_PROP_ONLINE:
  839. val->intval = smb->usb_online;
  840. break;
  841. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  842. ret = get_const_charge_voltage(smb);
  843. if (ret < 0)
  844. return ret;
  845. else
  846. val->intval = ret;
  847. break;
  848. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  849. ret = get_const_charge_current(smb);
  850. if (ret < 0)
  851. return ret;
  852. else
  853. val->intval = ret;
  854. break;
  855. default:
  856. return -EINVAL;
  857. }
  858. return 0;
  859. }
  860. static enum power_supply_property smb347_usb_properties[] = {
  861. POWER_SUPPLY_PROP_ONLINE,
  862. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  863. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  864. };
  865. static int smb347_get_charging_status(struct smb347_charger *smb)
  866. {
  867. int ret, status;
  868. unsigned int val;
  869. if (!smb347_is_ps_online(smb))
  870. return POWER_SUPPLY_STATUS_DISCHARGING;
  871. ret = regmap_read(smb->regmap, STAT_C, &val);
  872. if (ret < 0)
  873. return ret;
  874. if ((val & STAT_C_CHARGER_ERROR) ||
  875. (val & STAT_C_HOLDOFF_STAT)) {
  876. /*
  877. * set to NOT CHARGING upon charger error
  878. * or charging has stopped.
  879. */
  880. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  881. } else {
  882. if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) {
  883. /*
  884. * set to charging if battery is in pre-charge,
  885. * fast charge or taper charging mode.
  886. */
  887. status = POWER_SUPPLY_STATUS_CHARGING;
  888. } else if (val & STAT_C_CHG_TERM) {
  889. /*
  890. * set the status to FULL if battery is not in pre
  891. * charge, fast charge or taper charging mode AND
  892. * charging is terminated at least once.
  893. */
  894. status = POWER_SUPPLY_STATUS_FULL;
  895. } else {
  896. /*
  897. * in this case no charger error or termination
  898. * occured but charging is not in progress!!!
  899. */
  900. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  901. }
  902. }
  903. return status;
  904. }
  905. static int smb347_battery_get_property(struct power_supply *psy,
  906. enum power_supply_property prop,
  907. union power_supply_propval *val)
  908. {
  909. struct smb347_charger *smb = power_supply_get_drvdata(psy);
  910. const struct smb347_charger_platform_data *pdata = smb->pdata;
  911. int ret;
  912. ret = smb347_update_ps_status(smb);
  913. if (ret < 0)
  914. return ret;
  915. switch (prop) {
  916. case POWER_SUPPLY_PROP_STATUS:
  917. ret = smb347_get_charging_status(smb);
  918. if (ret < 0)
  919. return ret;
  920. val->intval = ret;
  921. break;
  922. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  923. if (!smb347_is_ps_online(smb))
  924. return -ENODATA;
  925. /*
  926. * We handle trickle and pre-charging the same, and taper
  927. * and none the same.
  928. */
  929. switch (smb347_charging_status(smb)) {
  930. case 1:
  931. val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  932. break;
  933. case 2:
  934. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  935. break;
  936. default:
  937. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  938. break;
  939. }
  940. break;
  941. case POWER_SUPPLY_PROP_TECHNOLOGY:
  942. val->intval = pdata->battery_info.technology;
  943. break;
  944. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  945. val->intval = pdata->battery_info.voltage_min_design;
  946. break;
  947. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  948. val->intval = pdata->battery_info.voltage_max_design;
  949. break;
  950. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  951. val->intval = pdata->battery_info.charge_full_design;
  952. break;
  953. case POWER_SUPPLY_PROP_MODEL_NAME:
  954. val->strval = pdata->battery_info.name;
  955. break;
  956. default:
  957. return -EINVAL;
  958. }
  959. return 0;
  960. }
  961. static enum power_supply_property smb347_battery_properties[] = {
  962. POWER_SUPPLY_PROP_STATUS,
  963. POWER_SUPPLY_PROP_CHARGE_TYPE,
  964. POWER_SUPPLY_PROP_TECHNOLOGY,
  965. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  966. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  967. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  968. POWER_SUPPLY_PROP_MODEL_NAME,
  969. };
  970. static bool smb347_volatile_reg(struct device *dev, unsigned int reg)
  971. {
  972. switch (reg) {
  973. case IRQSTAT_A:
  974. case IRQSTAT_C:
  975. case IRQSTAT_E:
  976. case IRQSTAT_F:
  977. case STAT_A:
  978. case STAT_B:
  979. case STAT_C:
  980. case STAT_E:
  981. return true;
  982. }
  983. return false;
  984. }
  985. static bool smb347_readable_reg(struct device *dev, unsigned int reg)
  986. {
  987. switch (reg) {
  988. case CFG_CHARGE_CURRENT:
  989. case CFG_CURRENT_LIMIT:
  990. case CFG_FLOAT_VOLTAGE:
  991. case CFG_STAT:
  992. case CFG_PIN:
  993. case CFG_THERM:
  994. case CFG_SYSOK:
  995. case CFG_OTHER:
  996. case CFG_OTG:
  997. case CFG_TEMP_LIMIT:
  998. case CFG_FAULT_IRQ:
  999. case CFG_STATUS_IRQ:
  1000. case CFG_ADDRESS:
  1001. case CMD_A:
  1002. case CMD_B:
  1003. case CMD_C:
  1004. return true;
  1005. }
  1006. return smb347_volatile_reg(dev, reg);
  1007. }
  1008. static const struct regmap_config smb347_regmap = {
  1009. .reg_bits = 8,
  1010. .val_bits = 8,
  1011. .max_register = SMB347_MAX_REGISTER,
  1012. .volatile_reg = smb347_volatile_reg,
  1013. .readable_reg = smb347_readable_reg,
  1014. };
  1015. static const struct power_supply_desc smb347_mains_desc = {
  1016. .name = "smb347-mains",
  1017. .type = POWER_SUPPLY_TYPE_MAINS,
  1018. .get_property = smb347_mains_get_property,
  1019. .properties = smb347_mains_properties,
  1020. .num_properties = ARRAY_SIZE(smb347_mains_properties),
  1021. };
  1022. static const struct power_supply_desc smb347_usb_desc = {
  1023. .name = "smb347-usb",
  1024. .type = POWER_SUPPLY_TYPE_USB,
  1025. .get_property = smb347_usb_get_property,
  1026. .properties = smb347_usb_properties,
  1027. .num_properties = ARRAY_SIZE(smb347_usb_properties),
  1028. };
  1029. static const struct power_supply_desc smb347_battery_desc = {
  1030. .name = "smb347-battery",
  1031. .type = POWER_SUPPLY_TYPE_BATTERY,
  1032. .get_property = smb347_battery_get_property,
  1033. .properties = smb347_battery_properties,
  1034. .num_properties = ARRAY_SIZE(smb347_battery_properties),
  1035. };
  1036. static int smb347_probe(struct i2c_client *client,
  1037. const struct i2c_device_id *id)
  1038. {
  1039. static char *battery[] = { "smb347-battery" };
  1040. const struct smb347_charger_platform_data *pdata;
  1041. struct power_supply_config mains_usb_cfg = {}, battery_cfg = {};
  1042. struct device *dev = &client->dev;
  1043. struct smb347_charger *smb;
  1044. int ret;
  1045. pdata = dev->platform_data;
  1046. if (!pdata)
  1047. return -EINVAL;
  1048. if (!pdata->use_mains && !pdata->use_usb)
  1049. return -EINVAL;
  1050. smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
  1051. if (!smb)
  1052. return -ENOMEM;
  1053. i2c_set_clientdata(client, smb);
  1054. mutex_init(&smb->lock);
  1055. smb->dev = &client->dev;
  1056. smb->pdata = pdata;
  1057. smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
  1058. if (IS_ERR(smb->regmap))
  1059. return PTR_ERR(smb->regmap);
  1060. ret = smb347_hw_init(smb);
  1061. if (ret < 0)
  1062. return ret;
  1063. mains_usb_cfg.supplied_to = battery;
  1064. mains_usb_cfg.num_supplicants = ARRAY_SIZE(battery);
  1065. mains_usb_cfg.drv_data = smb;
  1066. if (smb->pdata->use_mains) {
  1067. smb->mains = power_supply_register(dev, &smb347_mains_desc,
  1068. &mains_usb_cfg);
  1069. if (IS_ERR(smb->mains))
  1070. return PTR_ERR(smb->mains);
  1071. }
  1072. if (smb->pdata->use_usb) {
  1073. smb->usb = power_supply_register(dev, &smb347_usb_desc,
  1074. &mains_usb_cfg);
  1075. if (IS_ERR(smb->usb)) {
  1076. if (smb->pdata->use_mains)
  1077. power_supply_unregister(smb->mains);
  1078. return PTR_ERR(smb->usb);
  1079. }
  1080. }
  1081. battery_cfg.drv_data = smb;
  1082. smb->battery = power_supply_register(dev, &smb347_battery_desc,
  1083. &battery_cfg);
  1084. if (IS_ERR(smb->battery)) {
  1085. if (smb->pdata->use_usb)
  1086. power_supply_unregister(smb->usb);
  1087. if (smb->pdata->use_mains)
  1088. power_supply_unregister(smb->mains);
  1089. return PTR_ERR(smb->battery);
  1090. }
  1091. /*
  1092. * Interrupt pin is optional. If it is connected, we setup the
  1093. * interrupt support here.
  1094. */
  1095. if (pdata->irq_gpio >= 0) {
  1096. ret = smb347_irq_init(smb, client);
  1097. if (ret < 0) {
  1098. dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
  1099. dev_warn(dev, "disabling IRQ support\n");
  1100. } else {
  1101. smb347_irq_enable(smb);
  1102. }
  1103. }
  1104. return 0;
  1105. }
  1106. static int smb347_remove(struct i2c_client *client)
  1107. {
  1108. struct smb347_charger *smb = i2c_get_clientdata(client);
  1109. if (client->irq) {
  1110. smb347_irq_disable(smb);
  1111. free_irq(client->irq, smb);
  1112. gpio_free(smb->pdata->irq_gpio);
  1113. }
  1114. power_supply_unregister(smb->battery);
  1115. if (smb->pdata->use_usb)
  1116. power_supply_unregister(smb->usb);
  1117. if (smb->pdata->use_mains)
  1118. power_supply_unregister(smb->mains);
  1119. return 0;
  1120. }
  1121. static const struct i2c_device_id smb347_id[] = {
  1122. { "smb347", 0 },
  1123. { }
  1124. };
  1125. MODULE_DEVICE_TABLE(i2c, smb347_id);
  1126. static struct i2c_driver smb347_driver = {
  1127. .driver = {
  1128. .name = "smb347",
  1129. },
  1130. .probe = smb347_probe,
  1131. .remove = smb347_remove,
  1132. .id_table = smb347_id,
  1133. };
  1134. module_i2c_driver(smb347_driver);
  1135. MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
  1136. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1137. MODULE_DESCRIPTION("SMB347 battery charger driver");
  1138. MODULE_LICENSE("GPL");