ds2780_battery.c 21 KB

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