ds2780_battery.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 *to_ds2780_device_info(
  46. 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_read8(struct device *dev, u8 *val, int addr)
  55. {
  56. return w1_ds2780_io(dev, val, addr, sizeof(u8), 0);
  57. }
  58. static int ds2780_read16(struct device *dev, s16 *val, int addr)
  59. {
  60. int ret;
  61. u8 raw[2];
  62. ret = w1_ds2780_io(dev, raw, addr, sizeof(u8) * 2, 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 device *dev, u8 *val, int addr,
  69. size_t count)
  70. {
  71. return w1_ds2780_io(dev, val, addr, count, 0);
  72. }
  73. static inline int ds2780_write(struct device *dev, u8 *val, int addr,
  74. size_t count)
  75. {
  76. return w1_ds2780_io(dev, 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->w1_dev, &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->w1_dev, 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->w1_dev, raw,
  121. DS2780_RSGAIN_MSB_REG, sizeof(u8) * 2);
  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 compliment 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->w1_dev, &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 compliment 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->w1_dev, &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->w1_dev, &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 -ENXIO;
  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 compliment 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->w1_dev, &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->w1_dev, &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->w1_dev, &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->w1_dev, &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->w1_dev, &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->w1_dev, 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->w1_dev, &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->w1_dev, &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->w1_dev, &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->w1_dev, &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. count = min_t(loff_t, count,
  523. DS2780_EEPROM_BLOCK1_END -
  524. DS2780_EEPROM_BLOCK1_START + 1 - off);
  525. return ds2780_read_block(dev_info->w1_dev, buf,
  526. DS2780_EEPROM_BLOCK1_START + off, count);
  527. }
  528. static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
  529. struct kobject *kobj,
  530. struct bin_attribute *bin_attr,
  531. char *buf, loff_t off, size_t count)
  532. {
  533. struct device *dev = container_of(kobj, struct device, kobj);
  534. struct power_supply *psy = to_power_supply(dev);
  535. struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
  536. int ret;
  537. count = min_t(loff_t, count,
  538. DS2780_EEPROM_BLOCK1_END -
  539. DS2780_EEPROM_BLOCK1_START + 1 - off);
  540. ret = ds2780_write(dev_info->w1_dev, buf,
  541. DS2780_EEPROM_BLOCK1_START + off, count);
  542. if (ret < 0)
  543. return ret;
  544. ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
  545. if (ret < 0)
  546. return ret;
  547. return count;
  548. }
  549. static struct bin_attribute ds2780_param_eeprom_bin_attr = {
  550. .attr = {
  551. .name = "param_eeprom",
  552. .mode = S_IRUGO | S_IWUSR,
  553. },
  554. .size = DS2780_EEPROM_BLOCK1_END - DS2780_EEPROM_BLOCK1_START + 1,
  555. .read = ds2780_read_param_eeprom_bin,
  556. .write = ds2780_write_param_eeprom_bin,
  557. };
  558. static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
  559. struct kobject *kobj,
  560. struct bin_attribute *bin_attr,
  561. char *buf, loff_t off, size_t count)
  562. {
  563. struct device *dev = container_of(kobj, struct device, kobj);
  564. struct power_supply *psy = to_power_supply(dev);
  565. struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
  566. count = min_t(loff_t, count,
  567. DS2780_EEPROM_BLOCK0_END -
  568. DS2780_EEPROM_BLOCK0_START + 1 - off);
  569. return ds2780_read_block(dev_info->w1_dev, buf,
  570. DS2780_EEPROM_BLOCK0_START + off, count);
  571. }
  572. static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
  573. struct kobject *kobj,
  574. struct bin_attribute *bin_attr,
  575. char *buf, loff_t off, size_t count)
  576. {
  577. struct device *dev = container_of(kobj, struct device, kobj);
  578. struct power_supply *psy = to_power_supply(dev);
  579. struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
  580. int ret;
  581. count = min_t(loff_t, count,
  582. DS2780_EEPROM_BLOCK0_END -
  583. DS2780_EEPROM_BLOCK0_START + 1 - off);
  584. ret = ds2780_write(dev_info->w1_dev, buf,
  585. DS2780_EEPROM_BLOCK0_START + off, count);
  586. if (ret < 0)
  587. return ret;
  588. ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
  589. if (ret < 0)
  590. return ret;
  591. return count;
  592. }
  593. static struct bin_attribute ds2780_user_eeprom_bin_attr = {
  594. .attr = {
  595. .name = "user_eeprom",
  596. .mode = S_IRUGO | S_IWUSR,
  597. },
  598. .size = DS2780_EEPROM_BLOCK0_END - DS2780_EEPROM_BLOCK0_START + 1,
  599. .read = ds2780_read_user_eeprom_bin,
  600. .write = ds2780_write_user_eeprom_bin,
  601. };
  602. static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
  603. ds2780_set_pmod_enabled);
  604. static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
  605. ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
  606. static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
  607. ds2780_set_rsgain_setting);
  608. static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
  609. ds2780_set_pio_pin);
  610. static struct attribute *ds2780_attributes[] = {
  611. &dev_attr_pmod_enabled.attr,
  612. &dev_attr_sense_resistor_value.attr,
  613. &dev_attr_rsgain_setting.attr,
  614. &dev_attr_pio_pin.attr,
  615. NULL
  616. };
  617. static const struct attribute_group ds2780_attr_group = {
  618. .attrs = ds2780_attributes,
  619. };
  620. static int __devinit ds2780_battery_probe(struct platform_device *pdev)
  621. {
  622. int ret = 0;
  623. struct ds2780_device_info *dev_info;
  624. dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
  625. if (!dev_info) {
  626. ret = -ENOMEM;
  627. goto fail;
  628. }
  629. platform_set_drvdata(pdev, dev_info);
  630. dev_info->dev = &pdev->dev;
  631. dev_info->w1_dev = pdev->dev.parent;
  632. dev_info->bat.name = dev_name(&pdev->dev);
  633. dev_info->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  634. dev_info->bat.properties = ds2780_battery_props;
  635. dev_info->bat.num_properties = ARRAY_SIZE(ds2780_battery_props);
  636. dev_info->bat.get_property = ds2780_battery_get_property;
  637. ret = power_supply_register(&pdev->dev, &dev_info->bat);
  638. if (ret) {
  639. dev_err(dev_info->dev, "failed to register battery\n");
  640. goto fail_free_info;
  641. }
  642. ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
  643. if (ret) {
  644. dev_err(dev_info->dev, "failed to create sysfs group\n");
  645. goto fail_unregister;
  646. }
  647. ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
  648. &ds2780_param_eeprom_bin_attr);
  649. if (ret) {
  650. dev_err(dev_info->dev,
  651. "failed to create param eeprom bin file");
  652. goto fail_remove_group;
  653. }
  654. ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
  655. &ds2780_user_eeprom_bin_attr);
  656. if (ret) {
  657. dev_err(dev_info->dev,
  658. "failed to create user eeprom bin file");
  659. goto fail_remove_bin_file;
  660. }
  661. return 0;
  662. fail_remove_bin_file:
  663. sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
  664. &ds2780_param_eeprom_bin_attr);
  665. fail_remove_group:
  666. sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
  667. fail_unregister:
  668. power_supply_unregister(&dev_info->bat);
  669. fail_free_info:
  670. kfree(dev_info);
  671. fail:
  672. return ret;
  673. }
  674. static int __devexit ds2780_battery_remove(struct platform_device *pdev)
  675. {
  676. struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
  677. /* remove attributes */
  678. sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
  679. power_supply_unregister(&dev_info->bat);
  680. kfree(dev_info);
  681. return 0;
  682. }
  683. MODULE_ALIAS("platform:ds2780-battery");
  684. static struct platform_driver ds2780_battery_driver = {
  685. .driver = {
  686. .name = "ds2780-battery",
  687. },
  688. .probe = ds2780_battery_probe,
  689. .remove = ds2780_battery_remove,
  690. };
  691. static int __init ds2780_battery_init(void)
  692. {
  693. return platform_driver_register(&ds2780_battery_driver);
  694. }
  695. static void __exit ds2780_battery_exit(void)
  696. {
  697. platform_driver_unregister(&ds2780_battery_driver);
  698. }
  699. module_init(ds2780_battery_init);
  700. module_exit(ds2780_battery_exit);
  701. MODULE_LICENSE("GPL");
  702. MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
  703. MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");