ds2781_battery.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
  4. *
  5. * Author: Renata Sayakhova <renata@oktetlabs.ru>
  6. *
  7. * Based on ds2780_battery drivers
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/param.h>
  12. #include <linux/pm.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/power_supply.h>
  15. #include <linux/idr.h>
  16. #include <linux/w1.h>
  17. #include "../../w1/slaves/w1_ds2781.h"
  18. /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
  19. #define DS2781_CURRENT_UNITS 1563
  20. /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
  21. #define DS2781_CHARGE_UNITS 6250
  22. /* Number of bytes in user EEPROM space */
  23. #define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \
  24. DS2781_EEPROM_BLOCK0_START + 1)
  25. /* Number of bytes in parameter EEPROM space */
  26. #define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \
  27. DS2781_EEPROM_BLOCK1_START + 1)
  28. struct ds2781_device_info {
  29. struct device *dev;
  30. struct power_supply *bat;
  31. struct power_supply_desc bat_desc;
  32. struct device *w1_dev;
  33. };
  34. enum current_types {
  35. CURRENT_NOW,
  36. CURRENT_AVG,
  37. };
  38. static const char model[] = "DS2781";
  39. static const char manufacturer[] = "Maxim/Dallas";
  40. static inline struct ds2781_device_info *
  41. to_ds2781_device_info(struct power_supply *psy)
  42. {
  43. return power_supply_get_drvdata(psy);
  44. }
  45. static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
  46. char *buf, int addr, size_t count, int io)
  47. {
  48. return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
  49. }
  50. static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
  51. int addr, size_t count)
  52. {
  53. return ds2781_battery_io(dev_info, buf, addr, count, 0);
  54. }
  55. static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
  56. int addr)
  57. {
  58. return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
  59. }
  60. static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
  61. int addr)
  62. {
  63. int ret;
  64. u8 raw[2];
  65. ret = ds2781_battery_io(dev_info, raw, addr, sizeof(raw), 0);
  66. if (ret < 0)
  67. return ret;
  68. *val = (raw[0] << 8) | raw[1];
  69. return 0;
  70. }
  71. static inline int ds2781_read_block(struct ds2781_device_info *dev_info,
  72. u8 *val, int addr, size_t count)
  73. {
  74. return ds2781_battery_io(dev_info, val, addr, count, 0);
  75. }
  76. static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
  77. int addr, size_t count)
  78. {
  79. return ds2781_battery_io(dev_info, val, addr, count, 1);
  80. }
  81. static inline int ds2781_store_eeprom(struct device *dev, int addr)
  82. {
  83. return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
  84. }
  85. static inline int ds2781_recall_eeprom(struct device *dev, int addr)
  86. {
  87. return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
  88. }
  89. static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
  90. {
  91. int ret;
  92. ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
  93. if (ret < 0)
  94. return ret;
  95. ret = ds2781_recall_eeprom(dev_info->w1_dev, reg);
  96. if (ret < 0)
  97. return ret;
  98. return 0;
  99. }
  100. /* Set sense resistor value in mhos */
  101. static int ds2781_set_sense_register(struct ds2781_device_info *dev_info,
  102. u8 conductance)
  103. {
  104. int ret;
  105. ret = ds2781_write(dev_info, &conductance,
  106. DS2781_RSNSP, sizeof(u8));
  107. if (ret < 0)
  108. return ret;
  109. return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
  110. }
  111. /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
  112. static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
  113. u16 *rsgain)
  114. {
  115. return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
  116. }
  117. /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
  118. static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
  119. u16 rsgain)
  120. {
  121. int ret;
  122. u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
  123. ret = ds2781_write(dev_info, raw,
  124. DS2781_RSGAIN_MSB, sizeof(raw));
  125. if (ret < 0)
  126. return ret;
  127. return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
  128. }
  129. static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
  130. int *voltage_uV)
  131. {
  132. int ret;
  133. char val[2];
  134. int voltage_raw;
  135. ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
  136. if (ret < 0)
  137. return ret;
  138. /*
  139. * The voltage value is located in 10 bits across the voltage MSB
  140. * and LSB registers in two's complement form
  141. * Sign bit of the voltage value is in bit 7 of the voltage MSB register
  142. * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
  143. * voltage MSB register
  144. * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
  145. * voltage LSB register
  146. */
  147. voltage_raw = (val[0] << 3) |
  148. (val[1] >> 5);
  149. /* DS2781 reports voltage in units of 9.76mV, but the battery class
  150. * reports in units of uV, so convert by multiplying by 9760. */
  151. *voltage_uV = voltage_raw * 9760;
  152. return 0;
  153. }
  154. static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
  155. int *temp)
  156. {
  157. int ret;
  158. char val[2];
  159. int temp_raw;
  160. ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
  161. if (ret < 0)
  162. return ret;
  163. /*
  164. * The temperature value is located in 10 bits across the temperature
  165. * MSB and LSB registers in two's complement form
  166. * Sign bit of the temperature value is in bit 7 of the temperature
  167. * MSB register
  168. * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
  169. * temperature MSB register
  170. * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
  171. * temperature LSB register
  172. */
  173. temp_raw = ((val[0]) << 3) |
  174. (val[1] >> 5);
  175. *temp = temp_raw + (temp_raw / 4);
  176. return 0;
  177. }
  178. static int ds2781_get_current(struct ds2781_device_info *dev_info,
  179. enum current_types type, int *current_uA)
  180. {
  181. int ret, sense_res;
  182. s16 current_raw;
  183. u8 sense_res_raw, reg_msb;
  184. /*
  185. * The units of measurement for current are dependent on the value of
  186. * the sense resistor.
  187. */
  188. ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
  189. if (ret < 0)
  190. return ret;
  191. if (sense_res_raw == 0) {
  192. dev_err(dev_info->dev, "sense resistor value is 0\n");
  193. return -EINVAL;
  194. }
  195. sense_res = 1000 / sense_res_raw;
  196. if (type == CURRENT_NOW)
  197. reg_msb = DS2781_CURRENT_MSB;
  198. else if (type == CURRENT_AVG)
  199. reg_msb = DS2781_IAVG_MSB;
  200. else
  201. return -EINVAL;
  202. /*
  203. * The current value is located in 16 bits across the current MSB
  204. * and LSB registers in two's complement form
  205. * Sign bit of the current value is in bit 7 of the current MSB register
  206. * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
  207. * MSB register
  208. * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
  209. * LSB register
  210. */
  211. ret = ds2781_read16(dev_info, &current_raw, reg_msb);
  212. if (ret < 0)
  213. return ret;
  214. *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
  215. return 0;
  216. }
  217. static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
  218. int *accumulated_current)
  219. {
  220. int ret, sense_res;
  221. s16 current_raw;
  222. u8 sense_res_raw;
  223. /*
  224. * The units of measurement for accumulated current are dependent on
  225. * the value of the sense resistor.
  226. */
  227. ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
  228. if (ret < 0)
  229. return ret;
  230. if (sense_res_raw == 0) {
  231. dev_err(dev_info->dev, "sense resistor value is 0\n");
  232. return -EINVAL;
  233. }
  234. sense_res = 1000 / sense_res_raw;
  235. /*
  236. * The ACR value is located in 16 bits across the ACR MSB and
  237. * LSB registers
  238. * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
  239. * MSB register
  240. * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
  241. * LSB register
  242. */
  243. ret = ds2781_read16(dev_info, &current_raw, DS2781_ACR_MSB);
  244. if (ret < 0)
  245. return ret;
  246. *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
  247. return 0;
  248. }
  249. static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
  250. int *capacity)
  251. {
  252. int ret;
  253. u8 raw;
  254. ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
  255. if (ret < 0)
  256. return ret;
  257. *capacity = raw;
  258. return 0;
  259. }
  260. static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
  261. {
  262. int ret, current_uA, capacity;
  263. ret = ds2781_get_current(dev_info, CURRENT_NOW, &current_uA);
  264. if (ret < 0)
  265. return ret;
  266. ret = ds2781_get_capacity(dev_info, &capacity);
  267. if (ret < 0)
  268. return ret;
  269. if (power_supply_am_i_supplied(dev_info->bat)) {
  270. if (capacity == 100)
  271. *status = POWER_SUPPLY_STATUS_FULL;
  272. else if (current_uA > 50000)
  273. *status = POWER_SUPPLY_STATUS_CHARGING;
  274. else
  275. *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  276. } else {
  277. *status = POWER_SUPPLY_STATUS_DISCHARGING;
  278. }
  279. return 0;
  280. }
  281. static int ds2781_get_charge_now(struct ds2781_device_info *dev_info,
  282. int *charge_now)
  283. {
  284. int ret;
  285. u16 charge_raw;
  286. /*
  287. * The RAAC value is located in 16 bits across the RAAC MSB and
  288. * LSB registers
  289. * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
  290. * MSB register
  291. * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
  292. * LSB register
  293. */
  294. ret = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
  295. if (ret < 0)
  296. return ret;
  297. *charge_now = charge_raw * 1600;
  298. return 0;
  299. }
  300. static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
  301. u8 *control_reg)
  302. {
  303. return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
  304. }
  305. static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
  306. u8 control_reg)
  307. {
  308. int ret;
  309. ret = ds2781_write(dev_info, &control_reg,
  310. DS2781_CONTROL, sizeof(u8));
  311. if (ret < 0)
  312. return ret;
  313. return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
  314. }
  315. static int ds2781_battery_get_property(struct power_supply *psy,
  316. enum power_supply_property psp,
  317. union power_supply_propval *val)
  318. {
  319. int ret = 0;
  320. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  321. switch (psp) {
  322. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  323. ret = ds2781_get_voltage(dev_info, &val->intval);
  324. break;
  325. case POWER_SUPPLY_PROP_TEMP:
  326. ret = ds2781_get_temperature(dev_info, &val->intval);
  327. break;
  328. case POWER_SUPPLY_PROP_MODEL_NAME:
  329. val->strval = model;
  330. break;
  331. case POWER_SUPPLY_PROP_MANUFACTURER:
  332. val->strval = manufacturer;
  333. break;
  334. case POWER_SUPPLY_PROP_CURRENT_NOW:
  335. ret = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
  336. break;
  337. case POWER_SUPPLY_PROP_CURRENT_AVG:
  338. ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
  339. break;
  340. case POWER_SUPPLY_PROP_STATUS:
  341. ret = ds2781_get_status(dev_info, &val->intval);
  342. break;
  343. case POWER_SUPPLY_PROP_CAPACITY:
  344. ret = ds2781_get_capacity(dev_info, &val->intval);
  345. break;
  346. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  347. ret = ds2781_get_accumulated_current(dev_info, &val->intval);
  348. break;
  349. case POWER_SUPPLY_PROP_CHARGE_NOW:
  350. ret = ds2781_get_charge_now(dev_info, &val->intval);
  351. break;
  352. default:
  353. ret = -EINVAL;
  354. }
  355. return ret;
  356. }
  357. static enum power_supply_property ds2781_battery_props[] = {
  358. POWER_SUPPLY_PROP_STATUS,
  359. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  360. POWER_SUPPLY_PROP_TEMP,
  361. POWER_SUPPLY_PROP_MODEL_NAME,
  362. POWER_SUPPLY_PROP_MANUFACTURER,
  363. POWER_SUPPLY_PROP_CURRENT_NOW,
  364. POWER_SUPPLY_PROP_CURRENT_AVG,
  365. POWER_SUPPLY_PROP_CAPACITY,
  366. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  367. POWER_SUPPLY_PROP_CHARGE_NOW,
  368. };
  369. static ssize_t ds2781_get_pmod_enabled(struct device *dev,
  370. struct device_attribute *attr,
  371. char *buf)
  372. {
  373. int ret;
  374. u8 control_reg;
  375. struct power_supply *psy = to_power_supply(dev);
  376. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  377. /* Get power mode */
  378. ret = ds2781_get_control_register(dev_info, &control_reg);
  379. if (ret < 0)
  380. return ret;
  381. return sprintf(buf, "%d\n",
  382. !!(control_reg & DS2781_CONTROL_PMOD));
  383. }
  384. static ssize_t ds2781_set_pmod_enabled(struct device *dev,
  385. struct device_attribute *attr,
  386. const char *buf,
  387. size_t count)
  388. {
  389. int ret;
  390. u8 control_reg, new_setting;
  391. struct power_supply *psy = to_power_supply(dev);
  392. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  393. /* Set power mode */
  394. ret = ds2781_get_control_register(dev_info, &control_reg);
  395. if (ret < 0)
  396. return ret;
  397. ret = kstrtou8(buf, 0, &new_setting);
  398. if (ret < 0)
  399. return ret;
  400. if ((new_setting != 0) && (new_setting != 1)) {
  401. dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
  402. return -EINVAL;
  403. }
  404. if (new_setting)
  405. control_reg |= DS2781_CONTROL_PMOD;
  406. else
  407. control_reg &= ~DS2781_CONTROL_PMOD;
  408. ret = ds2781_set_control_register(dev_info, control_reg);
  409. if (ret < 0)
  410. return ret;
  411. return count;
  412. }
  413. static ssize_t ds2781_get_sense_resistor_value(struct device *dev,
  414. struct device_attribute *attr,
  415. char *buf)
  416. {
  417. int ret;
  418. u8 sense_resistor;
  419. struct power_supply *psy = to_power_supply(dev);
  420. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  421. ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
  422. if (ret < 0)
  423. return ret;
  424. ret = sprintf(buf, "%d\n", sense_resistor);
  425. return ret;
  426. }
  427. static ssize_t ds2781_set_sense_resistor_value(struct device *dev,
  428. struct device_attribute *attr,
  429. const char *buf,
  430. size_t count)
  431. {
  432. int ret;
  433. u8 new_setting;
  434. struct power_supply *psy = to_power_supply(dev);
  435. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  436. ret = kstrtou8(buf, 0, &new_setting);
  437. if (ret < 0)
  438. return ret;
  439. ret = ds2781_set_sense_register(dev_info, new_setting);
  440. if (ret < 0)
  441. return ret;
  442. return count;
  443. }
  444. static ssize_t ds2781_get_rsgain_setting(struct device *dev,
  445. struct device_attribute *attr,
  446. char *buf)
  447. {
  448. int ret;
  449. u16 rsgain;
  450. struct power_supply *psy = to_power_supply(dev);
  451. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  452. ret = ds2781_get_rsgain_register(dev_info, &rsgain);
  453. if (ret < 0)
  454. return ret;
  455. return sprintf(buf, "%d\n", rsgain);
  456. }
  457. static ssize_t ds2781_set_rsgain_setting(struct device *dev,
  458. struct device_attribute *attr,
  459. const char *buf,
  460. size_t count)
  461. {
  462. int ret;
  463. u16 new_setting;
  464. struct power_supply *psy = to_power_supply(dev);
  465. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  466. ret = kstrtou16(buf, 0, &new_setting);
  467. if (ret < 0)
  468. return ret;
  469. /* Gain can only be from 0 to 1.999 in steps of .001 */
  470. if (new_setting > 1999) {
  471. dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
  472. return -EINVAL;
  473. }
  474. ret = ds2781_set_rsgain_register(dev_info, new_setting);
  475. if (ret < 0)
  476. return ret;
  477. return count;
  478. }
  479. static ssize_t ds2781_get_pio_pin(struct device *dev,
  480. struct device_attribute *attr,
  481. char *buf)
  482. {
  483. int ret;
  484. u8 sfr;
  485. struct power_supply *psy = to_power_supply(dev);
  486. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  487. ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
  488. if (ret < 0)
  489. return ret;
  490. ret = sprintf(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
  491. return ret;
  492. }
  493. static ssize_t ds2781_set_pio_pin(struct device *dev,
  494. struct device_attribute *attr,
  495. const char *buf,
  496. size_t count)
  497. {
  498. int ret;
  499. u8 new_setting;
  500. struct power_supply *psy = to_power_supply(dev);
  501. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  502. ret = kstrtou8(buf, 0, &new_setting);
  503. if (ret < 0)
  504. return ret;
  505. if ((new_setting != 0) && (new_setting != 1)) {
  506. dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
  507. return -EINVAL;
  508. }
  509. ret = ds2781_write(dev_info, &new_setting,
  510. DS2781_SFR, sizeof(u8));
  511. if (ret < 0)
  512. return ret;
  513. return count;
  514. }
  515. static ssize_t ds2781_read_param_eeprom_bin(struct file *filp,
  516. struct kobject *kobj,
  517. struct bin_attribute *bin_attr,
  518. char *buf, loff_t off, size_t count)
  519. {
  520. struct device *dev = container_of(kobj, struct device, kobj);
  521. struct power_supply *psy = to_power_supply(dev);
  522. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  523. return ds2781_read_block(dev_info, buf,
  524. DS2781_EEPROM_BLOCK1_START + off, count);
  525. }
  526. static ssize_t ds2781_write_param_eeprom_bin(struct file *filp,
  527. struct kobject *kobj,
  528. struct bin_attribute *bin_attr,
  529. char *buf, loff_t off, size_t count)
  530. {
  531. struct device *dev = container_of(kobj, struct device, kobj);
  532. struct power_supply *psy = to_power_supply(dev);
  533. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  534. int ret;
  535. ret = ds2781_write(dev_info, buf,
  536. DS2781_EEPROM_BLOCK1_START + off, count);
  537. if (ret < 0)
  538. return ret;
  539. ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
  540. if (ret < 0)
  541. return ret;
  542. return count;
  543. }
  544. static struct bin_attribute ds2781_param_eeprom_bin_attr = {
  545. .attr = {
  546. .name = "param_eeprom",
  547. .mode = S_IRUGO | S_IWUSR,
  548. },
  549. .size = DS2781_PARAM_EEPROM_SIZE,
  550. .read = ds2781_read_param_eeprom_bin,
  551. .write = ds2781_write_param_eeprom_bin,
  552. };
  553. static ssize_t ds2781_read_user_eeprom_bin(struct file *filp,
  554. struct kobject *kobj,
  555. struct bin_attribute *bin_attr,
  556. char *buf, loff_t off, size_t count)
  557. {
  558. struct device *dev = container_of(kobj, struct device, kobj);
  559. struct power_supply *psy = to_power_supply(dev);
  560. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  561. return ds2781_read_block(dev_info, buf,
  562. DS2781_EEPROM_BLOCK0_START + off, count);
  563. }
  564. static ssize_t ds2781_write_user_eeprom_bin(struct file *filp,
  565. struct kobject *kobj,
  566. struct bin_attribute *bin_attr,
  567. char *buf, loff_t off, size_t count)
  568. {
  569. struct device *dev = container_of(kobj, struct device, kobj);
  570. struct power_supply *psy = to_power_supply(dev);
  571. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  572. int ret;
  573. ret = ds2781_write(dev_info, buf,
  574. DS2781_EEPROM_BLOCK0_START + off, count);
  575. if (ret < 0)
  576. return ret;
  577. ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
  578. if (ret < 0)
  579. return ret;
  580. return count;
  581. }
  582. static struct bin_attribute ds2781_user_eeprom_bin_attr = {
  583. .attr = {
  584. .name = "user_eeprom",
  585. .mode = S_IRUGO | S_IWUSR,
  586. },
  587. .size = DS2781_USER_EEPROM_SIZE,
  588. .read = ds2781_read_user_eeprom_bin,
  589. .write = ds2781_write_user_eeprom_bin,
  590. };
  591. static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
  592. ds2781_set_pmod_enabled);
  593. static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
  594. ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
  595. static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
  596. ds2781_set_rsgain_setting);
  597. static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
  598. ds2781_set_pio_pin);
  599. static struct attribute *ds2781_sysfs_attrs[] = {
  600. &dev_attr_pmod_enabled.attr,
  601. &dev_attr_sense_resistor_value.attr,
  602. &dev_attr_rsgain_setting.attr,
  603. &dev_attr_pio_pin.attr,
  604. NULL
  605. };
  606. static struct bin_attribute *ds2781_sysfs_bin_attrs[] = {
  607. &ds2781_param_eeprom_bin_attr,
  608. &ds2781_user_eeprom_bin_attr,
  609. NULL,
  610. };
  611. static const struct attribute_group ds2781_sysfs_group = {
  612. .attrs = ds2781_sysfs_attrs,
  613. .bin_attrs = ds2781_sysfs_bin_attrs,
  614. };
  615. static const struct attribute_group *ds2781_sysfs_groups[] = {
  616. &ds2781_sysfs_group,
  617. NULL,
  618. };
  619. static int ds2781_battery_probe(struct platform_device *pdev)
  620. {
  621. struct power_supply_config psy_cfg = {};
  622. struct ds2781_device_info *dev_info;
  623. dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
  624. if (!dev_info)
  625. return -ENOMEM;
  626. platform_set_drvdata(pdev, dev_info);
  627. dev_info->dev = &pdev->dev;
  628. dev_info->w1_dev = pdev->dev.parent;
  629. dev_info->bat_desc.name = dev_name(&pdev->dev);
  630. dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  631. dev_info->bat_desc.properties = ds2781_battery_props;
  632. dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2781_battery_props);
  633. dev_info->bat_desc.get_property = ds2781_battery_get_property;
  634. psy_cfg.drv_data = dev_info;
  635. psy_cfg.attr_grp = ds2781_sysfs_groups;
  636. dev_info->bat = devm_power_supply_register(&pdev->dev,
  637. &dev_info->bat_desc,
  638. &psy_cfg);
  639. if (IS_ERR(dev_info->bat)) {
  640. dev_err(dev_info->dev, "failed to register battery\n");
  641. return PTR_ERR(dev_info->bat);
  642. }
  643. return 0;
  644. }
  645. static struct platform_driver ds2781_battery_driver = {
  646. .driver = {
  647. .name = "ds2781-battery",
  648. },
  649. .probe = ds2781_battery_probe,
  650. };
  651. module_platform_driver(ds2781_battery_driver);
  652. MODULE_LICENSE("GPL");
  653. MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
  654. MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC driver");
  655. MODULE_ALIAS("platform:ds2781-battery");