ds2781_battery.c 21 KB

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