ds2780_battery.c 21 KB

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