max17042_battery.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Fuel gauge driver for Maxim 17042 / 8966 / 8997
  4. // Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
  5. //
  6. // Copyright (C) 2011 Samsung Electronics
  7. // MyungJoo Ham <myungjoo.ham@samsung.com>
  8. //
  9. // This driver is based on max17040_battery.c
  10. #include <linux/acpi.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/i2c.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/pm.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/power/max17042_battery.h>
  21. #include <linux/of.h>
  22. #include <linux/regmap.h>
  23. /* Status register bits */
  24. #define STATUS_POR_BIT (1 << 1)
  25. #define STATUS_BST_BIT (1 << 3)
  26. #define STATUS_VMN_BIT (1 << 8)
  27. #define STATUS_TMN_BIT (1 << 9)
  28. #define STATUS_SMN_BIT (1 << 10)
  29. #define STATUS_BI_BIT (1 << 11)
  30. #define STATUS_VMX_BIT (1 << 12)
  31. #define STATUS_TMX_BIT (1 << 13)
  32. #define STATUS_SMX_BIT (1 << 14)
  33. #define STATUS_BR_BIT (1 << 15)
  34. /* Interrupt mask bits */
  35. #define CONFIG_ALRT_BIT_ENBL (1 << 2)
  36. #define STATUS_INTR_SOCMIN_BIT (1 << 10)
  37. #define STATUS_INTR_SOCMAX_BIT (1 << 14)
  38. #define VFSOC0_LOCK 0x0000
  39. #define VFSOC0_UNLOCK 0x0080
  40. #define MODEL_UNLOCK1 0X0059
  41. #define MODEL_UNLOCK2 0X00C4
  42. #define MODEL_LOCK1 0X0000
  43. #define MODEL_LOCK2 0X0000
  44. #define dQ_ACC_DIV 0x4
  45. #define dP_ACC_100 0x1900
  46. #define dP_ACC_200 0x3200
  47. #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
  48. struct max17042_chip {
  49. struct i2c_client *client;
  50. struct regmap *regmap;
  51. struct power_supply *battery;
  52. enum max170xx_chip_type chip_type;
  53. struct max17042_platform_data *pdata;
  54. struct work_struct work;
  55. int init_complete;
  56. };
  57. static enum power_supply_property max17042_battery_props[] = {
  58. POWER_SUPPLY_PROP_STATUS,
  59. POWER_SUPPLY_PROP_PRESENT,
  60. POWER_SUPPLY_PROP_TECHNOLOGY,
  61. POWER_SUPPLY_PROP_CYCLE_COUNT,
  62. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  63. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  64. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  65. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  66. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  67. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  68. POWER_SUPPLY_PROP_CAPACITY,
  69. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  70. POWER_SUPPLY_PROP_CHARGE_FULL,
  71. POWER_SUPPLY_PROP_CHARGE_NOW,
  72. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  73. POWER_SUPPLY_PROP_TEMP,
  74. POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
  75. POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
  76. POWER_SUPPLY_PROP_TEMP_MIN,
  77. POWER_SUPPLY_PROP_TEMP_MAX,
  78. POWER_SUPPLY_PROP_HEALTH,
  79. POWER_SUPPLY_PROP_SCOPE,
  80. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  81. // these two have to be at the end on the list
  82. POWER_SUPPLY_PROP_CURRENT_NOW,
  83. POWER_SUPPLY_PROP_CURRENT_AVG,
  84. };
  85. static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
  86. {
  87. int ret;
  88. u32 data;
  89. struct regmap *map = chip->regmap;
  90. ret = regmap_read(map, MAX17042_TEMP, &data);
  91. if (ret < 0)
  92. return ret;
  93. *temp = sign_extend32(data, 15);
  94. /* The value is converted into deci-centigrade scale */
  95. /* Units of LSB = 1 / 256 degree Celsius */
  96. *temp = *temp * 10 / 256;
  97. return 0;
  98. }
  99. static int max17042_get_status(struct max17042_chip *chip, int *status)
  100. {
  101. int ret, charge_full, charge_now;
  102. int avg_current;
  103. u32 data;
  104. ret = power_supply_am_i_supplied(chip->battery);
  105. if (ret < 0) {
  106. *status = POWER_SUPPLY_STATUS_UNKNOWN;
  107. return 0;
  108. }
  109. if (ret == 0) {
  110. *status = POWER_SUPPLY_STATUS_DISCHARGING;
  111. return 0;
  112. }
  113. /*
  114. * The MAX170xx has builtin end-of-charge detection and will update
  115. * FullCAP to match RepCap when it detects end of charging.
  116. *
  117. * When this cycle the battery gets charged to a higher (calculated)
  118. * capacity then the previous cycle then FullCAP will get updated
  119. * contineously once end-of-charge detection kicks in, so allow the
  120. * 2 to differ a bit.
  121. */
  122. ret = regmap_read(chip->regmap, MAX17042_FullCAP, &charge_full);
  123. if (ret < 0)
  124. return ret;
  125. ret = regmap_read(chip->regmap, MAX17042_RepCap, &charge_now);
  126. if (ret < 0)
  127. return ret;
  128. if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) {
  129. *status = POWER_SUPPLY_STATUS_FULL;
  130. return 0;
  131. }
  132. /*
  133. * Even though we are supplied, we may still be discharging if the
  134. * supply is e.g. only delivering 5V 0.5A. Check current if available.
  135. */
  136. if (!chip->pdata->enable_current_sense) {
  137. *status = POWER_SUPPLY_STATUS_CHARGING;
  138. return 0;
  139. }
  140. ret = regmap_read(chip->regmap, MAX17042_AvgCurrent, &data);
  141. if (ret < 0)
  142. return ret;
  143. avg_current = sign_extend32(data, 15);
  144. avg_current *= 1562500 / chip->pdata->r_sns;
  145. if (avg_current > 0)
  146. *status = POWER_SUPPLY_STATUS_CHARGING;
  147. else
  148. *status = POWER_SUPPLY_STATUS_DISCHARGING;
  149. return 0;
  150. }
  151. static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
  152. {
  153. int temp, vavg, vbatt, ret;
  154. u32 val;
  155. ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val);
  156. if (ret < 0)
  157. goto health_error;
  158. /* bits [0-3] unused */
  159. vavg = val * 625 / 8;
  160. /* Convert to millivolts */
  161. vavg /= 1000;
  162. ret = regmap_read(chip->regmap, MAX17042_VCELL, &val);
  163. if (ret < 0)
  164. goto health_error;
  165. /* bits [0-3] unused */
  166. vbatt = val * 625 / 8;
  167. /* Convert to millivolts */
  168. vbatt /= 1000;
  169. if (vavg < chip->pdata->vmin) {
  170. *health = POWER_SUPPLY_HEALTH_DEAD;
  171. goto out;
  172. }
  173. if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) {
  174. *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  175. goto out;
  176. }
  177. ret = max17042_get_temperature(chip, &temp);
  178. if (ret < 0)
  179. goto health_error;
  180. if (temp < chip->pdata->temp_min) {
  181. *health = POWER_SUPPLY_HEALTH_COLD;
  182. goto out;
  183. }
  184. if (temp > chip->pdata->temp_max) {
  185. *health = POWER_SUPPLY_HEALTH_OVERHEAT;
  186. goto out;
  187. }
  188. *health = POWER_SUPPLY_HEALTH_GOOD;
  189. out:
  190. return 0;
  191. health_error:
  192. return ret;
  193. }
  194. static int max17042_get_property(struct power_supply *psy,
  195. enum power_supply_property psp,
  196. union power_supply_propval *val)
  197. {
  198. struct max17042_chip *chip = power_supply_get_drvdata(psy);
  199. struct regmap *map = chip->regmap;
  200. int ret;
  201. u32 data;
  202. u64 data64;
  203. if (!chip->init_complete)
  204. return -EAGAIN;
  205. switch (psp) {
  206. case POWER_SUPPLY_PROP_STATUS:
  207. ret = max17042_get_status(chip, &val->intval);
  208. if (ret < 0)
  209. return ret;
  210. break;
  211. case POWER_SUPPLY_PROP_PRESENT:
  212. ret = regmap_read(map, MAX17042_STATUS, &data);
  213. if (ret < 0)
  214. return ret;
  215. if (data & MAX17042_STATUS_BattAbsent)
  216. val->intval = 0;
  217. else
  218. val->intval = 1;
  219. break;
  220. case POWER_SUPPLY_PROP_TECHNOLOGY:
  221. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  222. break;
  223. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  224. ret = regmap_read(map, MAX17042_Cycles, &data);
  225. if (ret < 0)
  226. return ret;
  227. val->intval = data;
  228. break;
  229. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  230. ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
  231. if (ret < 0)
  232. return ret;
  233. val->intval = data >> 8;
  234. val->intval *= 20000; /* Units of LSB = 20mV */
  235. break;
  236. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  237. ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
  238. if (ret < 0)
  239. return ret;
  240. val->intval = (data & 0xff) * 20000; /* Units of 20mV */
  241. break;
  242. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  243. if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
  244. ret = regmap_read(map, MAX17042_V_empty, &data);
  245. else if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055)
  246. ret = regmap_read(map, MAX17055_V_empty, &data);
  247. else
  248. ret = regmap_read(map, MAX17047_V_empty, &data);
  249. if (ret < 0)
  250. return ret;
  251. val->intval = data >> 7;
  252. val->intval *= 10000; /* Units of LSB = 10mV */
  253. break;
  254. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  255. ret = regmap_read(map, MAX17042_VCELL, &data);
  256. if (ret < 0)
  257. return ret;
  258. val->intval = data * 625 / 8;
  259. break;
  260. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  261. ret = regmap_read(map, MAX17042_AvgVCELL, &data);
  262. if (ret < 0)
  263. return ret;
  264. val->intval = data * 625 / 8;
  265. break;
  266. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  267. ret = regmap_read(map, MAX17042_OCVInternal, &data);
  268. if (ret < 0)
  269. return ret;
  270. val->intval = data * 625 / 8;
  271. break;
  272. case POWER_SUPPLY_PROP_CAPACITY:
  273. if (chip->pdata->enable_current_sense)
  274. ret = regmap_read(map, MAX17042_RepSOC, &data);
  275. else
  276. ret = regmap_read(map, MAX17042_VFSOC, &data);
  277. if (ret < 0)
  278. return ret;
  279. val->intval = data >> 8;
  280. break;
  281. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  282. ret = regmap_read(map, MAX17042_DesignCap, &data);
  283. if (ret < 0)
  284. return ret;
  285. data64 = data * 5000000ll;
  286. do_div(data64, chip->pdata->r_sns);
  287. val->intval = data64;
  288. break;
  289. case POWER_SUPPLY_PROP_CHARGE_FULL:
  290. ret = regmap_read(map, MAX17042_FullCAP, &data);
  291. if (ret < 0)
  292. return ret;
  293. data64 = data * 5000000ll;
  294. do_div(data64, chip->pdata->r_sns);
  295. val->intval = data64;
  296. break;
  297. case POWER_SUPPLY_PROP_CHARGE_NOW:
  298. ret = regmap_read(map, MAX17042_RepCap, &data);
  299. if (ret < 0)
  300. return ret;
  301. data64 = data * 5000000ll;
  302. do_div(data64, chip->pdata->r_sns);
  303. val->intval = data64;
  304. break;
  305. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  306. ret = regmap_read(map, MAX17042_QH, &data);
  307. if (ret < 0)
  308. return ret;
  309. val->intval = data * 1000 / 2;
  310. break;
  311. case POWER_SUPPLY_PROP_TEMP:
  312. ret = max17042_get_temperature(chip, &val->intval);
  313. if (ret < 0)
  314. return ret;
  315. break;
  316. case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  317. ret = regmap_read(map, MAX17042_TALRT_Th, &data);
  318. if (ret < 0)
  319. return ret;
  320. /* LSB is Alert Minimum. In deci-centigrade */
  321. val->intval = sign_extend32(data & 0xff, 7) * 10;
  322. break;
  323. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  324. ret = regmap_read(map, MAX17042_TALRT_Th, &data);
  325. if (ret < 0)
  326. return ret;
  327. /* MSB is Alert Maximum. In deci-centigrade */
  328. val->intval = sign_extend32(data >> 8, 7) * 10;
  329. break;
  330. case POWER_SUPPLY_PROP_TEMP_MIN:
  331. val->intval = chip->pdata->temp_min;
  332. break;
  333. case POWER_SUPPLY_PROP_TEMP_MAX:
  334. val->intval = chip->pdata->temp_max;
  335. break;
  336. case POWER_SUPPLY_PROP_HEALTH:
  337. ret = max17042_get_battery_health(chip, &val->intval);
  338. if (ret < 0)
  339. return ret;
  340. break;
  341. case POWER_SUPPLY_PROP_SCOPE:
  342. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  343. break;
  344. case POWER_SUPPLY_PROP_CURRENT_NOW:
  345. if (chip->pdata->enable_current_sense) {
  346. ret = regmap_read(map, MAX17042_Current, &data);
  347. if (ret < 0)
  348. return ret;
  349. val->intval = sign_extend32(data, 15);
  350. val->intval *= 1562500 / chip->pdata->r_sns;
  351. } else {
  352. return -EINVAL;
  353. }
  354. break;
  355. case POWER_SUPPLY_PROP_CURRENT_AVG:
  356. if (chip->pdata->enable_current_sense) {
  357. ret = regmap_read(map, MAX17042_AvgCurrent, &data);
  358. if (ret < 0)
  359. return ret;
  360. val->intval = sign_extend32(data, 15);
  361. val->intval *= 1562500 / chip->pdata->r_sns;
  362. } else {
  363. return -EINVAL;
  364. }
  365. break;
  366. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  367. ret = regmap_read(map, MAX17042_TTE, &data);
  368. if (ret < 0)
  369. return ret;
  370. val->intval = data * 5625 / 1000;
  371. break;
  372. default:
  373. return -EINVAL;
  374. }
  375. return 0;
  376. }
  377. static int max17042_set_property(struct power_supply *psy,
  378. enum power_supply_property psp,
  379. const union power_supply_propval *val)
  380. {
  381. struct max17042_chip *chip = power_supply_get_drvdata(psy);
  382. struct regmap *map = chip->regmap;
  383. int ret = 0;
  384. u32 data;
  385. int8_t temp;
  386. switch (psp) {
  387. case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  388. ret = regmap_read(map, MAX17042_TALRT_Th, &data);
  389. if (ret < 0)
  390. return ret;
  391. /* Input in deci-centigrade, convert to centigrade */
  392. temp = val->intval / 10;
  393. /* force min < max */
  394. if (temp >= (int8_t)(data >> 8))
  395. temp = (int8_t)(data >> 8) - 1;
  396. /* Write both MAX and MIN ALERT */
  397. data = (data & 0xff00) + temp;
  398. ret = regmap_write(map, MAX17042_TALRT_Th, data);
  399. break;
  400. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  401. ret = regmap_read(map, MAX17042_TALRT_Th, &data);
  402. if (ret < 0)
  403. return ret;
  404. /* Input in Deci-Centigrade, convert to centigrade */
  405. temp = val->intval / 10;
  406. /* force max > min */
  407. if (temp <= (int8_t)(data & 0xff))
  408. temp = (int8_t)(data & 0xff) + 1;
  409. /* Write both MAX and MIN ALERT */
  410. data = (data & 0xff) + (temp << 8);
  411. ret = regmap_write(map, MAX17042_TALRT_Th, data);
  412. break;
  413. default:
  414. ret = -EINVAL;
  415. }
  416. return ret;
  417. }
  418. static int max17042_property_is_writeable(struct power_supply *psy,
  419. enum power_supply_property psp)
  420. {
  421. int ret;
  422. switch (psp) {
  423. case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  424. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  425. ret = 1;
  426. break;
  427. default:
  428. ret = 0;
  429. }
  430. return ret;
  431. }
  432. static void max17042_external_power_changed(struct power_supply *psy)
  433. {
  434. power_supply_changed(psy);
  435. }
  436. static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
  437. {
  438. int retries = 8;
  439. int ret;
  440. u32 read_value;
  441. do {
  442. ret = regmap_write(map, reg, value);
  443. regmap_read(map, reg, &read_value);
  444. if (read_value != value) {
  445. ret = -EIO;
  446. retries--;
  447. }
  448. } while (retries && read_value != value);
  449. if (ret < 0)
  450. pr_err("%s: err %d\n", __func__, ret);
  451. return ret;
  452. }
  453. static inline void max17042_override_por(struct regmap *map,
  454. u8 reg, u16 value)
  455. {
  456. if (value)
  457. regmap_write(map, reg, value);
  458. }
  459. static inline void max17042_unlock_model(struct max17042_chip *chip)
  460. {
  461. struct regmap *map = chip->regmap;
  462. regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
  463. regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
  464. }
  465. static inline void max17042_lock_model(struct max17042_chip *chip)
  466. {
  467. struct regmap *map = chip->regmap;
  468. regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
  469. regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
  470. }
  471. static inline void max17042_write_model_data(struct max17042_chip *chip,
  472. u8 addr, int size)
  473. {
  474. struct regmap *map = chip->regmap;
  475. int i;
  476. for (i = 0; i < size; i++)
  477. regmap_write(map, addr + i,
  478. chip->pdata->config_data->cell_char_tbl[i]);
  479. }
  480. static inline void max17042_read_model_data(struct max17042_chip *chip,
  481. u8 addr, u16 *data, int size)
  482. {
  483. struct regmap *map = chip->regmap;
  484. int i;
  485. u32 tmp;
  486. for (i = 0; i < size; i++) {
  487. regmap_read(map, addr + i, &tmp);
  488. data[i] = (u16)tmp;
  489. }
  490. }
  491. static inline int max17042_model_data_compare(struct max17042_chip *chip,
  492. u16 *data1, u16 *data2, int size)
  493. {
  494. int i;
  495. if (memcmp(data1, data2, size)) {
  496. dev_err(&chip->client->dev, "%s compare failed\n", __func__);
  497. for (i = 0; i < size; i++)
  498. dev_info(&chip->client->dev, "0x%x, 0x%x",
  499. data1[i], data2[i]);
  500. dev_info(&chip->client->dev, "\n");
  501. return -EINVAL;
  502. }
  503. return 0;
  504. }
  505. static int max17042_init_model(struct max17042_chip *chip)
  506. {
  507. int ret;
  508. int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
  509. u16 *temp_data;
  510. temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
  511. if (!temp_data)
  512. return -ENOMEM;
  513. max17042_unlock_model(chip);
  514. max17042_write_model_data(chip, MAX17042_MODELChrTbl,
  515. table_size);
  516. max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
  517. table_size);
  518. ret = max17042_model_data_compare(
  519. chip,
  520. chip->pdata->config_data->cell_char_tbl,
  521. temp_data,
  522. table_size);
  523. max17042_lock_model(chip);
  524. kfree(temp_data);
  525. return ret;
  526. }
  527. static int max17042_verify_model_lock(struct max17042_chip *chip)
  528. {
  529. int i;
  530. int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
  531. u16 *temp_data;
  532. int ret = 0;
  533. temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
  534. if (!temp_data)
  535. return -ENOMEM;
  536. max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
  537. table_size);
  538. for (i = 0; i < table_size; i++)
  539. if (temp_data[i])
  540. ret = -EINVAL;
  541. kfree(temp_data);
  542. return ret;
  543. }
  544. static void max17042_write_config_regs(struct max17042_chip *chip)
  545. {
  546. struct max17042_config_data *config = chip->pdata->config_data;
  547. struct regmap *map = chip->regmap;
  548. regmap_write(map, MAX17042_CONFIG, config->config);
  549. regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
  550. regmap_write(map, MAX17042_FilterCFG,
  551. config->filter_cfg);
  552. regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
  553. if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 ||
  554. chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050 ||
  555. chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055)
  556. regmap_write(map, MAX17047_FullSOCThr,
  557. config->full_soc_thresh);
  558. }
  559. static void max17042_write_custom_regs(struct max17042_chip *chip)
  560. {
  561. struct max17042_config_data *config = chip->pdata->config_data;
  562. struct regmap *map = chip->regmap;
  563. max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
  564. max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0);
  565. max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
  566. if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) {
  567. regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco);
  568. max17042_write_verify_reg(map, MAX17042_K_empty0,
  569. config->kempty0);
  570. } else {
  571. max17042_write_verify_reg(map, MAX17047_QRTbl00,
  572. config->qrtbl00);
  573. max17042_write_verify_reg(map, MAX17047_QRTbl10,
  574. config->qrtbl10);
  575. max17042_write_verify_reg(map, MAX17047_QRTbl20,
  576. config->qrtbl20);
  577. max17042_write_verify_reg(map, MAX17047_QRTbl30,
  578. config->qrtbl30);
  579. }
  580. }
  581. static void max17042_update_capacity_regs(struct max17042_chip *chip)
  582. {
  583. struct max17042_config_data *config = chip->pdata->config_data;
  584. struct regmap *map = chip->regmap;
  585. max17042_write_verify_reg(map, MAX17042_FullCAP,
  586. config->fullcap);
  587. regmap_write(map, MAX17042_DesignCap, config->design_cap);
  588. max17042_write_verify_reg(map, MAX17042_FullCAPNom,
  589. config->fullcapnom);
  590. }
  591. static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
  592. {
  593. unsigned int vfSoc;
  594. struct regmap *map = chip->regmap;
  595. regmap_read(map, MAX17042_VFSOC, &vfSoc);
  596. regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
  597. max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
  598. regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
  599. }
  600. static void max17042_load_new_capacity_params(struct max17042_chip *chip)
  601. {
  602. u32 full_cap0, rep_cap, dq_acc, vfSoc;
  603. u32 rem_cap;
  604. struct max17042_config_data *config = chip->pdata->config_data;
  605. struct regmap *map = chip->regmap;
  606. regmap_read(map, MAX17042_FullCAP0, &full_cap0);
  607. regmap_read(map, MAX17042_VFSOC, &vfSoc);
  608. /* fg_vfSoc needs to shifted by 8 bits to get the
  609. * perc in 1% accuracy, to get the right rem_cap multiply
  610. * full_cap0, fg_vfSoc and devide by 100
  611. */
  612. rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
  613. max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
  614. rep_cap = rem_cap;
  615. max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
  616. /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
  617. dq_acc = config->fullcap / dQ_ACC_DIV;
  618. max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
  619. max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
  620. max17042_write_verify_reg(map, MAX17042_FullCAP,
  621. config->fullcap);
  622. regmap_write(map, MAX17042_DesignCap,
  623. config->design_cap);
  624. max17042_write_verify_reg(map, MAX17042_FullCAPNom,
  625. config->fullcapnom);
  626. /* Update SOC register with new SOC */
  627. regmap_write(map, MAX17042_RepSOC, vfSoc);
  628. }
  629. /*
  630. * Block write all the override values coming from platform data.
  631. * This function MUST be called before the POR initialization proceedure
  632. * specified by maxim.
  633. */
  634. static inline void max17042_override_por_values(struct max17042_chip *chip)
  635. {
  636. struct regmap *map = chip->regmap;
  637. struct max17042_config_data *config = chip->pdata->config_data;
  638. max17042_override_por(map, MAX17042_TGAIN, config->tgain);
  639. max17042_override_por(map, MAX17042_TOFF, config->toff);
  640. max17042_override_por(map, MAX17042_CGAIN, config->cgain);
  641. max17042_override_por(map, MAX17042_COFF, config->coff);
  642. max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
  643. max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
  644. max17042_override_por(map, MAX17042_SALRT_Th,
  645. config->soc_alrt_thresh);
  646. max17042_override_por(map, MAX17042_CONFIG, config->config);
  647. max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
  648. max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
  649. max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
  650. max17042_override_por(map, MAX17042_AtRate, config->at_rate);
  651. max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
  652. max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
  653. max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
  654. max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
  655. max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
  656. max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
  657. max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
  658. if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
  659. max17042_override_por(map, MAX17042_SOC_empty,
  660. config->socempty);
  661. max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
  662. max17042_override_por(map, MAX17042_dQacc, config->dqacc);
  663. max17042_override_por(map, MAX17042_dPacc, config->dpacc);
  664. if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
  665. max17042_override_por(map, MAX17042_V_empty, config->vempty);
  666. if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17055)
  667. max17042_override_por(map, MAX17055_V_empty, config->vempty);
  668. else
  669. max17042_override_por(map, MAX17047_V_empty, config->vempty);
  670. max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
  671. max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
  672. max17042_override_por(map, MAX17042_FCTC, config->fctc);
  673. max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
  674. max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
  675. if (chip->chip_type &&
  676. ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) ||
  677. (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) ||
  678. (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050))) {
  679. max17042_override_por(map, MAX17042_EmptyTempCo,
  680. config->empty_tempco);
  681. max17042_override_por(map, MAX17042_K_empty0,
  682. config->kempty0);
  683. }
  684. }
  685. static int max17042_init_chip(struct max17042_chip *chip)
  686. {
  687. struct regmap *map = chip->regmap;
  688. int ret;
  689. max17042_override_por_values(chip);
  690. /* After Power up, the MAX17042 requires 500mS in order
  691. * to perform signal debouncing and initial SOC reporting
  692. */
  693. msleep(500);
  694. /* Initialize configaration */
  695. max17042_write_config_regs(chip);
  696. /* write cell characterization data */
  697. ret = max17042_init_model(chip);
  698. if (ret) {
  699. dev_err(&chip->client->dev, "%s init failed\n",
  700. __func__);
  701. return -EIO;
  702. }
  703. ret = max17042_verify_model_lock(chip);
  704. if (ret) {
  705. dev_err(&chip->client->dev, "%s lock verify failed\n",
  706. __func__);
  707. return -EIO;
  708. }
  709. /* write custom parameters */
  710. max17042_write_custom_regs(chip);
  711. /* update capacity params */
  712. max17042_update_capacity_regs(chip);
  713. /* delay must be atleast 350mS to allow VFSOC
  714. * to be calculated from the new configuration
  715. */
  716. msleep(350);
  717. /* reset vfsoc0 reg */
  718. max17042_reset_vfsoc0_reg(chip);
  719. /* load new capacity params */
  720. max17042_load_new_capacity_params(chip);
  721. /* Init complete, Clear the POR bit */
  722. regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0);
  723. return 0;
  724. }
  725. static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
  726. {
  727. struct regmap *map = chip->regmap;
  728. u32 soc, soc_tr;
  729. /* program interrupt thesholds such that we should
  730. * get interrupt for every 'off' perc change in the soc
  731. */
  732. regmap_read(map, MAX17042_RepSOC, &soc);
  733. soc >>= 8;
  734. soc_tr = (soc + off) << 8;
  735. if (off < soc)
  736. soc_tr |= soc - off;
  737. regmap_write(map, MAX17042_SALRT_Th, soc_tr);
  738. }
  739. static irqreturn_t max17042_thread_handler(int id, void *dev)
  740. {
  741. struct max17042_chip *chip = dev;
  742. u32 val;
  743. int ret;
  744. ret = regmap_read(chip->regmap, MAX17042_STATUS, &val);
  745. if (ret)
  746. return IRQ_HANDLED;
  747. if ((val & STATUS_INTR_SOCMIN_BIT) ||
  748. (val & STATUS_INTR_SOCMAX_BIT)) {
  749. dev_info(&chip->client->dev, "SOC threshold INTR\n");
  750. max17042_set_soc_threshold(chip, 1);
  751. }
  752. /* we implicitly handle all alerts via power_supply_changed */
  753. regmap_clear_bits(chip->regmap, MAX17042_STATUS,
  754. 0xFFFF & ~(STATUS_POR_BIT | STATUS_BST_BIT));
  755. power_supply_changed(chip->battery);
  756. return IRQ_HANDLED;
  757. }
  758. static void max17042_init_worker(struct work_struct *work)
  759. {
  760. struct max17042_chip *chip = container_of(work,
  761. struct max17042_chip, work);
  762. int ret;
  763. /* Initialize registers according to values from the platform data */
  764. if (chip->pdata->enable_por_init && chip->pdata->config_data) {
  765. ret = max17042_init_chip(chip);
  766. if (ret)
  767. return;
  768. }
  769. chip->init_complete = 1;
  770. }
  771. #ifdef CONFIG_OF
  772. static struct max17042_platform_data *
  773. max17042_get_of_pdata(struct max17042_chip *chip)
  774. {
  775. struct device *dev = &chip->client->dev;
  776. struct device_node *np = dev->of_node;
  777. u32 prop;
  778. struct max17042_platform_data *pdata;
  779. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  780. if (!pdata)
  781. return NULL;
  782. /*
  783. * Require current sense resistor value to be specified for
  784. * current-sense functionality to be enabled at all.
  785. */
  786. if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
  787. pdata->r_sns = prop;
  788. pdata->enable_current_sense = true;
  789. }
  790. if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min))
  791. pdata->temp_min = INT_MIN;
  792. if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max))
  793. pdata->temp_max = INT_MAX;
  794. if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin))
  795. pdata->vmin = INT_MIN;
  796. if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax))
  797. pdata->vmax = INT_MAX;
  798. return pdata;
  799. }
  800. #endif
  801. static struct max17042_reg_data max17047_default_pdata_init_regs[] = {
  802. /*
  803. * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
  804. * when the voltage FG reports 95%, as recommended in the datasheet.
  805. */
  806. { MAX17047_FullSOCThr, MAX17042_BATTERY_FULL << 8 },
  807. };
  808. static struct max17042_platform_data *
  809. max17042_get_default_pdata(struct max17042_chip *chip)
  810. {
  811. struct device *dev = &chip->client->dev;
  812. struct max17042_platform_data *pdata;
  813. int ret, misc_cfg;
  814. /*
  815. * The MAX17047 gets used on x86 where we might not have pdata, assume
  816. * the firmware will already have initialized the fuel-gauge and provide
  817. * default values for the non init bits to make things work.
  818. */
  819. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  820. if (!pdata)
  821. return pdata;
  822. if ((chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047) ||
  823. (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)) {
  824. pdata->init_data = max17047_default_pdata_init_regs;
  825. pdata->num_init_data =
  826. ARRAY_SIZE(max17047_default_pdata_init_regs);
  827. }
  828. ret = regmap_read(chip->regmap, MAX17042_MiscCFG, &misc_cfg);
  829. if (ret < 0)
  830. return NULL;
  831. /* If bits 0-1 are set to 3 then only Voltage readings are used */
  832. if ((misc_cfg & 0x3) == 0x3)
  833. pdata->enable_current_sense = false;
  834. else
  835. pdata->enable_current_sense = true;
  836. pdata->vmin = MAX17042_DEFAULT_VMIN;
  837. pdata->vmax = MAX17042_DEFAULT_VMAX;
  838. pdata->temp_min = MAX17042_DEFAULT_TEMP_MIN;
  839. pdata->temp_max = MAX17042_DEFAULT_TEMP_MAX;
  840. return pdata;
  841. }
  842. static struct max17042_platform_data *
  843. max17042_get_pdata(struct max17042_chip *chip)
  844. {
  845. struct device *dev = &chip->client->dev;
  846. #ifdef CONFIG_OF
  847. if (dev->of_node)
  848. return max17042_get_of_pdata(chip);
  849. #endif
  850. if (dev->platform_data)
  851. return dev->platform_data;
  852. return max17042_get_default_pdata(chip);
  853. }
  854. static const struct regmap_config max17042_regmap_config = {
  855. .reg_bits = 8,
  856. .val_bits = 16,
  857. .val_format_endian = REGMAP_ENDIAN_NATIVE,
  858. };
  859. static const struct power_supply_desc max17042_psy_desc = {
  860. .name = "max170xx_battery",
  861. .type = POWER_SUPPLY_TYPE_BATTERY,
  862. .get_property = max17042_get_property,
  863. .set_property = max17042_set_property,
  864. .property_is_writeable = max17042_property_is_writeable,
  865. .external_power_changed = max17042_external_power_changed,
  866. .properties = max17042_battery_props,
  867. .num_properties = ARRAY_SIZE(max17042_battery_props),
  868. };
  869. static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
  870. .name = "max170xx_battery",
  871. .type = POWER_SUPPLY_TYPE_BATTERY,
  872. .get_property = max17042_get_property,
  873. .set_property = max17042_set_property,
  874. .property_is_writeable = max17042_property_is_writeable,
  875. .properties = max17042_battery_props,
  876. .num_properties = ARRAY_SIZE(max17042_battery_props) - 2,
  877. };
  878. static void max17042_stop_work(void *data)
  879. {
  880. struct max17042_chip *chip = data;
  881. cancel_work_sync(&chip->work);
  882. }
  883. static int max17042_probe(struct i2c_client *client,
  884. const struct i2c_device_id *id)
  885. {
  886. struct i2c_adapter *adapter = client->adapter;
  887. const struct power_supply_desc *max17042_desc = &max17042_psy_desc;
  888. struct power_supply_config psy_cfg = {};
  889. const struct acpi_device_id *acpi_id = NULL;
  890. struct device *dev = &client->dev;
  891. struct max17042_chip *chip;
  892. int ret;
  893. int i;
  894. u32 val;
  895. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  896. return -EIO;
  897. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  898. if (!chip)
  899. return -ENOMEM;
  900. chip->client = client;
  901. if (id) {
  902. chip->chip_type = id->driver_data;
  903. } else {
  904. acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
  905. if (!acpi_id)
  906. return -ENODEV;
  907. chip->chip_type = acpi_id->driver_data;
  908. }
  909. chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
  910. if (IS_ERR(chip->regmap)) {
  911. dev_err(&client->dev, "Failed to initialize regmap\n");
  912. return -EINVAL;
  913. }
  914. chip->pdata = max17042_get_pdata(chip);
  915. if (!chip->pdata) {
  916. dev_err(&client->dev, "no platform data provided\n");
  917. return -EINVAL;
  918. }
  919. i2c_set_clientdata(client, chip);
  920. psy_cfg.drv_data = chip;
  921. psy_cfg.of_node = dev->of_node;
  922. /* When current is not measured,
  923. * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
  924. if (!chip->pdata->enable_current_sense)
  925. max17042_desc = &max17042_no_current_sense_psy_desc;
  926. if (chip->pdata->r_sns == 0)
  927. chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
  928. if (chip->pdata->init_data)
  929. for (i = 0; i < chip->pdata->num_init_data; i++)
  930. regmap_write(chip->regmap,
  931. chip->pdata->init_data[i].addr,
  932. chip->pdata->init_data[i].data);
  933. if (!chip->pdata->enable_current_sense) {
  934. regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
  935. regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
  936. regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
  937. }
  938. chip->battery = devm_power_supply_register(&client->dev, max17042_desc,
  939. &psy_cfg);
  940. if (IS_ERR(chip->battery)) {
  941. dev_err(&client->dev, "failed: power supply register\n");
  942. return PTR_ERR(chip->battery);
  943. }
  944. if (client->irq) {
  945. unsigned int flags = IRQF_ONESHOT;
  946. /*
  947. * On ACPI systems the IRQ may be handled by ACPI-event code,
  948. * so we need to share (if the ACPI code is willing to share).
  949. */
  950. if (acpi_id)
  951. flags |= IRQF_SHARED | IRQF_PROBE_SHARED;
  952. ret = devm_request_threaded_irq(&client->dev, client->irq,
  953. NULL,
  954. max17042_thread_handler, flags,
  955. chip->battery->desc->name,
  956. chip);
  957. if (!ret) {
  958. regmap_update_bits(chip->regmap, MAX17042_CONFIG,
  959. CONFIG_ALRT_BIT_ENBL,
  960. CONFIG_ALRT_BIT_ENBL);
  961. max17042_set_soc_threshold(chip, 1);
  962. } else {
  963. client->irq = 0;
  964. if (ret != -EBUSY)
  965. dev_err(&client->dev, "Failed to get IRQ\n");
  966. }
  967. }
  968. /* Not able to update the charge threshold when exceeded? -> disable */
  969. if (!client->irq)
  970. regmap_write(chip->regmap, MAX17042_SALRT_Th, 0xff00);
  971. regmap_read(chip->regmap, MAX17042_STATUS, &val);
  972. if (val & STATUS_POR_BIT) {
  973. INIT_WORK(&chip->work, max17042_init_worker);
  974. ret = devm_add_action(&client->dev, max17042_stop_work, chip);
  975. if (ret)
  976. return ret;
  977. schedule_work(&chip->work);
  978. } else {
  979. chip->init_complete = 1;
  980. }
  981. return 0;
  982. }
  983. #ifdef CONFIG_PM_SLEEP
  984. static int max17042_suspend(struct device *dev)
  985. {
  986. struct max17042_chip *chip = dev_get_drvdata(dev);
  987. /*
  988. * disable the irq and enable irq_wake
  989. * capability to the interrupt line.
  990. */
  991. if (chip->client->irq) {
  992. disable_irq(chip->client->irq);
  993. enable_irq_wake(chip->client->irq);
  994. }
  995. return 0;
  996. }
  997. static int max17042_resume(struct device *dev)
  998. {
  999. struct max17042_chip *chip = dev_get_drvdata(dev);
  1000. if (chip->client->irq) {
  1001. disable_irq_wake(chip->client->irq);
  1002. enable_irq(chip->client->irq);
  1003. /* re-program the SOC thresholds to 1% change */
  1004. max17042_set_soc_threshold(chip, 1);
  1005. }
  1006. return 0;
  1007. }
  1008. #endif
  1009. static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
  1010. max17042_resume);
  1011. #ifdef CONFIG_ACPI
  1012. static const struct acpi_device_id max17042_acpi_match[] = {
  1013. { "MAX17047", MAXIM_DEVICE_TYPE_MAX17047 },
  1014. { }
  1015. };
  1016. MODULE_DEVICE_TABLE(acpi, max17042_acpi_match);
  1017. #endif
  1018. #ifdef CONFIG_OF
  1019. static const struct of_device_id max17042_dt_match[] = {
  1020. { .compatible = "maxim,max17042" },
  1021. { .compatible = "maxim,max17047" },
  1022. { .compatible = "maxim,max17050" },
  1023. { .compatible = "maxim,max17055" },
  1024. { },
  1025. };
  1026. MODULE_DEVICE_TABLE(of, max17042_dt_match);
  1027. #endif
  1028. static const struct i2c_device_id max17042_id[] = {
  1029. { "max17042", MAXIM_DEVICE_TYPE_MAX17042 },
  1030. { "max17047", MAXIM_DEVICE_TYPE_MAX17047 },
  1031. { "max17050", MAXIM_DEVICE_TYPE_MAX17050 },
  1032. { "max17055", MAXIM_DEVICE_TYPE_MAX17055 },
  1033. { }
  1034. };
  1035. MODULE_DEVICE_TABLE(i2c, max17042_id);
  1036. static struct i2c_driver max17042_i2c_driver = {
  1037. .driver = {
  1038. .name = "max17042",
  1039. .acpi_match_table = ACPI_PTR(max17042_acpi_match),
  1040. .of_match_table = of_match_ptr(max17042_dt_match),
  1041. .pm = &max17042_pm_ops,
  1042. },
  1043. .probe = max17042_probe,
  1044. .id_table = max17042_id,
  1045. };
  1046. module_i2c_driver(max17042_i2c_driver);
  1047. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1048. MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
  1049. MODULE_LICENSE("GPL");