ds2780_battery.c 21 KB

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