smb347-charger.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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_D:
  976. case IRQSTAT_E:
  977. case IRQSTAT_F:
  978. case STAT_A:
  979. case STAT_B:
  980. case STAT_C:
  981. case STAT_E:
  982. return true;
  983. }
  984. return false;
  985. }
  986. static bool smb347_readable_reg(struct device *dev, unsigned int reg)
  987. {
  988. switch (reg) {
  989. case CFG_CHARGE_CURRENT:
  990. case CFG_CURRENT_LIMIT:
  991. case CFG_FLOAT_VOLTAGE:
  992. case CFG_STAT:
  993. case CFG_PIN:
  994. case CFG_THERM:
  995. case CFG_SYSOK:
  996. case CFG_OTHER:
  997. case CFG_OTG:
  998. case CFG_TEMP_LIMIT:
  999. case CFG_FAULT_IRQ:
  1000. case CFG_STATUS_IRQ:
  1001. case CFG_ADDRESS:
  1002. case CMD_A:
  1003. case CMD_B:
  1004. case CMD_C:
  1005. return true;
  1006. }
  1007. return smb347_volatile_reg(dev, reg);
  1008. }
  1009. static const struct regmap_config smb347_regmap = {
  1010. .reg_bits = 8,
  1011. .val_bits = 8,
  1012. .max_register = SMB347_MAX_REGISTER,
  1013. .volatile_reg = smb347_volatile_reg,
  1014. .readable_reg = smb347_readable_reg,
  1015. };
  1016. static const struct power_supply_desc smb347_mains_desc = {
  1017. .name = "smb347-mains",
  1018. .type = POWER_SUPPLY_TYPE_MAINS,
  1019. .get_property = smb347_mains_get_property,
  1020. .properties = smb347_mains_properties,
  1021. .num_properties = ARRAY_SIZE(smb347_mains_properties),
  1022. };
  1023. static const struct power_supply_desc smb347_usb_desc = {
  1024. .name = "smb347-usb",
  1025. .type = POWER_SUPPLY_TYPE_USB,
  1026. .get_property = smb347_usb_get_property,
  1027. .properties = smb347_usb_properties,
  1028. .num_properties = ARRAY_SIZE(smb347_usb_properties),
  1029. };
  1030. static const struct power_supply_desc smb347_battery_desc = {
  1031. .name = "smb347-battery",
  1032. .type = POWER_SUPPLY_TYPE_BATTERY,
  1033. .get_property = smb347_battery_get_property,
  1034. .properties = smb347_battery_properties,
  1035. .num_properties = ARRAY_SIZE(smb347_battery_properties),
  1036. };
  1037. static int smb347_probe(struct i2c_client *client,
  1038. const struct i2c_device_id *id)
  1039. {
  1040. static char *battery[] = { "smb347-battery" };
  1041. const struct smb347_charger_platform_data *pdata;
  1042. struct power_supply_config mains_usb_cfg = {}, battery_cfg = {};
  1043. struct device *dev = &client->dev;
  1044. struct smb347_charger *smb;
  1045. int ret;
  1046. pdata = dev->platform_data;
  1047. if (!pdata)
  1048. return -EINVAL;
  1049. if (!pdata->use_mains && !pdata->use_usb)
  1050. return -EINVAL;
  1051. smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
  1052. if (!smb)
  1053. return -ENOMEM;
  1054. i2c_set_clientdata(client, smb);
  1055. mutex_init(&smb->lock);
  1056. smb->dev = &client->dev;
  1057. smb->pdata = pdata;
  1058. smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap);
  1059. if (IS_ERR(smb->regmap))
  1060. return PTR_ERR(smb->regmap);
  1061. ret = smb347_hw_init(smb);
  1062. if (ret < 0)
  1063. return ret;
  1064. mains_usb_cfg.supplied_to = battery;
  1065. mains_usb_cfg.num_supplicants = ARRAY_SIZE(battery);
  1066. mains_usb_cfg.drv_data = smb;
  1067. if (smb->pdata->use_mains) {
  1068. smb->mains = power_supply_register(dev, &smb347_mains_desc,
  1069. &mains_usb_cfg);
  1070. if (IS_ERR(smb->mains))
  1071. return PTR_ERR(smb->mains);
  1072. }
  1073. if (smb->pdata->use_usb) {
  1074. smb->usb = power_supply_register(dev, &smb347_usb_desc,
  1075. &mains_usb_cfg);
  1076. if (IS_ERR(smb->usb)) {
  1077. if (smb->pdata->use_mains)
  1078. power_supply_unregister(smb->mains);
  1079. return PTR_ERR(smb->usb);
  1080. }
  1081. }
  1082. battery_cfg.drv_data = smb;
  1083. smb->battery = power_supply_register(dev, &smb347_battery_desc,
  1084. &battery_cfg);
  1085. if (IS_ERR(smb->battery)) {
  1086. if (smb->pdata->use_usb)
  1087. power_supply_unregister(smb->usb);
  1088. if (smb->pdata->use_mains)
  1089. power_supply_unregister(smb->mains);
  1090. return PTR_ERR(smb->battery);
  1091. }
  1092. /*
  1093. * Interrupt pin is optional. If it is connected, we setup the
  1094. * interrupt support here.
  1095. */
  1096. if (pdata->irq_gpio >= 0) {
  1097. ret = smb347_irq_init(smb, client);
  1098. if (ret < 0) {
  1099. dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
  1100. dev_warn(dev, "disabling IRQ support\n");
  1101. } else {
  1102. smb347_irq_enable(smb);
  1103. }
  1104. }
  1105. return 0;
  1106. }
  1107. static int smb347_remove(struct i2c_client *client)
  1108. {
  1109. struct smb347_charger *smb = i2c_get_clientdata(client);
  1110. if (client->irq) {
  1111. smb347_irq_disable(smb);
  1112. free_irq(client->irq, smb);
  1113. gpio_free(smb->pdata->irq_gpio);
  1114. }
  1115. power_supply_unregister(smb->battery);
  1116. if (smb->pdata->use_usb)
  1117. power_supply_unregister(smb->usb);
  1118. if (smb->pdata->use_mains)
  1119. power_supply_unregister(smb->mains);
  1120. return 0;
  1121. }
  1122. static const struct i2c_device_id smb347_id[] = {
  1123. { "smb347", 0 },
  1124. { }
  1125. };
  1126. MODULE_DEVICE_TABLE(i2c, smb347_id);
  1127. static struct i2c_driver smb347_driver = {
  1128. .driver = {
  1129. .name = "smb347",
  1130. },
  1131. .probe = smb347_probe,
  1132. .remove = smb347_remove,
  1133. .id_table = smb347_id,
  1134. };
  1135. module_i2c_driver(smb347_driver);
  1136. MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
  1137. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1138. MODULE_DESCRIPTION("SMB347 battery charger driver");
  1139. MODULE_LICENSE("GPL");