sbs-battery.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * Gas Gauge driver for SBS Compliant Batteries
  3. *
  4. * Copyright (c) 2010, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/i2c.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/power/sbs-battery.h>
  27. #include <linux/power_supply.h>
  28. #include <linux/slab.h>
  29. #include <linux/stat.h>
  30. enum {
  31. REG_MANUFACTURER_DATA,
  32. REG_TEMPERATURE,
  33. REG_VOLTAGE,
  34. REG_CURRENT,
  35. REG_CAPACITY,
  36. REG_TIME_TO_EMPTY,
  37. REG_TIME_TO_FULL,
  38. REG_STATUS,
  39. REG_CAPACITY_LEVEL,
  40. REG_CYCLE_COUNT,
  41. REG_SERIAL_NUMBER,
  42. REG_REMAINING_CAPACITY,
  43. REG_REMAINING_CAPACITY_CHARGE,
  44. REG_FULL_CHARGE_CAPACITY,
  45. REG_FULL_CHARGE_CAPACITY_CHARGE,
  46. REG_DESIGN_CAPACITY,
  47. REG_DESIGN_CAPACITY_CHARGE,
  48. REG_DESIGN_VOLTAGE_MIN,
  49. REG_DESIGN_VOLTAGE_MAX,
  50. REG_MANUFACTURER,
  51. REG_MODEL_NAME,
  52. };
  53. /* Battery Mode defines */
  54. #define BATTERY_MODE_OFFSET 0x03
  55. #define BATTERY_MODE_MASK 0x8000
  56. enum sbs_battery_mode {
  57. BATTERY_MODE_AMPS = 0,
  58. BATTERY_MODE_WATTS = 0x8000
  59. };
  60. /* manufacturer access defines */
  61. #define MANUFACTURER_ACCESS_STATUS 0x0006
  62. #define MANUFACTURER_ACCESS_SLEEP 0x0011
  63. /* battery status value bits */
  64. #define BATTERY_INITIALIZED 0x80
  65. #define BATTERY_DISCHARGING 0x40
  66. #define BATTERY_FULL_CHARGED 0x20
  67. #define BATTERY_FULL_DISCHARGED 0x10
  68. /* min_value and max_value are only valid for numerical data */
  69. #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
  70. .psp = _psp, \
  71. .addr = _addr, \
  72. .min_value = _min_value, \
  73. .max_value = _max_value, \
  74. }
  75. static const struct chip_data {
  76. enum power_supply_property psp;
  77. u8 addr;
  78. int min_value;
  79. int max_value;
  80. } sbs_data[] = {
  81. [REG_MANUFACTURER_DATA] =
  82. SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
  83. [REG_TEMPERATURE] =
  84. SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
  85. [REG_VOLTAGE] =
  86. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
  87. [REG_CURRENT] =
  88. SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
  89. [REG_CAPACITY] =
  90. SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
  91. [REG_REMAINING_CAPACITY] =
  92. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
  93. [REG_REMAINING_CAPACITY_CHARGE] =
  94. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
  95. [REG_FULL_CHARGE_CAPACITY] =
  96. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
  97. [REG_FULL_CHARGE_CAPACITY_CHARGE] =
  98. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
  99. [REG_TIME_TO_EMPTY] =
  100. SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
  101. [REG_TIME_TO_FULL] =
  102. SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
  103. [REG_STATUS] =
  104. SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
  105. [REG_CAPACITY_LEVEL] =
  106. SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
  107. [REG_CYCLE_COUNT] =
  108. SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
  109. [REG_DESIGN_CAPACITY] =
  110. SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
  111. [REG_DESIGN_CAPACITY_CHARGE] =
  112. SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
  113. [REG_DESIGN_VOLTAGE_MIN] =
  114. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
  115. [REG_DESIGN_VOLTAGE_MAX] =
  116. SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
  117. [REG_SERIAL_NUMBER] =
  118. SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
  119. /* Properties of type `const char *' */
  120. [REG_MANUFACTURER] =
  121. SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
  122. [REG_MODEL_NAME] =
  123. SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
  124. };
  125. static enum power_supply_property sbs_properties[] = {
  126. POWER_SUPPLY_PROP_STATUS,
  127. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  128. POWER_SUPPLY_PROP_HEALTH,
  129. POWER_SUPPLY_PROP_PRESENT,
  130. POWER_SUPPLY_PROP_TECHNOLOGY,
  131. POWER_SUPPLY_PROP_CYCLE_COUNT,
  132. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  133. POWER_SUPPLY_PROP_CURRENT_NOW,
  134. POWER_SUPPLY_PROP_CAPACITY,
  135. POWER_SUPPLY_PROP_TEMP,
  136. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  137. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  138. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  139. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  140. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  141. POWER_SUPPLY_PROP_ENERGY_NOW,
  142. POWER_SUPPLY_PROP_ENERGY_FULL,
  143. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  144. POWER_SUPPLY_PROP_CHARGE_NOW,
  145. POWER_SUPPLY_PROP_CHARGE_FULL,
  146. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  147. /* Properties of type `const char *' */
  148. POWER_SUPPLY_PROP_MANUFACTURER,
  149. POWER_SUPPLY_PROP_MODEL_NAME
  150. };
  151. /* Supports special manufacturer commands from TI BQ20Z75 IC. */
  152. #define SBS_FLAGS_TI_BQ20Z75 BIT(0)
  153. struct sbs_info {
  154. struct i2c_client *client;
  155. struct power_supply *power_supply;
  156. bool is_present;
  157. struct gpio_desc *gpio_detect;
  158. bool enable_detection;
  159. int last_state;
  160. int poll_time;
  161. u32 i2c_retry_count;
  162. u32 poll_retry_count;
  163. struct delayed_work work;
  164. struct mutex mode_lock;
  165. u32 flags;
  166. };
  167. static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
  168. static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
  169. static bool force_load;
  170. static int sbs_read_word_data(struct i2c_client *client, u8 address)
  171. {
  172. struct sbs_info *chip = i2c_get_clientdata(client);
  173. int retries = chip->i2c_retry_count;
  174. s32 ret = 0;
  175. while (retries > 0) {
  176. ret = i2c_smbus_read_word_data(client, address);
  177. if (ret >= 0)
  178. break;
  179. retries--;
  180. }
  181. if (ret < 0) {
  182. dev_dbg(&client->dev,
  183. "%s: i2c read at address 0x%x failed\n",
  184. __func__, address);
  185. return ret;
  186. }
  187. return ret;
  188. }
  189. static int sbs_read_string_data(struct i2c_client *client, u8 address,
  190. char *values)
  191. {
  192. struct sbs_info *chip = i2c_get_clientdata(client);
  193. s32 ret = 0, block_length = 0;
  194. int retries_length, retries_block;
  195. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  196. retries_length = chip->i2c_retry_count;
  197. retries_block = chip->i2c_retry_count;
  198. /* Adapter needs to support these two functions */
  199. if (!i2c_check_functionality(client->adapter,
  200. I2C_FUNC_SMBUS_BYTE_DATA |
  201. I2C_FUNC_SMBUS_I2C_BLOCK)){
  202. return -ENODEV;
  203. }
  204. /* Get the length of block data */
  205. while (retries_length > 0) {
  206. ret = i2c_smbus_read_byte_data(client, address);
  207. if (ret >= 0)
  208. break;
  209. retries_length--;
  210. }
  211. if (ret < 0) {
  212. dev_dbg(&client->dev,
  213. "%s: i2c read at address 0x%x failed\n",
  214. __func__, address);
  215. return ret;
  216. }
  217. /* block_length does not include NULL terminator */
  218. block_length = ret;
  219. if (block_length > I2C_SMBUS_BLOCK_MAX) {
  220. dev_err(&client->dev,
  221. "%s: Returned block_length is longer than 0x%x\n",
  222. __func__, I2C_SMBUS_BLOCK_MAX);
  223. return -EINVAL;
  224. }
  225. /* Get the block data */
  226. while (retries_block > 0) {
  227. ret = i2c_smbus_read_i2c_block_data(
  228. client, address,
  229. block_length + 1, block_buffer);
  230. if (ret >= 0)
  231. break;
  232. retries_block--;
  233. }
  234. if (ret < 0) {
  235. dev_dbg(&client->dev,
  236. "%s: i2c read at address 0x%x failed\n",
  237. __func__, address);
  238. return ret;
  239. }
  240. /* block_buffer[0] == block_length */
  241. memcpy(values, block_buffer + 1, block_length);
  242. values[block_length] = '\0';
  243. return ret;
  244. }
  245. static int sbs_write_word_data(struct i2c_client *client, u8 address,
  246. u16 value)
  247. {
  248. struct sbs_info *chip = i2c_get_clientdata(client);
  249. int retries = chip->i2c_retry_count;
  250. s32 ret = 0;
  251. while (retries > 0) {
  252. ret = i2c_smbus_write_word_data(client, address, value);
  253. if (ret >= 0)
  254. break;
  255. retries--;
  256. }
  257. if (ret < 0) {
  258. dev_dbg(&client->dev,
  259. "%s: i2c write to address 0x%x failed\n",
  260. __func__, address);
  261. return ret;
  262. }
  263. return 0;
  264. }
  265. static int sbs_status_correct(struct i2c_client *client, int *intval)
  266. {
  267. int ret;
  268. ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
  269. if (ret < 0)
  270. return ret;
  271. ret = (s16)ret;
  272. /* Not drawing current means full (cannot be not charging) */
  273. if (ret == 0)
  274. *intval = POWER_SUPPLY_STATUS_FULL;
  275. if (*intval == POWER_SUPPLY_STATUS_FULL) {
  276. /* Drawing or providing current when full */
  277. if (ret > 0)
  278. *intval = POWER_SUPPLY_STATUS_CHARGING;
  279. else if (ret < 0)
  280. *intval = POWER_SUPPLY_STATUS_DISCHARGING;
  281. }
  282. return 0;
  283. }
  284. static int sbs_get_battery_presence_and_health(
  285. struct i2c_client *client, enum power_supply_property psp,
  286. union power_supply_propval *val)
  287. {
  288. int ret;
  289. /* Dummy command; if it succeeds, battery is present. */
  290. ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
  291. if (ret < 0) { /* battery not present*/
  292. if (psp == POWER_SUPPLY_PROP_PRESENT) {
  293. val->intval = 0;
  294. return 0;
  295. }
  296. return ret;
  297. }
  298. if (psp == POWER_SUPPLY_PROP_PRESENT)
  299. val->intval = 1; /* battery present */
  300. else /* POWER_SUPPLY_PROP_HEALTH */
  301. /* SBS spec doesn't have a general health command. */
  302. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  303. return 0;
  304. }
  305. static int sbs_get_ti_battery_presence_and_health(
  306. struct i2c_client *client, enum power_supply_property psp,
  307. union power_supply_propval *val)
  308. {
  309. s32 ret;
  310. /*
  311. * Write to ManufacturerAccess with ManufacturerAccess command
  312. * and then read the status.
  313. */
  314. ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
  315. MANUFACTURER_ACCESS_STATUS);
  316. if (ret < 0) {
  317. if (psp == POWER_SUPPLY_PROP_PRESENT)
  318. val->intval = 0; /* battery removed */
  319. return ret;
  320. }
  321. ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
  322. if (ret < 0) {
  323. if (psp == POWER_SUPPLY_PROP_PRESENT)
  324. val->intval = 0; /* battery removed */
  325. return ret;
  326. }
  327. if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
  328. ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
  329. val->intval = 0;
  330. return 0;
  331. }
  332. /* Mask the upper nibble of 2nd byte and
  333. * lower byte of response then
  334. * shift the result by 8 to get status*/
  335. ret &= 0x0F00;
  336. ret >>= 8;
  337. if (psp == POWER_SUPPLY_PROP_PRESENT) {
  338. if (ret == 0x0F)
  339. /* battery removed */
  340. val->intval = 0;
  341. else
  342. val->intval = 1;
  343. } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
  344. if (ret == 0x09)
  345. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  346. else if (ret == 0x0B)
  347. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  348. else if (ret == 0x0C)
  349. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  350. else
  351. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  352. }
  353. return 0;
  354. }
  355. static int sbs_get_battery_property(struct i2c_client *client,
  356. int reg_offset, enum power_supply_property psp,
  357. union power_supply_propval *val)
  358. {
  359. struct sbs_info *chip = i2c_get_clientdata(client);
  360. s32 ret;
  361. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  362. if (ret < 0)
  363. return ret;
  364. /* returned values are 16 bit */
  365. if (sbs_data[reg_offset].min_value < 0)
  366. ret = (s16)ret;
  367. if (ret >= sbs_data[reg_offset].min_value &&
  368. ret <= sbs_data[reg_offset].max_value) {
  369. val->intval = ret;
  370. if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
  371. if (!(ret & BATTERY_INITIALIZED))
  372. val->intval =
  373. POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  374. else if (ret & BATTERY_FULL_CHARGED)
  375. val->intval =
  376. POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  377. else if (ret & BATTERY_FULL_DISCHARGED)
  378. val->intval =
  379. POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  380. else
  381. val->intval =
  382. POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  383. return 0;
  384. } else if (psp != POWER_SUPPLY_PROP_STATUS) {
  385. return 0;
  386. }
  387. if (ret & BATTERY_FULL_CHARGED)
  388. val->intval = POWER_SUPPLY_STATUS_FULL;
  389. else if (ret & BATTERY_DISCHARGING)
  390. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  391. else
  392. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  393. sbs_status_correct(client, &val->intval);
  394. if (chip->poll_time == 0)
  395. chip->last_state = val->intval;
  396. else if (chip->last_state != val->intval) {
  397. cancel_delayed_work_sync(&chip->work);
  398. power_supply_changed(chip->power_supply);
  399. chip->poll_time = 0;
  400. }
  401. } else {
  402. if (psp == POWER_SUPPLY_PROP_STATUS)
  403. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  404. else if (psp == POWER_SUPPLY_PROP_CAPACITY)
  405. /* sbs spec says that this can be >100 %
  406. * even if max value is 100 %
  407. */
  408. val->intval = min(ret, 100);
  409. else
  410. val->intval = 0;
  411. }
  412. return 0;
  413. }
  414. static int sbs_get_battery_string_property(struct i2c_client *client,
  415. int reg_offset, enum power_supply_property psp, char *val)
  416. {
  417. s32 ret;
  418. ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
  419. if (ret < 0)
  420. return ret;
  421. return 0;
  422. }
  423. static void sbs_unit_adjustment(struct i2c_client *client,
  424. enum power_supply_property psp, union power_supply_propval *val)
  425. {
  426. #define BASE_UNIT_CONVERSION 1000
  427. #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
  428. #define TIME_UNIT_CONVERSION 60
  429. #define TEMP_KELVIN_TO_CELSIUS 2731
  430. switch (psp) {
  431. case POWER_SUPPLY_PROP_ENERGY_NOW:
  432. case POWER_SUPPLY_PROP_ENERGY_FULL:
  433. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  434. /* sbs provides energy in units of 10mWh.
  435. * Convert to µWh
  436. */
  437. val->intval *= BATTERY_MODE_CAP_MULT_WATT;
  438. break;
  439. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  440. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  441. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  442. case POWER_SUPPLY_PROP_CURRENT_NOW:
  443. case POWER_SUPPLY_PROP_CHARGE_NOW:
  444. case POWER_SUPPLY_PROP_CHARGE_FULL:
  445. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  446. val->intval *= BASE_UNIT_CONVERSION;
  447. break;
  448. case POWER_SUPPLY_PROP_TEMP:
  449. /* sbs provides battery temperature in 0.1K
  450. * so convert it to 0.1°C
  451. */
  452. val->intval -= TEMP_KELVIN_TO_CELSIUS;
  453. break;
  454. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  455. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  456. /* sbs provides time to empty and time to full in minutes.
  457. * Convert to seconds
  458. */
  459. val->intval *= TIME_UNIT_CONVERSION;
  460. break;
  461. default:
  462. dev_dbg(&client->dev,
  463. "%s: no need for unit conversion %d\n", __func__, psp);
  464. }
  465. }
  466. static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
  467. enum sbs_battery_mode mode)
  468. {
  469. int ret, original_val;
  470. original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
  471. if (original_val < 0)
  472. return original_val;
  473. if ((original_val & BATTERY_MODE_MASK) == mode)
  474. return mode;
  475. if (mode == BATTERY_MODE_AMPS)
  476. ret = original_val & ~BATTERY_MODE_MASK;
  477. else
  478. ret = original_val | BATTERY_MODE_MASK;
  479. ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
  480. if (ret < 0)
  481. return ret;
  482. usleep_range(1000, 2000);
  483. return original_val & BATTERY_MODE_MASK;
  484. }
  485. static int sbs_get_battery_capacity(struct i2c_client *client,
  486. int reg_offset, enum power_supply_property psp,
  487. union power_supply_propval *val)
  488. {
  489. s32 ret;
  490. enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
  491. if (power_supply_is_amp_property(psp))
  492. mode = BATTERY_MODE_AMPS;
  493. mode = sbs_set_battery_mode(client, mode);
  494. if (mode < 0)
  495. return mode;
  496. ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
  497. if (ret < 0)
  498. return ret;
  499. val->intval = ret;
  500. ret = sbs_set_battery_mode(client, mode);
  501. if (ret < 0)
  502. return ret;
  503. return 0;
  504. }
  505. static char sbs_serial[5];
  506. static int sbs_get_battery_serial_number(struct i2c_client *client,
  507. union power_supply_propval *val)
  508. {
  509. int ret;
  510. ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
  511. if (ret < 0)
  512. return ret;
  513. sprintf(sbs_serial, "%04x", ret);
  514. val->strval = sbs_serial;
  515. return 0;
  516. }
  517. static int sbs_get_property_index(struct i2c_client *client,
  518. enum power_supply_property psp)
  519. {
  520. int count;
  521. for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
  522. if (psp == sbs_data[count].psp)
  523. return count;
  524. dev_warn(&client->dev,
  525. "%s: Invalid Property - %d\n", __func__, psp);
  526. return -EINVAL;
  527. }
  528. static int sbs_get_property(struct power_supply *psy,
  529. enum power_supply_property psp,
  530. union power_supply_propval *val)
  531. {
  532. int ret = 0;
  533. struct sbs_info *chip = power_supply_get_drvdata(psy);
  534. struct i2c_client *client = chip->client;
  535. if (chip->gpio_detect) {
  536. ret = gpiod_get_value_cansleep(chip->gpio_detect);
  537. if (ret < 0)
  538. return ret;
  539. if (psp == POWER_SUPPLY_PROP_PRESENT) {
  540. val->intval = ret;
  541. chip->is_present = val->intval;
  542. return 0;
  543. }
  544. if (ret == 0)
  545. return -ENODATA;
  546. }
  547. switch (psp) {
  548. case POWER_SUPPLY_PROP_PRESENT:
  549. case POWER_SUPPLY_PROP_HEALTH:
  550. if (chip->flags & SBS_FLAGS_TI_BQ20Z75)
  551. ret = sbs_get_ti_battery_presence_and_health(client,
  552. psp, val);
  553. else
  554. ret = sbs_get_battery_presence_and_health(client, psp,
  555. val);
  556. /* this can only be true if no gpio is used */
  557. if (psp == POWER_SUPPLY_PROP_PRESENT)
  558. return 0;
  559. break;
  560. case POWER_SUPPLY_PROP_TECHNOLOGY:
  561. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  562. goto done; /* don't trigger power_supply_changed()! */
  563. case POWER_SUPPLY_PROP_ENERGY_NOW:
  564. case POWER_SUPPLY_PROP_ENERGY_FULL:
  565. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  566. case POWER_SUPPLY_PROP_CHARGE_NOW:
  567. case POWER_SUPPLY_PROP_CHARGE_FULL:
  568. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  569. ret = sbs_get_property_index(client, psp);
  570. if (ret < 0)
  571. break;
  572. /* sbs_get_battery_capacity() will change the battery mode
  573. * temporarily to read the requested attribute. Ensure we stay
  574. * in the desired mode for the duration of the attribute read.
  575. */
  576. mutex_lock(&chip->mode_lock);
  577. ret = sbs_get_battery_capacity(client, ret, psp, val);
  578. mutex_unlock(&chip->mode_lock);
  579. break;
  580. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  581. ret = sbs_get_battery_serial_number(client, val);
  582. break;
  583. case POWER_SUPPLY_PROP_STATUS:
  584. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  585. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  586. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  587. case POWER_SUPPLY_PROP_CURRENT_NOW:
  588. case POWER_SUPPLY_PROP_TEMP:
  589. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  590. case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
  591. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  592. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  593. case POWER_SUPPLY_PROP_CAPACITY:
  594. ret = sbs_get_property_index(client, psp);
  595. if (ret < 0)
  596. break;
  597. ret = sbs_get_battery_property(client, ret, psp, val);
  598. break;
  599. case POWER_SUPPLY_PROP_MODEL_NAME:
  600. ret = sbs_get_property_index(client, psp);
  601. if (ret < 0)
  602. break;
  603. ret = sbs_get_battery_string_property(client, ret, psp,
  604. model_name);
  605. val->strval = model_name;
  606. break;
  607. case POWER_SUPPLY_PROP_MANUFACTURER:
  608. ret = sbs_get_property_index(client, psp);
  609. if (ret < 0)
  610. break;
  611. ret = sbs_get_battery_string_property(client, ret, psp,
  612. manufacturer);
  613. val->strval = manufacturer;
  614. break;
  615. default:
  616. dev_err(&client->dev,
  617. "%s: INVALID property\n", __func__);
  618. return -EINVAL;
  619. }
  620. if (!chip->enable_detection)
  621. goto done;
  622. if (!chip->gpio_detect &&
  623. chip->is_present != (ret >= 0)) {
  624. chip->is_present = (ret >= 0);
  625. power_supply_changed(chip->power_supply);
  626. }
  627. done:
  628. if (!ret) {
  629. /* Convert units to match requirements for power supply class */
  630. sbs_unit_adjustment(client, psp, val);
  631. }
  632. dev_dbg(&client->dev,
  633. "%s: property = %d, value = %x\n", __func__, psp, val->intval);
  634. if (ret && chip->is_present)
  635. return ret;
  636. /* battery not present, so return NODATA for properties */
  637. if (ret)
  638. return -ENODATA;
  639. return 0;
  640. }
  641. static void sbs_supply_changed(struct sbs_info *chip)
  642. {
  643. struct power_supply *battery = chip->power_supply;
  644. int ret;
  645. ret = gpiod_get_value_cansleep(chip->gpio_detect);
  646. if (ret < 0)
  647. return;
  648. chip->is_present = ret;
  649. power_supply_changed(battery);
  650. }
  651. static irqreturn_t sbs_irq(int irq, void *devid)
  652. {
  653. sbs_supply_changed(devid);
  654. return IRQ_HANDLED;
  655. }
  656. static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
  657. unsigned int data)
  658. {
  659. sbs_supply_changed(i2c_get_clientdata(client));
  660. }
  661. static void sbs_external_power_changed(struct power_supply *psy)
  662. {
  663. struct sbs_info *chip = power_supply_get_drvdata(psy);
  664. /* cancel outstanding work */
  665. cancel_delayed_work_sync(&chip->work);
  666. schedule_delayed_work(&chip->work, HZ);
  667. chip->poll_time = chip->poll_retry_count;
  668. }
  669. static void sbs_delayed_work(struct work_struct *work)
  670. {
  671. struct sbs_info *chip;
  672. s32 ret;
  673. chip = container_of(work, struct sbs_info, work.work);
  674. ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
  675. /* if the read failed, give up on this work */
  676. if (ret < 0) {
  677. chip->poll_time = 0;
  678. return;
  679. }
  680. if (ret & BATTERY_FULL_CHARGED)
  681. ret = POWER_SUPPLY_STATUS_FULL;
  682. else if (ret & BATTERY_DISCHARGING)
  683. ret = POWER_SUPPLY_STATUS_DISCHARGING;
  684. else
  685. ret = POWER_SUPPLY_STATUS_CHARGING;
  686. sbs_status_correct(chip->client, &ret);
  687. if (chip->last_state != ret) {
  688. chip->poll_time = 0;
  689. power_supply_changed(chip->power_supply);
  690. return;
  691. }
  692. if (chip->poll_time > 0) {
  693. schedule_delayed_work(&chip->work, HZ);
  694. chip->poll_time--;
  695. return;
  696. }
  697. }
  698. static const struct power_supply_desc sbs_default_desc = {
  699. .type = POWER_SUPPLY_TYPE_BATTERY,
  700. .properties = sbs_properties,
  701. .num_properties = ARRAY_SIZE(sbs_properties),
  702. .get_property = sbs_get_property,
  703. .external_power_changed = sbs_external_power_changed,
  704. };
  705. static int sbs_probe(struct i2c_client *client,
  706. const struct i2c_device_id *id)
  707. {
  708. struct sbs_info *chip;
  709. struct power_supply_desc *sbs_desc;
  710. struct sbs_platform_data *pdata = client->dev.platform_data;
  711. struct power_supply_config psy_cfg = {};
  712. int rc;
  713. int irq;
  714. sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
  715. sizeof(*sbs_desc), GFP_KERNEL);
  716. if (!sbs_desc)
  717. return -ENOMEM;
  718. sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
  719. dev_name(&client->dev));
  720. if (!sbs_desc->name)
  721. return -ENOMEM;
  722. chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
  723. if (!chip)
  724. return -ENOMEM;
  725. chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
  726. chip->client = client;
  727. chip->enable_detection = false;
  728. psy_cfg.of_node = client->dev.of_node;
  729. psy_cfg.drv_data = chip;
  730. chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
  731. mutex_init(&chip->mode_lock);
  732. /* use pdata if available, fall back to DT properties,
  733. * or hardcoded defaults if not
  734. */
  735. rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
  736. &chip->i2c_retry_count);
  737. if (rc)
  738. chip->i2c_retry_count = 0;
  739. rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
  740. &chip->poll_retry_count);
  741. if (rc)
  742. chip->poll_retry_count = 0;
  743. if (pdata) {
  744. chip->poll_retry_count = pdata->poll_retry_count;
  745. chip->i2c_retry_count = pdata->i2c_retry_count;
  746. }
  747. chip->i2c_retry_count = chip->i2c_retry_count + 1;
  748. chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
  749. "sbs,battery-detect", GPIOD_IN);
  750. if (IS_ERR(chip->gpio_detect)) {
  751. dev_err(&client->dev, "Failed to get gpio: %ld\n",
  752. PTR_ERR(chip->gpio_detect));
  753. return PTR_ERR(chip->gpio_detect);
  754. }
  755. i2c_set_clientdata(client, chip);
  756. if (!chip->gpio_detect)
  757. goto skip_gpio;
  758. irq = gpiod_to_irq(chip->gpio_detect);
  759. if (irq <= 0) {
  760. dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
  761. goto skip_gpio;
  762. }
  763. rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
  764. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  765. dev_name(&client->dev), chip);
  766. if (rc) {
  767. dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
  768. goto skip_gpio;
  769. }
  770. skip_gpio:
  771. /*
  772. * Before we register, we might need to make sure we can actually talk
  773. * to the battery.
  774. */
  775. if (!(force_load || chip->gpio_detect)) {
  776. rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
  777. if (rc < 0) {
  778. dev_err(&client->dev, "%s: Failed to get device status\n",
  779. __func__);
  780. goto exit_psupply;
  781. }
  782. }
  783. chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
  784. &psy_cfg);
  785. if (IS_ERR(chip->power_supply)) {
  786. dev_err(&client->dev,
  787. "%s: Failed to register power supply\n", __func__);
  788. rc = PTR_ERR(chip->power_supply);
  789. goto exit_psupply;
  790. }
  791. dev_info(&client->dev,
  792. "%s: battery gas gauge device registered\n", client->name);
  793. INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
  794. chip->enable_detection = true;
  795. return 0;
  796. exit_psupply:
  797. return rc;
  798. }
  799. static int sbs_remove(struct i2c_client *client)
  800. {
  801. struct sbs_info *chip = i2c_get_clientdata(client);
  802. cancel_delayed_work_sync(&chip->work);
  803. return 0;
  804. }
  805. #if defined CONFIG_PM_SLEEP
  806. static int sbs_suspend(struct device *dev)
  807. {
  808. struct i2c_client *client = to_i2c_client(dev);
  809. struct sbs_info *chip = i2c_get_clientdata(client);
  810. int ret;
  811. if (chip->poll_time > 0)
  812. cancel_delayed_work_sync(&chip->work);
  813. if (chip->flags & SBS_FLAGS_TI_BQ20Z75) {
  814. /* Write to manufacturer access with sleep command. */
  815. ret = sbs_write_word_data(client,
  816. sbs_data[REG_MANUFACTURER_DATA].addr,
  817. MANUFACTURER_ACCESS_SLEEP);
  818. if (chip->is_present && ret < 0)
  819. return ret;
  820. }
  821. return 0;
  822. }
  823. static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
  824. #define SBS_PM_OPS (&sbs_pm_ops)
  825. #else
  826. #define SBS_PM_OPS NULL
  827. #endif
  828. static const struct i2c_device_id sbs_id[] = {
  829. { "bq20z75", 0 },
  830. { "sbs-battery", 1 },
  831. {}
  832. };
  833. MODULE_DEVICE_TABLE(i2c, sbs_id);
  834. static const struct of_device_id sbs_dt_ids[] = {
  835. { .compatible = "sbs,sbs-battery" },
  836. {
  837. .compatible = "ti,bq20z75",
  838. .data = (void *)SBS_FLAGS_TI_BQ20Z75,
  839. },
  840. { }
  841. };
  842. MODULE_DEVICE_TABLE(of, sbs_dt_ids);
  843. static struct i2c_driver sbs_battery_driver = {
  844. .probe = sbs_probe,
  845. .remove = sbs_remove,
  846. .alert = sbs_alert,
  847. .id_table = sbs_id,
  848. .driver = {
  849. .name = "sbs-battery",
  850. .of_match_table = sbs_dt_ids,
  851. .pm = SBS_PM_OPS,
  852. },
  853. };
  854. module_i2c_driver(sbs_battery_driver);
  855. MODULE_DESCRIPTION("SBS battery monitor driver");
  856. MODULE_LICENSE("GPL");
  857. module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
  858. MODULE_PARM_DESC(force_load,
  859. "Attempt to load the driver even if no battery is connected");