ds2780_battery.c 21 KB

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