ds2781_battery.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 "../w1/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 device *w1_dev;
  36. };
  37. enum current_types {
  38. CURRENT_NOW,
  39. CURRENT_AVG,
  40. };
  41. static const char model[] = "DS2781";
  42. static const char manufacturer[] = "Maxim/Dallas";
  43. static inline struct ds2781_device_info *
  44. to_ds2781_device_info(struct power_supply *psy)
  45. {
  46. return container_of(psy, struct ds2781_device_info, bat);
  47. }
  48. static inline struct power_supply *to_power_supply(struct device *dev)
  49. {
  50. return dev_get_drvdata(dev);
  51. }
  52. static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
  53. char *buf, int addr, size_t count, int io)
  54. {
  55. return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
  56. }
  57. static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
  58. int addr, size_t count)
  59. {
  60. return ds2781_battery_io(dev_info, buf, addr, count, 0);
  61. }
  62. static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
  63. int addr)
  64. {
  65. return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
  66. }
  67. static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
  68. int addr)
  69. {
  70. int ret;
  71. u8 raw[2];
  72. ret = ds2781_battery_io(dev_info, raw, addr, sizeof(raw), 0);
  73. if (ret < 0)
  74. return ret;
  75. *val = (raw[0] << 8) | raw[1];
  76. return 0;
  77. }
  78. static inline int ds2781_read_block(struct ds2781_device_info *dev_info,
  79. u8 *val, int addr, size_t count)
  80. {
  81. return ds2781_battery_io(dev_info, val, addr, count, 0);
  82. }
  83. static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
  84. int addr, size_t count)
  85. {
  86. return ds2781_battery_io(dev_info, val, addr, count, 1);
  87. }
  88. static inline int ds2781_store_eeprom(struct device *dev, int addr)
  89. {
  90. return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
  91. }
  92. static inline int ds2781_recall_eeprom(struct device *dev, int addr)
  93. {
  94. return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
  95. }
  96. static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
  97. {
  98. int ret;
  99. ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
  100. if (ret < 0)
  101. return ret;
  102. ret = ds2781_recall_eeprom(dev_info->w1_dev, reg);
  103. if (ret < 0)
  104. return ret;
  105. return 0;
  106. }
  107. /* Set sense resistor value in mhos */
  108. static int ds2781_set_sense_register(struct ds2781_device_info *dev_info,
  109. u8 conductance)
  110. {
  111. int ret;
  112. ret = ds2781_write(dev_info, &conductance,
  113. DS2781_RSNSP, sizeof(u8));
  114. if (ret < 0)
  115. return ret;
  116. return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
  117. }
  118. /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
  119. static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
  120. u16 *rsgain)
  121. {
  122. return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
  123. }
  124. /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
  125. static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
  126. u16 rsgain)
  127. {
  128. int ret;
  129. u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
  130. ret = ds2781_write(dev_info, raw,
  131. DS2781_RSGAIN_MSB, sizeof(raw));
  132. if (ret < 0)
  133. return ret;
  134. return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
  135. }
  136. static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
  137. int *voltage_uV)
  138. {
  139. int ret;
  140. char val[2];
  141. int voltage_raw;
  142. ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
  143. if (ret < 0)
  144. return ret;
  145. /*
  146. * The voltage value is located in 10 bits across the voltage MSB
  147. * and LSB registers in two's compliment form
  148. * Sign bit of the voltage value is in bit 7 of the voltage MSB register
  149. * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
  150. * voltage MSB register
  151. * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
  152. * voltage LSB register
  153. */
  154. voltage_raw = (val[0] << 3) |
  155. (val[1] >> 5);
  156. /* DS2781 reports voltage in units of 9.76mV, but the battery class
  157. * reports in units of uV, so convert by multiplying by 9760. */
  158. *voltage_uV = voltage_raw * 9760;
  159. return 0;
  160. }
  161. static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
  162. int *temp)
  163. {
  164. int ret;
  165. char val[2];
  166. int temp_raw;
  167. ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
  168. if (ret < 0)
  169. return ret;
  170. /*
  171. * The temperature value is located in 10 bits across the temperature
  172. * MSB and LSB registers in two's compliment form
  173. * Sign bit of the temperature value is in bit 7 of the temperature
  174. * MSB register
  175. * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
  176. * temperature MSB register
  177. * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
  178. * temperature LSB register
  179. */
  180. temp_raw = ((val[0]) << 3) |
  181. (val[1] >> 5);
  182. *temp = temp_raw + (temp_raw / 4);
  183. return 0;
  184. }
  185. static int ds2781_get_current(struct ds2781_device_info *dev_info,
  186. enum current_types type, int *current_uA)
  187. {
  188. int ret, sense_res;
  189. s16 current_raw;
  190. u8 sense_res_raw, reg_msb;
  191. /*
  192. * The units of measurement for current are dependent on the value of
  193. * the sense resistor.
  194. */
  195. ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
  196. if (ret < 0)
  197. return ret;
  198. if (sense_res_raw == 0) {
  199. dev_err(dev_info->dev, "sense resistor value is 0\n");
  200. return -EINVAL;
  201. }
  202. sense_res = 1000 / sense_res_raw;
  203. if (type == CURRENT_NOW)
  204. reg_msb = DS2781_CURRENT_MSB;
  205. else if (type == CURRENT_AVG)
  206. reg_msb = DS2781_IAVG_MSB;
  207. else
  208. return -EINVAL;
  209. /*
  210. * The current value is located in 16 bits across the current MSB
  211. * and LSB registers in two's compliment form
  212. * Sign bit of the current value is in bit 7 of the current MSB register
  213. * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
  214. * MSB register
  215. * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
  216. * LSB register
  217. */
  218. ret = ds2781_read16(dev_info, &current_raw, reg_msb);
  219. if (ret < 0)
  220. return ret;
  221. *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
  222. return 0;
  223. }
  224. static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
  225. int *accumulated_current)
  226. {
  227. int ret, sense_res;
  228. s16 current_raw;
  229. u8 sense_res_raw;
  230. /*
  231. * The units of measurement for accumulated current are dependent on
  232. * the value of the sense resistor.
  233. */
  234. ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
  235. if (ret < 0)
  236. return ret;
  237. if (sense_res_raw == 0) {
  238. dev_err(dev_info->dev, "sense resistor value is 0\n");
  239. return -EINVAL;
  240. }
  241. sense_res = 1000 / sense_res_raw;
  242. /*
  243. * The ACR value is located in 16 bits across the ACR MSB and
  244. * LSB registers
  245. * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
  246. * MSB register
  247. * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
  248. * LSB register
  249. */
  250. ret = ds2781_read16(dev_info, &current_raw, DS2781_ACR_MSB);
  251. if (ret < 0)
  252. return ret;
  253. *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
  254. return 0;
  255. }
  256. static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
  257. int *capacity)
  258. {
  259. int ret;
  260. u8 raw;
  261. ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
  262. if (ret < 0)
  263. return ret;
  264. *capacity = raw;
  265. return 0;
  266. }
  267. static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
  268. {
  269. int ret, current_uA, capacity;
  270. ret = ds2781_get_current(dev_info, CURRENT_NOW, &current_uA);
  271. if (ret < 0)
  272. return ret;
  273. ret = ds2781_get_capacity(dev_info, &capacity);
  274. if (ret < 0)
  275. return ret;
  276. if (power_supply_am_i_supplied(&dev_info->bat)) {
  277. if (capacity == 100)
  278. *status = POWER_SUPPLY_STATUS_FULL;
  279. else if (current_uA > 50000)
  280. *status = POWER_SUPPLY_STATUS_CHARGING;
  281. else
  282. *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  283. } else {
  284. *status = POWER_SUPPLY_STATUS_DISCHARGING;
  285. }
  286. return 0;
  287. }
  288. static int ds2781_get_charge_now(struct ds2781_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 = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
  302. if (ret < 0)
  303. return ret;
  304. *charge_now = charge_raw * 1600;
  305. return 0;
  306. }
  307. static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
  308. u8 *control_reg)
  309. {
  310. return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
  311. }
  312. static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
  313. u8 control_reg)
  314. {
  315. int ret;
  316. ret = ds2781_write(dev_info, &control_reg,
  317. DS2781_CONTROL, sizeof(u8));
  318. if (ret < 0)
  319. return ret;
  320. return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
  321. }
  322. static int ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  328. switch (psp) {
  329. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  330. ret = ds2781_get_voltage(dev_info, &val->intval);
  331. break;
  332. case POWER_SUPPLY_PROP_TEMP:
  333. ret = ds2781_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 = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
  343. break;
  344. case POWER_SUPPLY_PROP_CURRENT_AVG:
  345. ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
  346. break;
  347. case POWER_SUPPLY_PROP_STATUS:
  348. ret = ds2781_get_status(dev_info, &val->intval);
  349. break;
  350. case POWER_SUPPLY_PROP_CAPACITY:
  351. ret = ds2781_get_capacity(dev_info, &val->intval);
  352. break;
  353. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  354. ret = ds2781_get_accumulated_current(dev_info, &val->intval);
  355. break;
  356. case POWER_SUPPLY_PROP_CHARGE_NOW:
  357. ret = ds2781_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 ds2781_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 ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  384. /* Get power mode */
  385. ret = ds2781_get_control_register(dev_info, &control_reg);
  386. if (ret < 0)
  387. return ret;
  388. return sprintf(buf, "%d\n",
  389. !!(control_reg & DS2781_CONTROL_PMOD));
  390. }
  391. static ssize_t ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  400. /* Set power mode */
  401. ret = ds2781_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 |= DS2781_CONTROL_PMOD;
  413. else
  414. control_reg &= ~DS2781_CONTROL_PMOD;
  415. ret = ds2781_set_control_register(dev_info, control_reg);
  416. if (ret < 0)
  417. return ret;
  418. return count;
  419. }
  420. static ssize_t ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  428. ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
  429. if (ret < 0)
  430. return ret;
  431. ret = sprintf(buf, "%d\n", sense_resistor);
  432. return ret;
  433. }
  434. static ssize_t ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  443. ret = kstrtou8(buf, 0, &new_setting);
  444. if (ret < 0)
  445. return ret;
  446. ret = ds2781_set_sense_register(dev_info, new_setting);
  447. if (ret < 0)
  448. return ret;
  449. return count;
  450. }
  451. static ssize_t ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  459. ret = ds2781_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 ds2781_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 ds2781_device_info *dev_info = to_ds2781_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 = ds2781_set_rsgain_register(dev_info, new_setting);
  482. if (ret < 0)
  483. return ret;
  484. return count;
  485. }
  486. static ssize_t ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  494. ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
  495. if (ret < 0)
  496. return ret;
  497. ret = sprintf(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
  498. return ret;
  499. }
  500. static ssize_t ds2781_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 ds2781_device_info *dev_info = to_ds2781_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 = ds2781_write(dev_info, &new_setting,
  517. DS2781_SFR, sizeof(u8));
  518. if (ret < 0)
  519. return ret;
  520. return count;
  521. }
  522. static ssize_t ds2781_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 ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  530. count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off);
  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. count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off);
  544. ret = ds2781_write(dev_info, buf,
  545. DS2781_EEPROM_BLOCK1_START + off, count);
  546. if (ret < 0)
  547. return ret;
  548. ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
  549. if (ret < 0)
  550. return ret;
  551. return count;
  552. }
  553. static struct bin_attribute ds2781_param_eeprom_bin_attr = {
  554. .attr = {
  555. .name = "param_eeprom",
  556. .mode = S_IRUGO | S_IWUSR,
  557. },
  558. .size = DS2781_PARAM_EEPROM_SIZE,
  559. .read = ds2781_read_param_eeprom_bin,
  560. .write = ds2781_write_param_eeprom_bin,
  561. };
  562. static ssize_t ds2781_read_user_eeprom_bin(struct file *filp,
  563. struct kobject *kobj,
  564. struct bin_attribute *bin_attr,
  565. char *buf, loff_t off, size_t count)
  566. {
  567. struct device *dev = container_of(kobj, struct device, kobj);
  568. struct power_supply *psy = to_power_supply(dev);
  569. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  570. count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off);
  571. return ds2781_read_block(dev_info, buf,
  572. DS2781_EEPROM_BLOCK0_START + off, count);
  573. }
  574. static ssize_t ds2781_write_user_eeprom_bin(struct file *filp,
  575. struct kobject *kobj,
  576. struct bin_attribute *bin_attr,
  577. char *buf, loff_t off, size_t count)
  578. {
  579. struct device *dev = container_of(kobj, struct device, kobj);
  580. struct power_supply *psy = to_power_supply(dev);
  581. struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
  582. int ret;
  583. count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off);
  584. ret = ds2781_write(dev_info, buf,
  585. DS2781_EEPROM_BLOCK0_START + off, count);
  586. if (ret < 0)
  587. return ret;
  588. ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
  589. if (ret < 0)
  590. return ret;
  591. return count;
  592. }
  593. static struct bin_attribute ds2781_user_eeprom_bin_attr = {
  594. .attr = {
  595. .name = "user_eeprom",
  596. .mode = S_IRUGO | S_IWUSR,
  597. },
  598. .size = DS2781_USER_EEPROM_SIZE,
  599. .read = ds2781_read_user_eeprom_bin,
  600. .write = ds2781_write_user_eeprom_bin,
  601. };
  602. static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
  603. ds2781_set_pmod_enabled);
  604. static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
  605. ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
  606. static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
  607. ds2781_set_rsgain_setting);
  608. static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
  609. ds2781_set_pio_pin);
  610. static struct attribute *ds2781_attributes[] = {
  611. &dev_attr_pmod_enabled.attr,
  612. &dev_attr_sense_resistor_value.attr,
  613. &dev_attr_rsgain_setting.attr,
  614. &dev_attr_pio_pin.attr,
  615. NULL
  616. };
  617. static const struct attribute_group ds2781_attr_group = {
  618. .attrs = ds2781_attributes,
  619. };
  620. static int ds2781_battery_probe(struct platform_device *pdev)
  621. {
  622. int ret = 0;
  623. struct ds2781_device_info *dev_info;
  624. dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
  625. if (!dev_info)
  626. return -ENOMEM;
  627. platform_set_drvdata(pdev, dev_info);
  628. dev_info->dev = &pdev->dev;
  629. dev_info->w1_dev = pdev->dev.parent;
  630. dev_info->bat.name = dev_name(&pdev->dev);
  631. dev_info->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  632. dev_info->bat.properties = ds2781_battery_props;
  633. dev_info->bat.num_properties = ARRAY_SIZE(ds2781_battery_props);
  634. dev_info->bat.get_property = ds2781_battery_get_property;
  635. ret = power_supply_register(&pdev->dev, &dev_info->bat);
  636. if (ret) {
  637. dev_err(dev_info->dev, "failed to register battery\n");
  638. goto fail;
  639. }
  640. ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
  641. if (ret) {
  642. dev_err(dev_info->dev, "failed to create sysfs group\n");
  643. goto fail_unregister;
  644. }
  645. ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
  646. &ds2781_param_eeprom_bin_attr);
  647. if (ret) {
  648. dev_err(dev_info->dev,
  649. "failed to create param eeprom bin file");
  650. goto fail_remove_group;
  651. }
  652. ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
  653. &ds2781_user_eeprom_bin_attr);
  654. if (ret) {
  655. dev_err(dev_info->dev,
  656. "failed to create user eeprom bin file");
  657. goto fail_remove_bin_file;
  658. }
  659. return 0;
  660. fail_remove_bin_file:
  661. sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
  662. &ds2781_param_eeprom_bin_attr);
  663. fail_remove_group:
  664. sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);
  665. fail_unregister:
  666. power_supply_unregister(&dev_info->bat);
  667. fail:
  668. return ret;
  669. }
  670. static int ds2781_battery_remove(struct platform_device *pdev)
  671. {
  672. struct ds2781_device_info *dev_info = platform_get_drvdata(pdev);
  673. /* remove attributes */
  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");