smb347-charger.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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/debugfs.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/seq_file.h>
  24. #include <linux/delay.h>
  25. /*
  26. * Configuration registers. These are mirrored to volatile RAM and can be
  27. * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be
  28. * reloaded from non-volatile registers after POR.
  29. */
  30. #define CFG_CHARGE_CURRENT 0x00
  31. #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0
  32. #define CFG_CHARGE_CURRENT_FCC_SHIFT 5
  33. #define CFG_CHARGE_CURRENT_PCC_MASK 0x18
  34. #define CFG_CHARGE_CURRENT_PCC_SHIFT 3
  35. #define CFG_CHARGE_CURRENT_TC_MASK 0x07
  36. #define CFG_CURRENT_LIMIT 0x01
  37. #define CFG_CURRENT_LIMIT_DC_MASK 0xf0
  38. #define CFG_CURRENT_LIMIT_DC_SHIFT 4
  39. #define CFG_CURRENT_LIMIT_USB_MASK 0x0f
  40. #define CFG_VARIOUS_FUNCTION 0x02
  41. #define CFG_INPUT_SOURCE_PRIORITY BIT(2)
  42. #define CFG_FLOAT_VOLTAGE 0x03
  43. #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0
  44. #define CFG_FLOAT_VOLTAGE_MASK 0x3F
  45. #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6
  46. #define CFG_CHARGE_CONTROL 0x04
  47. #define CFG_AUTOMATIC_RECHARGE_DISABLE BIT(7)
  48. #define CFG_STAT 0x05
  49. #define CFG_STAT_DISABLED BIT(5)
  50. #define CFG_STAT_ACTIVE_HIGH BIT(7)
  51. #define CFG_PIN 0x06
  52. #define CFG_PIN_EN_CTRL_MASK 0x60
  53. #define CFG_PIN_USB_MODE_CTRL BIT(4)
  54. #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40
  55. #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60
  56. #define CFG_PIN_EN_APSD_IRQ BIT(1)
  57. #define CFG_PIN_EN_CHARGER_ERROR BIT(2)
  58. #define CFG_THERM 0x07
  59. #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03
  60. #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0
  61. #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c
  62. #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2
  63. #define CFG_THERM_MONITOR_DISABLED BIT(4)
  64. #define CFG_SYSOK 0x08
  65. #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2)
  66. #define CFG_OTHER 0x09
  67. #define CFG_OTHER_RID_MASK 0xc0
  68. #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0
  69. #define CFG_OTG 0x0a
  70. #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30
  71. #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4
  72. #define CFG_OTG_CC_COMPENSATION_MASK 0xc0
  73. #define CFG_OTG_CC_COMPENSATION_SHIFT 6
  74. #define CFG_TEMP_LIMIT 0x0b
  75. #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03
  76. #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0
  77. #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c
  78. #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2
  79. #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30
  80. #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4
  81. #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0
  82. #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6
  83. #define CFG_FAULT_IRQ 0x0c
  84. #define CFG_FAULT_IRQ_DCIN_UV BIT(2)
  85. #define CFG_STATUS_IRQ 0x0d
  86. #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4)
  87. #define CFG_ADDRESS 0x0e
  88. /* Command registers */
  89. #define CMD_A 0x30
  90. #define CMD_A_CHG_ENABLED BIT(1)
  91. #define CMD_A_SUSPEND_ENABLED BIT(2)
  92. #define CMD_A_OTG_ENABLE BIT(4)
  93. #define CMD_A_ALLOW_WRITE BIT(7)
  94. #define CMD_B 0x31
  95. #define CMD_B_POR BIT(7)
  96. #define CMD_B_USB59_MODE BIT(1)
  97. #define CMD_B_HC_MODE BIT(0)
  98. #define CMD_C 0x33
  99. /* Interrupt Status registers */
  100. #define IRQSTAT_A 0x35
  101. #define IRQSTAT_C 0x37
  102. #define IRQSTAT_C_TERMINATION_STAT BIT(0)
  103. #define IRQSTAT_C_TERMINATION_IRQ BIT(1)
  104. #define IRQSTAT_C_TAPER_IRQ BIT(3)
  105. #define IRQSTAT_E 0x39
  106. #define IRQSTAT_E_USBIN_UV_STAT BIT(0)
  107. #define IRQSTAT_E_USBIN_UV_IRQ BIT(1)
  108. #define IRQSTAT_E_DCIN_UV_STAT BIT(4)
  109. #define IRQSTAT_E_DCIN_UV_IRQ BIT(5)
  110. #define IRQSTAT_F 0x3a
  111. /* Status registers */
  112. #define STAT_A 0x3b
  113. #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f
  114. #define STAT_B 0x3c
  115. #define STAT_C 0x3d
  116. #define STAT_C_CHG_ENABLED BIT(0)
  117. #define STAT_C_CHG_STATUS BIT(5)
  118. #define STAT_C_CHG_MASK 0x06
  119. #define STAT_C_CHG_SHIFT 1
  120. #define STAT_C_CHARGER_ERROR BIT(6)
  121. #define STAT_E 0x3f
  122. /**
  123. * struct smb347_charger - smb347 charger instance
  124. * @lock: protects concurrent access to online variables
  125. * @client: pointer to i2c client
  126. * @mains: power_supply instance for AC/DC power
  127. * @usb: power_supply instance for USB power
  128. * @battery: power_supply instance for battery
  129. * @mains_online: is AC/DC input connected
  130. * @usb_online: is USB input connected
  131. * @charging_enabled: is charging enabled
  132. * @dentry: for debugfs
  133. * @pdata: pointer to platform data
  134. */
  135. struct smb347_charger {
  136. struct mutex lock;
  137. struct i2c_client *client;
  138. struct power_supply mains;
  139. struct power_supply usb;
  140. struct power_supply battery;
  141. bool mains_online;
  142. bool usb_online;
  143. bool charging_enabled;
  144. unsigned int mains_current_limit;
  145. bool usb_hc_mode;
  146. bool usb_otg_enabled;
  147. bool is_fully_charged;
  148. int en_gpio;
  149. struct dentry *dentry;
  150. const struct smb347_charger_platform_data *pdata;
  151. };
  152. /* Fast charge current in uA */
  153. static const unsigned int fcc_tbl[] = {
  154. 700000,
  155. 900000,
  156. 1200000,
  157. 1500000,
  158. 1800000,
  159. 2000000,
  160. 2200000,
  161. 2500000,
  162. };
  163. /* Pre-charge current in uA */
  164. static const unsigned int pcc_tbl[] = {
  165. 100000,
  166. 150000,
  167. 200000,
  168. 250000,
  169. };
  170. /* Termination current in uA */
  171. static const unsigned int tc_tbl[] = {
  172. 37500,
  173. 50000,
  174. 100000,
  175. 150000,
  176. 200000,
  177. 250000,
  178. 500000,
  179. 600000,
  180. };
  181. /* Input current limit in uA */
  182. static const unsigned int icl_tbl[] = {
  183. 300000,
  184. 500000,
  185. 700000,
  186. 900000,
  187. 1200000,
  188. 1500000,
  189. 1800000,
  190. 2000000,
  191. 2200000,
  192. 2500000,
  193. };
  194. /* Charge current compensation in uA */
  195. static const unsigned int ccc_tbl[] = {
  196. 250000,
  197. 700000,
  198. 900000,
  199. 1200000,
  200. };
  201. /* Convert register value to current using lookup table */
  202. static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val)
  203. {
  204. if (val >= size)
  205. return -EINVAL;
  206. return tbl[val];
  207. }
  208. /* Convert current to register value using lookup table */
  209. static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val)
  210. {
  211. size_t i;
  212. for (i = 0; i < size; i++)
  213. if (val < tbl[i])
  214. break;
  215. return i > 0 ? i - 1 : -EINVAL;
  216. }
  217. static int smb347_read(struct smb347_charger *smb, u8 reg)
  218. {
  219. int ret;
  220. ret = i2c_smbus_read_byte_data(smb->client, reg);
  221. if (ret < 0)
  222. dev_warn(&smb->client->dev, "failed to read reg 0x%x: %d\n",
  223. reg, ret);
  224. return ret;
  225. }
  226. static int smb347_write(struct smb347_charger *smb, u8 reg, u8 val)
  227. {
  228. int ret;
  229. ret = i2c_smbus_write_byte_data(smb->client, reg, val);
  230. if (ret < 0)
  231. dev_warn(&smb->client->dev, "failed to write reg 0x%x: %d\n",
  232. reg, ret);
  233. return ret;
  234. }
  235. /**
  236. * smb347_update_status - updates the charging status
  237. * @smb: pointer to smb347 charger instance
  238. *
  239. * Function checks status of the charging and updates internal state
  240. * accordingly. Returns %0 if there is no change in status, %1 if the
  241. * status has changed and negative errno in case of failure.
  242. */
  243. static int smb347_update_status(struct smb347_charger *smb)
  244. {
  245. bool usb = false;
  246. bool dc = false;
  247. int ret;
  248. ret = smb347_read(smb, IRQSTAT_E);
  249. if (ret < 0)
  250. return ret;
  251. /*
  252. * Dc and usb are set depending on whether they are enabled in
  253. * platform data _and_ whether corresponding undervoltage is set.
  254. */
  255. if (smb->pdata->use_mains)
  256. dc = !(ret & IRQSTAT_E_DCIN_UV_STAT);
  257. if (smb->pdata->use_usb)
  258. usb = !(ret & IRQSTAT_E_USBIN_UV_STAT);
  259. mutex_lock(&smb->lock);
  260. ret = smb->mains_online != dc || smb->usb_online != usb;
  261. smb->mains_online = dc;
  262. smb->usb_online = usb;
  263. mutex_unlock(&smb->lock);
  264. return ret;
  265. }
  266. /*
  267. * smb347_is_online - returns whether input power source is connected
  268. * @smb: pointer to smb347 charger instance
  269. *
  270. * Returns %true if input power source is connected. Note that this is
  271. * dependent on what platform has configured for usable power sources. For
  272. * example if USB is disabled, this will return %false even if the USB
  273. * cable is connected.
  274. */
  275. static bool smb347_is_online(struct smb347_charger *smb)
  276. {
  277. bool ret;
  278. mutex_lock(&smb->lock);
  279. ret = smb->usb_online || smb->mains_online;
  280. mutex_unlock(&smb->lock);
  281. return ret;
  282. }
  283. /**
  284. * smb347_charging_status - returns status of charging
  285. * @smb: pointer to smb347 charger instance
  286. *
  287. * Function returns charging status. %0 means no charging is in progress,
  288. * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
  289. */
  290. static int smb347_charging_status(struct smb347_charger *smb)
  291. {
  292. int ret;
  293. if (!smb347_is_online(smb))
  294. return 0;
  295. ret = smb347_read(smb, STAT_C);
  296. if (ret < 0)
  297. return 0;
  298. return (ret & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT;
  299. }
  300. static int smb347_charging_set(struct smb347_charger *smb, bool enable)
  301. {
  302. int ret = 0;
  303. if (enable && !smb->charging_enabled)
  304. smb->is_fully_charged = false;
  305. if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) {
  306. smb->charging_enabled = enable;
  307. if (smb->en_gpio)
  308. gpio_set_value(
  309. smb->en_gpio,
  310. (smb->pdata->enable_control ==
  311. SMB347_CHG_ENABLE_PIN_ACTIVE_LOW) ^ enable);
  312. return 0;
  313. }
  314. mutex_lock(&smb->lock);
  315. if (smb->charging_enabled != enable) {
  316. ret = smb347_read(smb, CMD_A);
  317. if (ret < 0)
  318. goto out;
  319. smb->charging_enabled = enable;
  320. if (enable)
  321. ret |= CMD_A_CHG_ENABLED;
  322. else
  323. ret &= ~CMD_A_CHG_ENABLED;
  324. ret = smb347_write(smb, CMD_A, ret);
  325. }
  326. out:
  327. mutex_unlock(&smb->lock);
  328. return ret;
  329. }
  330. static inline int smb347_charging_enable(struct smb347_charger *smb)
  331. {
  332. return smb347_charging_set(smb, true);
  333. }
  334. static inline int smb347_charging_disable(struct smb347_charger *smb)
  335. {
  336. return smb347_charging_set(smb, false);
  337. }
  338. static int smb347_update_online(struct smb347_charger *smb)
  339. {
  340. int ret;
  341. /*
  342. * Depending on whether valid power source is connected or not, we
  343. * disable or enable the charging. We do it manually because it
  344. * depends on how the platform has configured the valid inputs.
  345. */
  346. if (smb347_is_online(smb)) {
  347. ret = smb347_charging_enable(smb);
  348. if (ret < 0)
  349. dev_err(&smb->client->dev,
  350. "failed to enable charging\n");
  351. } else {
  352. ret = smb347_charging_disable(smb);
  353. if (ret < 0)
  354. dev_err(&smb->client->dev,
  355. "failed to disable charging\n");
  356. }
  357. return ret;
  358. }
  359. static int smb347_set_charge_current(struct smb347_charger *smb)
  360. {
  361. int ret, val;
  362. ret = smb347_read(smb, CFG_CHARGE_CURRENT);
  363. if (ret < 0)
  364. return ret;
  365. if (smb->pdata->max_charge_current) {
  366. val = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl),
  367. smb->pdata->max_charge_current);
  368. if (val < 0)
  369. return val;
  370. ret &= ~CFG_CHARGE_CURRENT_FCC_MASK;
  371. ret |= val << CFG_CHARGE_CURRENT_FCC_SHIFT;
  372. }
  373. if (smb->pdata->pre_charge_current) {
  374. val = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl),
  375. smb->pdata->pre_charge_current);
  376. if (val < 0)
  377. return val;
  378. ret &= ~CFG_CHARGE_CURRENT_PCC_MASK;
  379. ret |= val << CFG_CHARGE_CURRENT_PCC_SHIFT;
  380. }
  381. if (smb->pdata->termination_current) {
  382. val = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl),
  383. smb->pdata->termination_current);
  384. if (val < 0)
  385. return val;
  386. ret &= ~CFG_CHARGE_CURRENT_TC_MASK;
  387. ret |= val;
  388. }
  389. return smb347_write(smb, CFG_CHARGE_CURRENT, ret);
  390. }
  391. static int smb347_set_current_limits(struct smb347_charger *smb)
  392. {
  393. int ret, val;
  394. ret = smb347_read(smb, CFG_CURRENT_LIMIT);
  395. if (ret < 0)
  396. return ret;
  397. if (smb->mains_current_limit) {
  398. val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
  399. smb->mains_current_limit);
  400. if (val < 0)
  401. return val;
  402. ret &= ~CFG_CURRENT_LIMIT_DC_MASK;
  403. ret |= val << CFG_CURRENT_LIMIT_DC_SHIFT;
  404. }
  405. if (smb->pdata->usb_hc_current_limit) {
  406. val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl),
  407. smb->pdata->usb_hc_current_limit);
  408. if (val < 0)
  409. return val;
  410. ret &= ~CFG_CURRENT_LIMIT_USB_MASK;
  411. ret |= val;
  412. }
  413. return smb347_write(smb, CFG_CURRENT_LIMIT, ret);
  414. }
  415. static int smb347_set_voltage_limits(struct smb347_charger *smb)
  416. {
  417. int ret, val;
  418. ret = smb347_read(smb, CFG_FLOAT_VOLTAGE);
  419. if (ret < 0)
  420. return ret;
  421. if (smb->pdata->pre_to_fast_voltage) {
  422. val = smb->pdata->pre_to_fast_voltage;
  423. /* uV */
  424. val = clamp_val(val, 2400000, 3000000) - 2400000;
  425. val /= 200000;
  426. ret &= ~CFG_FLOAT_VOLTAGE_THRESHOLD_MASK;
  427. ret |= val << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT;
  428. }
  429. if (smb->pdata->max_charge_voltage) {
  430. val = smb->pdata->max_charge_voltage;
  431. /* uV */
  432. val = clamp_val(val, 3500000, 4500000) - 3500000;
  433. val /= 20000;
  434. ret &= ~CFG_FLOAT_VOLTAGE_MASK;
  435. ret |= val;
  436. }
  437. return smb347_write(smb, CFG_FLOAT_VOLTAGE, ret);
  438. }
  439. static int smb347_set_temp_limits(struct smb347_charger *smb)
  440. {
  441. bool enable_therm_monitor = false;
  442. int ret, val;
  443. if (smb->pdata->chip_temp_threshold) {
  444. val = smb->pdata->chip_temp_threshold;
  445. /* degree C */
  446. val = clamp_val(val, 100, 130) - 100;
  447. val /= 10;
  448. ret = smb347_read(smb, CFG_OTG);
  449. if (ret < 0)
  450. return ret;
  451. ret &= ~CFG_OTG_TEMP_THRESHOLD_MASK;
  452. ret |= val << CFG_OTG_TEMP_THRESHOLD_SHIFT;
  453. ret = smb347_write(smb, CFG_OTG, ret);
  454. if (ret < 0)
  455. return ret;
  456. }
  457. ret = smb347_read(smb, CFG_TEMP_LIMIT);
  458. if (ret < 0)
  459. return ret;
  460. if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  461. val = smb->pdata->soft_cold_temp_limit;
  462. val = clamp_val(val, 0, 15);
  463. val /= 5;
  464. /* this goes from higher to lower so invert the value */
  465. val = ~val & 0x3;
  466. ret &= ~CFG_TEMP_LIMIT_SOFT_COLD_MASK;
  467. ret |= val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT;
  468. enable_therm_monitor = true;
  469. }
  470. if (smb->pdata->soft_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  471. val = smb->pdata->soft_hot_temp_limit;
  472. val = clamp_val(val, 40, 55) - 40;
  473. val /= 5;
  474. ret &= ~CFG_TEMP_LIMIT_SOFT_HOT_MASK;
  475. ret |= val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT;
  476. enable_therm_monitor = true;
  477. }
  478. if (smb->pdata->hard_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  479. val = smb->pdata->hard_cold_temp_limit;
  480. val = clamp_val(val, -5, 10) + 5;
  481. val /= 5;
  482. /* this goes from higher to lower so invert the value */
  483. val = ~val & 0x3;
  484. ret &= ~CFG_TEMP_LIMIT_HARD_COLD_MASK;
  485. ret |= val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT;
  486. enable_therm_monitor = true;
  487. }
  488. if (smb->pdata->hard_hot_temp_limit != SMB347_TEMP_USE_DEFAULT) {
  489. val = smb->pdata->hard_hot_temp_limit;
  490. val = clamp_val(val, 50, 65) - 50;
  491. val /= 5;
  492. ret &= ~CFG_TEMP_LIMIT_HARD_HOT_MASK;
  493. ret |= val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT;
  494. enable_therm_monitor = true;
  495. }
  496. ret = smb347_write(smb, CFG_TEMP_LIMIT, ret);
  497. if (ret < 0)
  498. return ret;
  499. /*
  500. * If any of the temperature limits are set, we also enable the
  501. * thermistor monitoring.
  502. *
  503. * When soft limits are hit, the device will start to compensate
  504. * current and/or voltage depending on the configuration.
  505. *
  506. * When hard limit is hit, the device will suspend charging
  507. * depending on the configuration.
  508. */
  509. if (enable_therm_monitor) {
  510. ret = smb347_read(smb, CFG_THERM);
  511. if (ret < 0)
  512. return ret;
  513. ret &= ~CFG_THERM_MONITOR_DISABLED;
  514. ret = smb347_write(smb, CFG_THERM, ret);
  515. if (ret < 0)
  516. return ret;
  517. }
  518. if (smb->pdata->suspend_on_hard_temp_limit) {
  519. ret = smb347_read(smb, CFG_SYSOK);
  520. if (ret < 0)
  521. return ret;
  522. ret &= ~CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED;
  523. ret = smb347_write(smb, CFG_SYSOK, ret);
  524. if (ret < 0)
  525. return ret;
  526. }
  527. if (smb->pdata->soft_temp_limit_compensation !=
  528. SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) {
  529. val = smb->pdata->soft_temp_limit_compensation & 0x3;
  530. ret = smb347_read(smb, CFG_THERM);
  531. if (ret < 0)
  532. return ret;
  533. ret &= ~CFG_THERM_SOFT_HOT_COMPENSATION_MASK;
  534. ret |= val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT;
  535. ret &= ~CFG_THERM_SOFT_COLD_COMPENSATION_MASK;
  536. ret |= val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT;
  537. ret = smb347_write(smb, CFG_THERM, ret);
  538. if (ret < 0)
  539. return ret;
  540. }
  541. if (smb->pdata->charge_current_compensation) {
  542. val = current_to_hw(ccc_tbl, ARRAY_SIZE(ccc_tbl),
  543. smb->pdata->charge_current_compensation);
  544. if (val < 0)
  545. return val;
  546. ret = smb347_read(smb, CFG_OTG);
  547. if (ret < 0)
  548. return ret;
  549. ret &= ~CFG_OTG_CC_COMPENSATION_MASK;
  550. ret |= (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT;
  551. ret = smb347_write(smb, CFG_OTG, ret);
  552. if (ret < 0)
  553. return ret;
  554. }
  555. return ret;
  556. }
  557. /*
  558. * smb347_set_writable - enables/disables writing to non-volatile registers
  559. * @smb: pointer to smb347 charger instance
  560. *
  561. * You can enable/disable writing to the non-volatile configuration
  562. * registers by calling this function.
  563. *
  564. * Returns %0 on success and negative errno in case of failure.
  565. */
  566. static int smb347_set_writable(struct smb347_charger *smb, bool writable)
  567. {
  568. int ret;
  569. ret = smb347_read(smb, CMD_A);
  570. if (ret < 0)
  571. return ret;
  572. if (writable)
  573. ret |= CMD_A_ALLOW_WRITE;
  574. else
  575. ret &= ~CMD_A_ALLOW_WRITE;
  576. return smb347_write(smb, CMD_A, ret);
  577. }
  578. static int smb347_irq_set(struct smb347_charger *smb, bool enable)
  579. {
  580. int ret;
  581. ret = smb347_set_writable(smb, true);
  582. if (ret < 0)
  583. return ret;
  584. /*
  585. * Enable/disable interrupts for:
  586. * - under voltage
  587. * - termination current reached
  588. * - charger error
  589. */
  590. if (enable) {
  591. ret = smb347_write(smb, CFG_FAULT_IRQ, CFG_FAULT_IRQ_DCIN_UV);
  592. if (ret < 0)
  593. goto fail;
  594. ret = smb347_write(smb, CFG_STATUS_IRQ,
  595. CFG_STATUS_IRQ_TERMINATION_OR_TAPER);
  596. if (ret < 0)
  597. goto fail;
  598. ret = smb347_read(smb, CFG_PIN);
  599. if (ret < 0)
  600. goto fail;
  601. ret |= CFG_PIN_EN_CHARGER_ERROR;
  602. ret = smb347_write(smb, CFG_PIN, ret);
  603. } else {
  604. ret = smb347_write(smb, CFG_FAULT_IRQ, 0);
  605. if (ret < 0)
  606. goto fail;
  607. ret = smb347_write(smb, CFG_STATUS_IRQ, 0);
  608. if (ret < 0)
  609. goto fail;
  610. ret = smb347_read(smb, CFG_PIN);
  611. if (ret < 0)
  612. goto fail;
  613. ret &= ~CFG_PIN_EN_CHARGER_ERROR;
  614. ret = smb347_write(smb, CFG_PIN, ret);
  615. }
  616. fail:
  617. smb347_set_writable(smb, false);
  618. return ret;
  619. }
  620. static inline int smb347_irq_enable(struct smb347_charger *smb)
  621. {
  622. return smb347_irq_set(smb, true);
  623. }
  624. static inline int smb347_irq_disable(struct smb347_charger *smb)
  625. {
  626. return smb347_irq_set(smb, false);
  627. }
  628. static irqreturn_t smb347_interrupt(int irq, void *data)
  629. {
  630. struct smb347_charger *smb = data;
  631. int stat_c, t;
  632. u8 irqstat[6];
  633. irqreturn_t ret = IRQ_NONE;
  634. t = i2c_smbus_read_i2c_block_data(smb->client, IRQSTAT_A, 6, irqstat);
  635. if (t < 0) {
  636. dev_warn(&smb->client->dev,
  637. "reading IRQSTAT registers failed\n");
  638. return IRQ_NONE;
  639. }
  640. stat_c = smb347_read(smb, STAT_C);
  641. if (stat_c < 0) {
  642. dev_warn(&smb->client->dev, "reading STAT_C failed\n");
  643. return IRQ_NONE;
  644. }
  645. pr_debug("%s: stat c=%x irq a=%x b=%x c=%x d=%x e=%x f=%x\n",
  646. __func__, stat_c, irqstat[0], irqstat[1], irqstat[2],
  647. irqstat[3], irqstat[4], irqstat[5]);
  648. /*
  649. * If we get charger error we report the error back to user and
  650. * disable charging.
  651. */
  652. if (stat_c & STAT_C_CHARGER_ERROR) {
  653. dev_err(&smb->client->dev,
  654. "error in charger, disabling charging\n");
  655. smb347_charging_disable(smb);
  656. power_supply_changed(&smb->battery);
  657. ret = IRQ_HANDLED;
  658. } else if (((stat_c & STAT_C_CHG_STATUS) ||
  659. (irqstat[2] & (IRQSTAT_C_TERMINATION_IRQ |
  660. IRQSTAT_C_TERMINATION_STAT))) &&
  661. !smb->is_fully_charged) {
  662. dev_info(&smb->client->dev, "charge terminated\n");
  663. smb->is_fully_charged = true;
  664. smb347_charging_disable(smb);
  665. power_supply_changed(&smb->battery);
  666. ret = IRQ_HANDLED;
  667. }
  668. if (irqstat[2] & IRQSTAT_C_TAPER_IRQ)
  669. ret = IRQ_HANDLED;
  670. /*
  671. * If we got an under voltage interrupt it means that AC/USB input
  672. * was disconnected.
  673. */
  674. if (irqstat[4] & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ))
  675. ret = IRQ_HANDLED;
  676. if (smb347_update_status(smb) > 0) {
  677. smb347_update_online(smb);
  678. power_supply_changed(&smb->mains);
  679. power_supply_changed(&smb->usb);
  680. ret = IRQ_HANDLED;
  681. }
  682. return ret;
  683. }
  684. static int smb347_irq_init(struct smb347_charger *smb)
  685. {
  686. const struct smb347_charger_platform_data *pdata = smb->pdata;
  687. int ret, irq = gpio_to_irq(pdata->irq_gpio);
  688. ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, smb->client->name);
  689. if (ret < 0)
  690. goto fail;
  691. ret = request_threaded_irq(irq, NULL, smb347_interrupt,
  692. pdata->disable_stat_interrupts ?
  693. IRQF_TRIGGER_RISING | IRQF_ONESHOT :
  694. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  695. smb->client->name, smb);
  696. if (ret < 0)
  697. goto fail_gpio;
  698. ret = enable_irq_wake(irq);
  699. if (ret)
  700. pr_err("%s: failed to enable wake on irq %d\n", __func__, irq);
  701. smb->client->irq = irq;
  702. return 0;
  703. fail_gpio:
  704. gpio_free(pdata->irq_gpio);
  705. fail:
  706. smb->client->irq = 0;
  707. return ret;
  708. }
  709. static int smb347_hw_init(struct smb347_charger *smb)
  710. {
  711. int ret;
  712. ret = smb347_set_writable(smb, true);
  713. if (ret < 0)
  714. return ret;
  715. /*
  716. * Program the platform specific configuration values to the device
  717. * first.
  718. */
  719. ret = smb347_set_charge_current(smb);
  720. if (ret < 0)
  721. goto fail;
  722. ret = smb347_set_current_limits(smb);
  723. if (ret < 0)
  724. goto fail;
  725. ret = smb347_set_voltage_limits(smb);
  726. if (ret < 0)
  727. goto fail;
  728. // HACK for Manta pre-alpha 0.2, TH_BATTERY not connected properly
  729. #if 0 // HACK
  730. ret = smb347_set_temp_limits(smb);
  731. if (ret < 0)
  732. goto fail;
  733. #endif // HACK
  734. /* If USB charging is disabled we put the USB in suspend mode */
  735. if (!smb->pdata->use_usb) {
  736. ret = smb347_read(smb, CMD_A);
  737. if (ret < 0)
  738. goto fail;
  739. ret |= CMD_A_SUSPEND_ENABLED;
  740. ret = smb347_write(smb, CMD_A, ret);
  741. if (ret < 0)
  742. goto fail;
  743. }
  744. ret = smb347_read(smb, CFG_OTHER);
  745. if (ret < 0)
  746. goto fail;
  747. /*
  748. * If configured by platform data, we enable hardware Auto-OTG
  749. * support for driving VBUS. Otherwise we disable it.
  750. */
  751. ret &= ~CFG_OTHER_RID_MASK;
  752. if (smb->pdata->use_usb_otg)
  753. ret |= CFG_OTHER_RID_ENABLED_AUTO_OTG;
  754. ret = smb347_write(smb, CFG_OTHER, ret);
  755. if (ret < 0)
  756. goto fail;
  757. /* If configured by platform data, disable AUTOMATIC RECHARGE */
  758. if (smb->pdata->disable_automatic_recharge) {
  759. ret = smb347_read(smb, CFG_CHARGE_CONTROL);
  760. if (ret < 0)
  761. goto fail;
  762. ret |= CFG_AUTOMATIC_RECHARGE_DISABLE;
  763. ret = smb347_write(smb, CFG_CHARGE_CONTROL, ret);
  764. if (ret < 0)
  765. goto fail;
  766. }
  767. ret = smb347_read(smb, CFG_PIN);
  768. if (ret < 0)
  769. goto fail;
  770. /*
  771. * Make the charging functionality controllable by a write to the
  772. * command register unless pin control is specified in the platform
  773. * data.
  774. */
  775. ret &= ~(CFG_PIN_EN_CTRL_MASK | CFG_PIN_USB_MODE_CTRL);
  776. switch (smb->pdata->enable_control) {
  777. case SMB347_CHG_ENABLE_SW:
  778. /* Do nothing, 0 means i2c control */
  779. break;
  780. case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW:
  781. ret |= CFG_PIN_EN_CTRL_ACTIVE_LOW;
  782. break;
  783. case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH:
  784. ret |= CFG_PIN_EN_CTRL_ACTIVE_HIGH;
  785. break;
  786. }
  787. if (smb->pdata->usb_mode_pin_ctrl)
  788. ret |= CFG_PIN_USB_MODE_CTRL;
  789. /* Disable Automatic Power Source Detection (APSD) interrupt. */
  790. ret &= ~CFG_PIN_EN_APSD_IRQ;
  791. ret = smb347_write(smb, CFG_PIN, ret);
  792. if (ret < 0)
  793. goto fail;
  794. ret = smb347_update_status(smb);
  795. if (ret < 0)
  796. goto fail;
  797. ret = smb347_update_online(smb);
  798. if ((smb->pdata->irq_gpio >= 0) &&
  799. !smb->pdata->disable_stat_interrupts) {
  800. /*
  801. * Configure the STAT output to be suitable for interrupts:
  802. * disable all other output (except interrupts) and make it
  803. * active low.
  804. */
  805. ret = smb347_read(smb, CFG_STAT);
  806. if (ret < 0)
  807. goto fail;
  808. ret &= ~CFG_STAT_ACTIVE_HIGH;
  809. ret |= CFG_STAT_DISABLED;
  810. ret = smb347_write(smb, CFG_STAT, ret);
  811. if (ret < 0)
  812. goto fail;
  813. ret = smb347_irq_enable(smb);
  814. if (ret < 0)
  815. goto fail;
  816. }
  817. fail:
  818. smb347_set_writable(smb, false);
  819. return ret;
  820. }
  821. static int smb347_mains_get_property(struct power_supply *psy,
  822. enum power_supply_property prop,
  823. union power_supply_propval *val)
  824. {
  825. struct smb347_charger *smb =
  826. container_of(psy, struct smb347_charger, mains);
  827. switch (prop) {
  828. case POWER_SUPPLY_PROP_ONLINE:
  829. val->intval = smb->mains_online;
  830. return 0;
  831. case POWER_SUPPLY_PROP_CURRENT_MAX:
  832. val->intval = smb->mains_current_limit;
  833. return 0;
  834. default:
  835. return -EINVAL;
  836. }
  837. return -EINVAL;
  838. }
  839. static int smb347_mains_set_property(struct power_supply *psy,
  840. enum power_supply_property prop,
  841. const union power_supply_propval *val)
  842. {
  843. struct smb347_charger *smb =
  844. container_of(psy, struct smb347_charger, mains);
  845. int ret;
  846. bool oldval;
  847. switch (prop) {
  848. case POWER_SUPPLY_PROP_ONLINE:
  849. oldval = smb->mains_online;
  850. smb->mains_online = val->intval;
  851. smb347_set_writable(smb, true);
  852. ret = smb347_read(smb, CMD_A);
  853. if (ret < 0)
  854. return -EINVAL;
  855. ret &= ~CMD_A_SUSPEND_ENABLED;
  856. if (val->intval)
  857. ret |= CMD_A_SUSPEND_ENABLED;
  858. ret = smb347_write(smb, CMD_A, ret);
  859. smb347_hw_init(smb);
  860. smb347_set_writable(smb, false);
  861. if (smb->mains_online != oldval)
  862. power_supply_changed(psy);
  863. return 0;
  864. case POWER_SUPPLY_PROP_CURRENT_MAX:
  865. smb->mains_current_limit = val->intval;
  866. smb347_hw_init(smb);
  867. return 0;
  868. default:
  869. return -EINVAL;
  870. }
  871. return -EINVAL;
  872. }
  873. static int smb347_mains_property_is_writeable(struct power_supply *psy,
  874. enum power_supply_property prop)
  875. {
  876. switch (prop) {
  877. case POWER_SUPPLY_PROP_CURRENT_MAX:
  878. return 1;
  879. default:
  880. break;
  881. }
  882. return 0;
  883. }
  884. static enum power_supply_property smb347_mains_properties[] = {
  885. POWER_SUPPLY_PROP_ONLINE,
  886. POWER_SUPPLY_PROP_CURRENT_MAX,
  887. };
  888. static int smb347_usb_get_property(struct power_supply *psy,
  889. enum power_supply_property prop,
  890. union power_supply_propval *val)
  891. {
  892. struct smb347_charger *smb =
  893. container_of(psy, struct smb347_charger, usb);
  894. switch (prop) {
  895. case POWER_SUPPLY_PROP_ONLINE:
  896. val->intval = smb->usb_online;
  897. return 0;
  898. case POWER_SUPPLY_PROP_USB_HC:
  899. val->intval = smb->usb_hc_mode;
  900. return 0;
  901. case POWER_SUPPLY_PROP_USB_OTG:
  902. val->intval = smb->usb_otg_enabled;
  903. return 0;
  904. default:
  905. break;
  906. }
  907. return -EINVAL;
  908. }
  909. static int smb347_usb_set_property(struct power_supply *psy,
  910. enum power_supply_property prop,
  911. const union power_supply_propval *val)
  912. {
  913. int ret = -EINVAL;
  914. struct smb347_charger *smb =
  915. container_of(psy, struct smb347_charger, usb);
  916. bool oldval;
  917. switch (prop) {
  918. case POWER_SUPPLY_PROP_ONLINE:
  919. oldval = smb->usb_online;
  920. smb->usb_online = val->intval;
  921. if (smb->usb_online != oldval)
  922. power_supply_changed(psy);
  923. ret = 0;
  924. break;
  925. case POWER_SUPPLY_PROP_USB_HC:
  926. smb347_set_writable(smb, true);
  927. ret = smb347_write(smb, CMD_B, val->intval ?
  928. CMD_B_HC_MODE : CMD_B_USB59_MODE);
  929. smb347_set_writable(smb, false);
  930. smb->usb_hc_mode = val->intval;
  931. break;
  932. case POWER_SUPPLY_PROP_USB_OTG:
  933. ret = smb347_read(smb, CMD_A);
  934. if (ret < 0)
  935. return ret;
  936. if (val->intval)
  937. ret |= CMD_A_OTG_ENABLE;
  938. else
  939. ret &= ~CMD_A_OTG_ENABLE;
  940. ret = smb347_write(smb, CMD_A, ret);
  941. if (ret >= 0)
  942. smb->usb_otg_enabled = val->intval;
  943. break;
  944. default:
  945. break;
  946. }
  947. return ret;
  948. }
  949. static int smb347_usb_property_is_writeable(struct power_supply *psy,
  950. enum power_supply_property prop)
  951. {
  952. switch (prop) {
  953. case POWER_SUPPLY_PROP_USB_HC:
  954. case POWER_SUPPLY_PROP_USB_OTG:
  955. return 1;
  956. default:
  957. break;
  958. }
  959. return 0;
  960. }
  961. static enum power_supply_property smb347_usb_properties[] = {
  962. POWER_SUPPLY_PROP_ONLINE,
  963. POWER_SUPPLY_PROP_USB_HC,
  964. POWER_SUPPLY_PROP_USB_OTG,
  965. };
  966. static int smb347_battery_get_property(struct power_supply *psy,
  967. enum power_supply_property prop,
  968. union power_supply_propval *val)
  969. {
  970. struct smb347_charger *smb =
  971. container_of(psy, struct smb347_charger, battery);
  972. const struct smb347_charger_platform_data *pdata = smb->pdata;
  973. int ret;
  974. ret = smb347_update_status(smb);
  975. if (ret < 0)
  976. return ret;
  977. if (ret > 0) {
  978. smb347_update_online(smb);
  979. power_supply_changed(&smb->mains);
  980. power_supply_changed(&smb->usb);
  981. }
  982. switch (prop) {
  983. case POWER_SUPPLY_PROP_STATUS:
  984. if (!smb347_is_online(smb)) {
  985. smb->is_fully_charged = false;
  986. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  987. break;
  988. }
  989. if (smb347_charging_status(smb))
  990. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  991. else
  992. val->intval = smb->is_fully_charged ?
  993. POWER_SUPPLY_STATUS_FULL :
  994. POWER_SUPPLY_STATUS_NOT_CHARGING;
  995. break;
  996. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  997. if (!smb347_is_online(smb))
  998. return -ENODATA;
  999. /*
  1000. * We handle trickle and pre-charging the same, and taper
  1001. * and none the same.
  1002. */
  1003. switch (smb347_charging_status(smb)) {
  1004. case 1:
  1005. val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  1006. break;
  1007. case 2:
  1008. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  1009. break;
  1010. default:
  1011. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  1012. break;
  1013. }
  1014. break;
  1015. case POWER_SUPPLY_PROP_TECHNOLOGY:
  1016. val->intval = pdata->battery_info.technology;
  1017. break;
  1018. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  1019. val->intval = pdata->battery_info.voltage_min_design;
  1020. break;
  1021. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  1022. val->intval = pdata->battery_info.voltage_max_design;
  1023. break;
  1024. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1025. if (!smb347_is_online(smb))
  1026. return -ENODATA;
  1027. ret = smb347_read(smb, STAT_A);
  1028. if (ret < 0)
  1029. return ret;
  1030. ret &= STAT_A_FLOAT_VOLTAGE_MASK;
  1031. if (ret > 0x3d)
  1032. ret = 0x3d;
  1033. val->intval = 3500000 + ret * 20000;
  1034. break;
  1035. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1036. if (!smb347_is_online(smb))
  1037. return -ENODATA;
  1038. ret = smb347_read(smb, STAT_B);
  1039. if (ret < 0)
  1040. return ret;
  1041. /*
  1042. * The current value is composition of FCC and PCC values
  1043. * and we can detect which table to use from bit 5.
  1044. */
  1045. if (ret & 0x20) {
  1046. val->intval = hw_to_current(fcc_tbl,
  1047. ARRAY_SIZE(fcc_tbl),
  1048. ret & 7);
  1049. } else {
  1050. ret >>= 3;
  1051. val->intval = hw_to_current(pcc_tbl,
  1052. ARRAY_SIZE(pcc_tbl),
  1053. ret & 7);
  1054. }
  1055. break;
  1056. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  1057. val->intval = pdata->battery_info.charge_full_design;
  1058. break;
  1059. case POWER_SUPPLY_PROP_CHARGE_ENABLED:
  1060. val->intval = smb->charging_enabled;
  1061. break;
  1062. case POWER_SUPPLY_PROP_MODEL_NAME:
  1063. val->strval = pdata->battery_info.name;
  1064. break;
  1065. default:
  1066. return -EINVAL;
  1067. }
  1068. return 0;
  1069. }
  1070. static int smb347_battery_set_property(struct power_supply *psy,
  1071. enum power_supply_property prop,
  1072. const union power_supply_propval *val)
  1073. {
  1074. int ret = -EINVAL;
  1075. struct smb347_charger *smb =
  1076. container_of(psy, struct smb347_charger, battery);
  1077. switch (prop) {
  1078. case POWER_SUPPLY_PROP_CHARGE_ENABLED:
  1079. ret = smb347_charging_set(smb, val->intval);
  1080. break;
  1081. default:
  1082. break;
  1083. }
  1084. return ret;
  1085. }
  1086. static int smb347_battery_property_is_writeable(struct power_supply *psy,
  1087. enum power_supply_property prop)
  1088. {
  1089. switch (prop) {
  1090. case POWER_SUPPLY_PROP_CHARGE_ENABLED:
  1091. return 1;
  1092. default:
  1093. break;
  1094. }
  1095. return 0;
  1096. }
  1097. static enum power_supply_property smb347_battery_properties[] = {
  1098. POWER_SUPPLY_PROP_STATUS,
  1099. POWER_SUPPLY_PROP_CHARGE_TYPE,
  1100. POWER_SUPPLY_PROP_TECHNOLOGY,
  1101. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  1102. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  1103. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  1104. POWER_SUPPLY_PROP_CURRENT_NOW,
  1105. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  1106. POWER_SUPPLY_PROP_CHARGE_ENABLED,
  1107. POWER_SUPPLY_PROP_MODEL_NAME,
  1108. };
  1109. static int smb347_debugfs_show(struct seq_file *s, void *data)
  1110. {
  1111. struct smb347_charger *smb = s->private;
  1112. int ret;
  1113. u8 reg;
  1114. seq_printf(s, "Control registers:\n");
  1115. seq_printf(s, "==================\n");
  1116. for (reg = CFG_CHARGE_CURRENT; reg <= CFG_ADDRESS; reg++) {
  1117. ret = smb347_read(smb, reg);
  1118. seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
  1119. }
  1120. seq_printf(s, "\n");
  1121. seq_printf(s, "Command registers:\n");
  1122. seq_printf(s, "==================\n");
  1123. ret = smb347_read(smb, CMD_A);
  1124. seq_printf(s, "0x%02x:\t0x%02x\n", CMD_A, ret);
  1125. ret = smb347_read(smb, CMD_B);
  1126. seq_printf(s, "0x%02x:\t0x%02x\n", CMD_B, ret);
  1127. ret = smb347_read(smb, CMD_C);
  1128. seq_printf(s, "0x%02x:\t0x%02x\n", CMD_C, ret);
  1129. seq_printf(s, "\n");
  1130. seq_printf(s, "Interrupt status registers:\n");
  1131. seq_printf(s, "===========================\n");
  1132. for (reg = IRQSTAT_A; reg <= IRQSTAT_F; reg++) {
  1133. ret = smb347_read(smb, reg);
  1134. seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
  1135. }
  1136. seq_printf(s, "\n");
  1137. seq_printf(s, "Status registers:\n");
  1138. seq_printf(s, "=================\n");
  1139. for (reg = STAT_A; reg <= STAT_E; reg++) {
  1140. ret = smb347_read(smb, reg);
  1141. seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret);
  1142. }
  1143. return 0;
  1144. }
  1145. static int smb347_debugfs_open(struct inode *inode, struct file *file)
  1146. {
  1147. return single_open(file, smb347_debugfs_show, inode->i_private);
  1148. }
  1149. static const struct file_operations smb347_debugfs_fops = {
  1150. .open = smb347_debugfs_open,
  1151. .read = seq_read,
  1152. .llseek = seq_lseek,
  1153. .release = single_release,
  1154. };
  1155. static int smb347_probe(struct i2c_client *client,
  1156. const struct i2c_device_id *id)
  1157. {
  1158. static char *battery[] = { "smb347-battery" };
  1159. const struct smb347_charger_platform_data *pdata;
  1160. struct device *dev = &client->dev;
  1161. struct smb347_charger *smb;
  1162. int ret;
  1163. pdata = dev->platform_data;
  1164. if (!pdata)
  1165. return -EINVAL;
  1166. if (!pdata->use_mains && !pdata->use_usb)
  1167. return -EINVAL;
  1168. smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL);
  1169. if (!smb)
  1170. return -ENOMEM;
  1171. i2c_set_clientdata(client, smb);
  1172. mutex_init(&smb->lock);
  1173. smb->client = client;
  1174. smb->pdata = pdata;
  1175. smb->mains_current_limit = smb->pdata->mains_current_limit;
  1176. if (pdata->en_gpio) {
  1177. ret = gpio_request_one(
  1178. pdata->en_gpio,
  1179. smb->pdata->enable_control ==
  1180. SMB347_CHG_ENABLE_PIN_ACTIVE_LOW ?
  1181. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
  1182. smb->client->name);
  1183. if (ret < 0)
  1184. dev_warn(dev, "failed to claim EN GPIO: %d\n", ret);
  1185. else
  1186. smb->en_gpio = pdata->en_gpio;
  1187. }
  1188. ret = smb347_write(smb, CMD_B, CMD_B_POR);
  1189. if (ret < 0)
  1190. return ret;
  1191. msleep(20);
  1192. ret = smb347_read(smb, CMD_B);
  1193. if (ret < 0) {
  1194. dev_err(dev, "failed read after reset\n");
  1195. return ret;
  1196. }
  1197. ret = smb347_hw_init(smb);
  1198. if (ret < 0)
  1199. return ret;
  1200. smb->mains.name = "smb347-mains";
  1201. smb->mains.type = POWER_SUPPLY_TYPE_MAINS;
  1202. smb->mains.get_property = smb347_mains_get_property;
  1203. smb->mains.set_property = smb347_mains_set_property;
  1204. smb->mains.property_is_writeable = smb347_mains_property_is_writeable;
  1205. smb->mains.properties = smb347_mains_properties;
  1206. smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties);
  1207. smb->mains.supplied_to = battery;
  1208. smb->mains.num_supplicants = ARRAY_SIZE(battery);
  1209. smb->usb.name = "smb347-usb";
  1210. smb->usb.type = POWER_SUPPLY_TYPE_USB;
  1211. smb->usb.get_property = smb347_usb_get_property;
  1212. smb->usb.set_property = smb347_usb_set_property;
  1213. smb->usb.property_is_writeable = smb347_usb_property_is_writeable;
  1214. smb->usb.properties = smb347_usb_properties;
  1215. smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties);
  1216. smb->usb.supplied_to = battery;
  1217. smb->usb.num_supplicants = ARRAY_SIZE(battery);
  1218. smb->battery.name = "smb347-battery";
  1219. smb->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  1220. smb->battery.get_property = smb347_battery_get_property;
  1221. smb->battery.set_property = smb347_battery_set_property;
  1222. smb->battery.property_is_writeable = smb347_battery_property_is_writeable;
  1223. smb->battery.properties = smb347_battery_properties;
  1224. smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties);
  1225. if (smb->pdata->supplied_to) {
  1226. smb->battery.supplied_to = smb->pdata->supplied_to;
  1227. smb->battery.num_supplicants = smb->pdata->num_supplicants;
  1228. smb->battery.external_power_changed = power_supply_changed;
  1229. }
  1230. ret = power_supply_register(dev, &smb->mains);
  1231. if (ret < 0)
  1232. return ret;
  1233. ret = power_supply_register(dev, &smb->usb);
  1234. if (ret < 0) {
  1235. power_supply_unregister(&smb->mains);
  1236. return ret;
  1237. }
  1238. ret = power_supply_register(dev, &smb->battery);
  1239. if (ret < 0) {
  1240. power_supply_unregister(&smb->usb);
  1241. power_supply_unregister(&smb->mains);
  1242. return ret;
  1243. }
  1244. /*
  1245. * Interrupt pin is optional. If it is connected, we setup the
  1246. * interrupt support here.
  1247. */
  1248. if (pdata->irq_gpio >= 0) {
  1249. ret = smb347_irq_init(smb);
  1250. if (ret < 0) {
  1251. dev_warn(dev, "failed to initialize IRQ: %d\n", ret);
  1252. dev_warn(dev, "disabling IRQ support\n");
  1253. }
  1254. }
  1255. smb->dentry = debugfs_create_file("smb347-regs", S_IRUSR, NULL, smb,
  1256. &smb347_debugfs_fops);
  1257. return 0;
  1258. }
  1259. static int smb347_remove(struct i2c_client *client)
  1260. {
  1261. struct smb347_charger *smb = i2c_get_clientdata(client);
  1262. if (!IS_ERR_OR_NULL(smb->dentry))
  1263. debugfs_remove(smb->dentry);
  1264. if (client->irq) {
  1265. smb347_irq_disable(smb);
  1266. disable_irq_wake(client->irq);
  1267. free_irq(client->irq, smb);
  1268. gpio_free(smb->pdata->irq_gpio);
  1269. }
  1270. power_supply_unregister(&smb->battery);
  1271. power_supply_unregister(&smb->usb);
  1272. power_supply_unregister(&smb->mains);
  1273. return 0;
  1274. }
  1275. static int smb347_suspend(struct device *dev)
  1276. {
  1277. struct i2c_client *client = to_i2c_client(dev);
  1278. if (client->irq)
  1279. disable_irq(client->irq);
  1280. return 0;
  1281. }
  1282. static int smb347_resume(struct device *dev)
  1283. {
  1284. struct i2c_client *client = to_i2c_client(dev);
  1285. if (client->irq)
  1286. enable_irq(client->irq);
  1287. return 0;
  1288. }
  1289. static const struct dev_pm_ops smb347_pm_ops = {
  1290. .suspend = smb347_suspend,
  1291. .resume = smb347_resume,
  1292. };
  1293. static const struct i2c_device_id smb347_id[] = {
  1294. { "smb347", 0 },
  1295. { }
  1296. };
  1297. MODULE_DEVICE_TABLE(i2c, smb347_id);
  1298. static struct i2c_driver smb347_driver = {
  1299. .driver = {
  1300. .name = "smb347",
  1301. .pm = &smb347_pm_ops,
  1302. },
  1303. .probe = smb347_probe,
  1304. .remove = __devexit_p(smb347_remove),
  1305. .id_table = smb347_id,
  1306. };
  1307. static int __init smb347_init(void)
  1308. {
  1309. return i2c_add_driver(&smb347_driver);
  1310. }
  1311. module_init(smb347_init);
  1312. static void __exit smb347_exit(void)
  1313. {
  1314. i2c_del_driver(&smb347_driver);
  1315. }
  1316. module_exit(smb347_exit);
  1317. MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>");
  1318. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  1319. MODULE_DESCRIPTION("SMB347 battery charger driver");
  1320. MODULE_LICENSE("GPL");
  1321. MODULE_ALIAS("i2c:smb347");