ds2780_battery.c 20 KB

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