ds2781_battery.c 21 KB

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