ds2781_battery.c 21 KB

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