ds2760_battery.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Driver for batteries with DS2760 chips inside.
  3. *
  4. * Copyright © 2007 Anton Vorontsov
  5. * 2004-2007 Matt Reimer
  6. * 2004 Szabolcs Gyurko
  7. *
  8. * Use consistent with the GNU GPL is permitted,
  9. * provided that this copyright notice is
  10. * preserved in its entirety in all copies and derived works.
  11. *
  12. * Author: Anton Vorontsov <cbou@mail.ru>
  13. * February 2007
  14. *
  15. * Matt Reimer <mreimer@vpop.net>
  16. * April 2004, 2005, 2007
  17. *
  18. * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
  19. * September 2004
  20. */
  21. #include <linux/module.h>
  22. #include <linux/param.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/pm.h>
  26. #include <linux/slab.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/power_supply.h>
  29. #include <linux/suspend.h>
  30. #include <linux/w1.h>
  31. #include <linux/of.h>
  32. static unsigned int cache_time = 1000;
  33. module_param(cache_time, uint, 0644);
  34. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  35. static bool pmod_enabled;
  36. module_param(pmod_enabled, bool, 0644);
  37. MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
  38. static unsigned int rated_capacity;
  39. module_param(rated_capacity, uint, 0644);
  40. MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
  41. static unsigned int current_accum;
  42. module_param(current_accum, uint, 0644);
  43. MODULE_PARM_DESC(current_accum, "current accumulator value");
  44. #define W1_FAMILY_DS2760 0x30
  45. /* Known commands to the DS2760 chip */
  46. #define W1_DS2760_SWAP 0xAA
  47. #define W1_DS2760_READ_DATA 0x69
  48. #define W1_DS2760_WRITE_DATA 0x6C
  49. #define W1_DS2760_COPY_DATA 0x48
  50. #define W1_DS2760_RECALL_DATA 0xB8
  51. #define W1_DS2760_LOCK 0x6A
  52. /* Number of valid register addresses */
  53. #define DS2760_DATA_SIZE 0x40
  54. #define DS2760_PROTECTION_REG 0x00
  55. #define DS2760_STATUS_REG 0x01
  56. #define DS2760_STATUS_IE (1 << 2)
  57. #define DS2760_STATUS_SWEN (1 << 3)
  58. #define DS2760_STATUS_RNAOP (1 << 4)
  59. #define DS2760_STATUS_PMOD (1 << 5)
  60. #define DS2760_EEPROM_REG 0x07
  61. #define DS2760_SPECIAL_FEATURE_REG 0x08
  62. #define DS2760_VOLTAGE_MSB 0x0c
  63. #define DS2760_VOLTAGE_LSB 0x0d
  64. #define DS2760_CURRENT_MSB 0x0e
  65. #define DS2760_CURRENT_LSB 0x0f
  66. #define DS2760_CURRENT_ACCUM_MSB 0x10
  67. #define DS2760_CURRENT_ACCUM_LSB 0x11
  68. #define DS2760_TEMP_MSB 0x18
  69. #define DS2760_TEMP_LSB 0x19
  70. #define DS2760_EEPROM_BLOCK0 0x20
  71. #define DS2760_ACTIVE_FULL 0x20
  72. #define DS2760_EEPROM_BLOCK1 0x30
  73. #define DS2760_STATUS_WRITE_REG 0x31
  74. #define DS2760_RATED_CAPACITY 0x32
  75. #define DS2760_CURRENT_OFFSET_BIAS 0x33
  76. #define DS2760_ACTIVE_EMPTY 0x3b
  77. struct ds2760_device_info {
  78. struct device *dev;
  79. /* DS2760 data, valid after calling ds2760_battery_read_status() */
  80. unsigned long update_time; /* jiffies when data read */
  81. char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
  82. int voltage_raw; /* units of 4.88 mV */
  83. int voltage_uV; /* units of µV */
  84. int current_raw; /* units of 0.625 mA */
  85. int current_uA; /* units of µA */
  86. int accum_current_raw; /* units of 0.25 mAh */
  87. int accum_current_uAh; /* units of µAh */
  88. int temp_raw; /* units of 0.125 °C */
  89. int temp_C; /* units of 0.1 °C */
  90. int rated_capacity; /* units of µAh */
  91. int rem_capacity; /* percentage */
  92. int full_active_uAh; /* units of µAh */
  93. int empty_uAh; /* units of µAh */
  94. int life_sec; /* units of seconds */
  95. int charge_status; /* POWER_SUPPLY_STATUS_* */
  96. int full_counter;
  97. struct power_supply *bat;
  98. struct power_supply_desc bat_desc;
  99. struct workqueue_struct *monitor_wqueue;
  100. struct delayed_work monitor_work;
  101. struct delayed_work set_charged_work;
  102. struct notifier_block pm_notifier;
  103. };
  104. static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
  105. int io)
  106. {
  107. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  108. if (!dev)
  109. return 0;
  110. mutex_lock(&sl->master->bus_mutex);
  111. if (addr > DS2760_DATA_SIZE || addr < 0) {
  112. count = 0;
  113. goto out;
  114. }
  115. if (addr + count > DS2760_DATA_SIZE)
  116. count = DS2760_DATA_SIZE - addr;
  117. if (!w1_reset_select_slave(sl)) {
  118. if (!io) {
  119. w1_write_8(sl->master, W1_DS2760_READ_DATA);
  120. w1_write_8(sl->master, addr);
  121. count = w1_read_block(sl->master, buf, count);
  122. } else {
  123. w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
  124. w1_write_8(sl->master, addr);
  125. w1_write_block(sl->master, buf, count);
  126. /* XXX w1_write_block returns void, not n_written */
  127. }
  128. }
  129. out:
  130. mutex_unlock(&sl->master->bus_mutex);
  131. return count;
  132. }
  133. static int w1_ds2760_read(struct device *dev,
  134. char *buf, int addr,
  135. size_t count)
  136. {
  137. return w1_ds2760_io(dev, buf, addr, count, 0);
  138. }
  139. static int w1_ds2760_write(struct device *dev,
  140. char *buf,
  141. int addr, size_t count)
  142. {
  143. return w1_ds2760_io(dev, buf, addr, count, 1);
  144. }
  145. static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
  146. {
  147. struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
  148. if (!dev)
  149. return -EINVAL;
  150. mutex_lock(&sl->master->bus_mutex);
  151. if (w1_reset_select_slave(sl) == 0) {
  152. w1_write_8(sl->master, cmd);
  153. w1_write_8(sl->master, addr);
  154. }
  155. mutex_unlock(&sl->master->bus_mutex);
  156. return 0;
  157. }
  158. static int w1_ds2760_store_eeprom(struct device *dev, int addr)
  159. {
  160. return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
  161. }
  162. static int w1_ds2760_recall_eeprom(struct device *dev, int addr)
  163. {
  164. return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
  165. }
  166. static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
  167. struct bin_attribute *bin_attr, char *buf,
  168. loff_t off, size_t count)
  169. {
  170. struct device *dev = kobj_to_dev(kobj);
  171. return w1_ds2760_read(dev, buf, off, count);
  172. }
  173. static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
  174. static struct bin_attribute *w1_ds2760_bin_attrs[] = {
  175. &bin_attr_w1_slave,
  176. NULL,
  177. };
  178. static const struct attribute_group w1_ds2760_group = {
  179. .bin_attrs = w1_ds2760_bin_attrs,
  180. };
  181. static const struct attribute_group *w1_ds2760_groups[] = {
  182. &w1_ds2760_group,
  183. NULL,
  184. };
  185. /* Some batteries have their rated capacity stored a N * 10 mAh, while
  186. * others use an index into this table. */
  187. static int rated_capacities[] = {
  188. 0,
  189. 920, /* Samsung */
  190. 920, /* BYD */
  191. 920, /* Lishen */
  192. 920, /* NEC */
  193. 1440, /* Samsung */
  194. 1440, /* BYD */
  195. 1440, /* Lishen */
  196. 1440, /* NEC */
  197. 2880, /* Samsung */
  198. 2880, /* BYD */
  199. 2880, /* Lishen */
  200. 2880, /* NEC */
  201. };
  202. /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
  203. * temp is in Celsius */
  204. static int battery_interpolate(int array[], int temp)
  205. {
  206. int index, dt;
  207. if (temp <= 0)
  208. return array[0];
  209. if (temp >= 40)
  210. return array[4];
  211. index = temp / 10;
  212. dt = temp % 10;
  213. return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
  214. }
  215. static int ds2760_battery_read_status(struct ds2760_device_info *di)
  216. {
  217. int ret, i, start, count, scale[5];
  218. if (di->update_time && time_before(jiffies, di->update_time +
  219. msecs_to_jiffies(cache_time)))
  220. return 0;
  221. /* The first time we read the entire contents of SRAM/EEPROM,
  222. * but after that we just read the interesting bits that change. */
  223. if (di->update_time == 0) {
  224. start = 0;
  225. count = DS2760_DATA_SIZE;
  226. } else {
  227. start = DS2760_VOLTAGE_MSB;
  228. count = DS2760_TEMP_LSB - start + 1;
  229. }
  230. ret = w1_ds2760_read(di->dev, di->raw + start, start, count);
  231. if (ret != count) {
  232. dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
  233. di->dev);
  234. return 1;
  235. }
  236. di->update_time = jiffies;
  237. /* DS2760 reports voltage in units of 4.88mV, but the battery class
  238. * reports in units of uV, so convert by multiplying by 4880. */
  239. di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
  240. (di->raw[DS2760_VOLTAGE_LSB] >> 5);
  241. di->voltage_uV = di->voltage_raw * 4880;
  242. /* DS2760 reports current in signed units of 0.625mA, but the battery
  243. * class reports in units of µA, so convert by multiplying by 625. */
  244. di->current_raw =
  245. (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
  246. (di->raw[DS2760_CURRENT_LSB] >> 3);
  247. di->current_uA = di->current_raw * 625;
  248. /* DS2760 reports accumulated current in signed units of 0.25mAh. */
  249. di->accum_current_raw =
  250. (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
  251. di->raw[DS2760_CURRENT_ACCUM_LSB];
  252. di->accum_current_uAh = di->accum_current_raw * 250;
  253. /* DS2760 reports temperature in signed units of 0.125°C, but the
  254. * battery class reports in units of 1/10 °C, so we convert by
  255. * multiplying by .125 * 10 = 1.25. */
  256. di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
  257. (di->raw[DS2760_TEMP_LSB] >> 5);
  258. di->temp_C = di->temp_raw + (di->temp_raw / 4);
  259. /* At least some battery monitors (e.g. HP iPAQ) store the battery's
  260. * maximum rated capacity. */
  261. if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
  262. di->rated_capacity = rated_capacities[
  263. (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
  264. else
  265. di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
  266. di->rated_capacity *= 1000; /* convert to µAh */
  267. /* Calculate the full level at the present temperature. */
  268. di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
  269. di->raw[DS2760_ACTIVE_FULL + 1];
  270. /* If the full_active_uAh value is not given, fall back to the rated
  271. * capacity. This is likely to happen when chips are not part of the
  272. * battery pack and is therefore not bootstrapped. */
  273. if (di->full_active_uAh == 0)
  274. di->full_active_uAh = di->rated_capacity / 1000L;
  275. scale[0] = di->full_active_uAh;
  276. for (i = 1; i < 5; i++)
  277. scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
  278. di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
  279. di->full_active_uAh *= 1000; /* convert to µAh */
  280. /* Calculate the empty level at the present temperature. */
  281. scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
  282. for (i = 3; i >= 0; i--)
  283. scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
  284. di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
  285. di->empty_uAh *= 1000; /* convert to µAh */
  286. if (di->full_active_uAh == di->empty_uAh)
  287. di->rem_capacity = 0;
  288. else
  289. /* From Maxim Application Note 131: remaining capacity =
  290. * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
  291. di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
  292. (di->full_active_uAh - di->empty_uAh);
  293. if (di->rem_capacity < 0)
  294. di->rem_capacity = 0;
  295. if (di->rem_capacity > 100)
  296. di->rem_capacity = 100;
  297. if (di->current_uA < -100L)
  298. di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
  299. / (di->current_uA / 100L);
  300. else
  301. di->life_sec = 0;
  302. return 0;
  303. }
  304. static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
  305. unsigned int acr_val)
  306. {
  307. unsigned char acr[2];
  308. /* acr is in units of 0.25 mAh */
  309. acr_val *= 4L;
  310. acr_val /= 1000;
  311. acr[0] = acr_val >> 8;
  312. acr[1] = acr_val & 0xff;
  313. if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
  314. dev_warn(di->dev, "ACR write failed\n");
  315. }
  316. static void ds2760_battery_update_status(struct ds2760_device_info *di)
  317. {
  318. int old_charge_status = di->charge_status;
  319. ds2760_battery_read_status(di);
  320. if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
  321. di->full_counter = 0;
  322. if (power_supply_am_i_supplied(di->bat)) {
  323. if (di->current_uA > 10000) {
  324. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  325. di->full_counter = 0;
  326. } else if (di->current_uA < -5000) {
  327. if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
  328. dev_notice(di->dev, "not enough power to "
  329. "charge\n");
  330. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  331. di->full_counter = 0;
  332. } else if (di->current_uA < 10000 &&
  333. di->charge_status != POWER_SUPPLY_STATUS_FULL) {
  334. /* Don't consider the battery to be full unless
  335. * we've seen the current < 10 mA at least two
  336. * consecutive times. */
  337. di->full_counter++;
  338. if (di->full_counter < 2) {
  339. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  340. } else {
  341. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  342. ds2760_battery_set_current_accum(di,
  343. di->full_active_uAh);
  344. }
  345. }
  346. } else {
  347. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  348. di->full_counter = 0;
  349. }
  350. if (di->charge_status != old_charge_status)
  351. power_supply_changed(di->bat);
  352. }
  353. static void ds2760_battery_write_status(struct ds2760_device_info *di,
  354. char status)
  355. {
  356. if (status == di->raw[DS2760_STATUS_REG])
  357. return;
  358. w1_ds2760_write(di->dev, &status, DS2760_STATUS_WRITE_REG, 1);
  359. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  360. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  361. }
  362. static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
  363. unsigned char rated_capacity)
  364. {
  365. if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
  366. return;
  367. w1_ds2760_write(di->dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
  368. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  369. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  370. }
  371. static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
  372. int active_full)
  373. {
  374. unsigned char tmp[2] = {
  375. active_full >> 8,
  376. active_full & 0xff
  377. };
  378. if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
  379. tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
  380. return;
  381. w1_ds2760_write(di->dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
  382. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
  383. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
  384. /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
  385. * values won't be read back by ds2760_battery_read_status() */
  386. di->raw[DS2760_ACTIVE_FULL] = tmp[0];
  387. di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
  388. }
  389. static void ds2760_battery_work(struct work_struct *work)
  390. {
  391. struct ds2760_device_info *di = container_of(work,
  392. struct ds2760_device_info, monitor_work.work);
  393. const int interval = HZ * 60;
  394. dev_dbg(di->dev, "%s\n", __func__);
  395. ds2760_battery_update_status(di);
  396. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
  397. }
  398. static void ds2760_battery_external_power_changed(struct power_supply *psy)
  399. {
  400. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  401. dev_dbg(di->dev, "%s\n", __func__);
  402. mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
  403. }
  404. static void ds2760_battery_set_charged_work(struct work_struct *work)
  405. {
  406. char bias;
  407. struct ds2760_device_info *di = container_of(work,
  408. struct ds2760_device_info, set_charged_work.work);
  409. dev_dbg(di->dev, "%s\n", __func__);
  410. ds2760_battery_read_status(di);
  411. /* When we get notified by external circuitry that the battery is
  412. * considered fully charged now, we know that there is no current
  413. * flow any more. However, the ds2760's internal current meter is
  414. * too inaccurate to rely on - spec say something ~15% failure.
  415. * Hence, we use the current offset bias register to compensate
  416. * that error.
  417. */
  418. if (!power_supply_am_i_supplied(di->bat))
  419. return;
  420. bias = (signed char) di->current_raw +
  421. (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
  422. dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
  423. w1_ds2760_write(di->dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
  424. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  425. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  426. /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
  427. * value won't be read back by ds2760_battery_read_status() */
  428. di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
  429. }
  430. static void ds2760_battery_set_charged(struct power_supply *psy)
  431. {
  432. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  433. /* postpone the actual work by 20 secs. This is for debouncing GPIO
  434. * signals and to let the current value settle. See AN4188. */
  435. mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
  436. }
  437. static int ds2760_battery_get_property(struct power_supply *psy,
  438. enum power_supply_property psp,
  439. union power_supply_propval *val)
  440. {
  441. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  442. switch (psp) {
  443. case POWER_SUPPLY_PROP_STATUS:
  444. val->intval = di->charge_status;
  445. return 0;
  446. default:
  447. break;
  448. }
  449. ds2760_battery_read_status(di);
  450. switch (psp) {
  451. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  452. val->intval = di->voltage_uV;
  453. break;
  454. case POWER_SUPPLY_PROP_CURRENT_NOW:
  455. val->intval = di->current_uA;
  456. break;
  457. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  458. val->intval = di->rated_capacity;
  459. break;
  460. case POWER_SUPPLY_PROP_CHARGE_FULL:
  461. val->intval = di->full_active_uAh;
  462. break;
  463. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  464. val->intval = di->empty_uAh;
  465. break;
  466. case POWER_SUPPLY_PROP_CHARGE_NOW:
  467. val->intval = di->accum_current_uAh;
  468. break;
  469. case POWER_SUPPLY_PROP_TEMP:
  470. val->intval = di->temp_C;
  471. break;
  472. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  473. val->intval = di->life_sec;
  474. break;
  475. case POWER_SUPPLY_PROP_CAPACITY:
  476. val->intval = di->rem_capacity;
  477. break;
  478. default:
  479. return -EINVAL;
  480. }
  481. return 0;
  482. }
  483. static int ds2760_battery_set_property(struct power_supply *psy,
  484. enum power_supply_property psp,
  485. const union power_supply_propval *val)
  486. {
  487. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  488. switch (psp) {
  489. case POWER_SUPPLY_PROP_CHARGE_FULL:
  490. /* the interface counts in uAh, convert the value */
  491. ds2760_battery_write_active_full(di, val->intval / 1000L);
  492. break;
  493. case POWER_SUPPLY_PROP_CHARGE_NOW:
  494. /* ds2760_battery_set_current_accum() does the conversion */
  495. ds2760_battery_set_current_accum(di, val->intval);
  496. break;
  497. default:
  498. return -EPERM;
  499. }
  500. return 0;
  501. }
  502. static int ds2760_battery_property_is_writeable(struct power_supply *psy,
  503. enum power_supply_property psp)
  504. {
  505. switch (psp) {
  506. case POWER_SUPPLY_PROP_CHARGE_FULL:
  507. case POWER_SUPPLY_PROP_CHARGE_NOW:
  508. return 1;
  509. default:
  510. break;
  511. }
  512. return 0;
  513. }
  514. static enum power_supply_property ds2760_battery_props[] = {
  515. POWER_SUPPLY_PROP_STATUS,
  516. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  517. POWER_SUPPLY_PROP_CURRENT_NOW,
  518. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  519. POWER_SUPPLY_PROP_CHARGE_FULL,
  520. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  521. POWER_SUPPLY_PROP_CHARGE_NOW,
  522. POWER_SUPPLY_PROP_TEMP,
  523. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  524. POWER_SUPPLY_PROP_CAPACITY,
  525. };
  526. static int ds2760_pm_notifier(struct notifier_block *notifier,
  527. unsigned long pm_event,
  528. void *unused)
  529. {
  530. struct ds2760_device_info *di =
  531. container_of(notifier, struct ds2760_device_info, pm_notifier);
  532. switch (pm_event) {
  533. case PM_HIBERNATION_PREPARE:
  534. case PM_SUSPEND_PREPARE:
  535. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  536. break;
  537. case PM_POST_RESTORE:
  538. case PM_POST_HIBERNATION:
  539. case PM_POST_SUSPEND:
  540. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  541. power_supply_changed(di->bat);
  542. mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
  543. break;
  544. case PM_RESTORE_PREPARE:
  545. default:
  546. break;
  547. }
  548. return NOTIFY_DONE;
  549. }
  550. static int w1_ds2760_add_slave(struct w1_slave *sl)
  551. {
  552. struct power_supply_config psy_cfg = {};
  553. struct ds2760_device_info *di;
  554. struct device *dev = &sl->dev;
  555. int retval = 0;
  556. char name[32];
  557. char status;
  558. di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
  559. if (!di) {
  560. retval = -ENOMEM;
  561. goto di_alloc_failed;
  562. }
  563. snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id);
  564. di->dev = dev;
  565. di->bat_desc.name = name;
  566. di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  567. di->bat_desc.properties = ds2760_battery_props;
  568. di->bat_desc.num_properties = ARRAY_SIZE(ds2760_battery_props);
  569. di->bat_desc.get_property = ds2760_battery_get_property;
  570. di->bat_desc.set_property = ds2760_battery_set_property;
  571. di->bat_desc.property_is_writeable =
  572. ds2760_battery_property_is_writeable;
  573. di->bat_desc.set_charged = ds2760_battery_set_charged;
  574. di->bat_desc.external_power_changed =
  575. ds2760_battery_external_power_changed;
  576. psy_cfg.drv_data = di;
  577. if (dev->of_node) {
  578. u32 tmp;
  579. psy_cfg.of_node = dev->of_node;
  580. if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled"))
  581. pmod_enabled = true;
  582. if (!of_property_read_u32(dev->of_node,
  583. "maxim,cache-time-ms", &tmp))
  584. cache_time = tmp;
  585. if (!of_property_read_u32(dev->of_node,
  586. "rated-capacity-microamp-hours",
  587. &tmp))
  588. rated_capacity = tmp / 10; /* property is in mAh */
  589. }
  590. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  591. sl->family_data = di;
  592. /* enable sleep mode feature */
  593. ds2760_battery_read_status(di);
  594. status = di->raw[DS2760_STATUS_REG];
  595. if (pmod_enabled)
  596. status |= DS2760_STATUS_PMOD;
  597. else
  598. status &= ~DS2760_STATUS_PMOD;
  599. ds2760_battery_write_status(di, status);
  600. /* set rated capacity from module param or device tree */
  601. if (rated_capacity)
  602. ds2760_battery_write_rated_capacity(di, rated_capacity);
  603. /* set current accumulator if given as parameter.
  604. * this should only be done for bootstrapping the value */
  605. if (current_accum)
  606. ds2760_battery_set_current_accum(di, current_accum);
  607. di->bat = devm_power_supply_register(dev, &di->bat_desc, &psy_cfg);
  608. if (IS_ERR(di->bat)) {
  609. dev_err(di->dev, "failed to register battery\n");
  610. retval = PTR_ERR(di->bat);
  611. goto batt_failed;
  612. }
  613. INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
  614. INIT_DELAYED_WORK(&di->set_charged_work,
  615. ds2760_battery_set_charged_work);
  616. di->monitor_wqueue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
  617. if (!di->monitor_wqueue) {
  618. retval = -ESRCH;
  619. goto workqueue_failed;
  620. }
  621. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
  622. di->pm_notifier.notifier_call = ds2760_pm_notifier;
  623. register_pm_notifier(&di->pm_notifier);
  624. goto success;
  625. workqueue_failed:
  626. batt_failed:
  627. di_alloc_failed:
  628. success:
  629. return retval;
  630. }
  631. static void w1_ds2760_remove_slave(struct w1_slave *sl)
  632. {
  633. struct ds2760_device_info *di = sl->family_data;
  634. unregister_pm_notifier(&di->pm_notifier);
  635. cancel_delayed_work_sync(&di->monitor_work);
  636. cancel_delayed_work_sync(&di->set_charged_work);
  637. destroy_workqueue(di->monitor_wqueue);
  638. }
  639. #ifdef CONFIG_OF
  640. static const struct of_device_id w1_ds2760_of_ids[] = {
  641. { .compatible = "maxim,ds2760" },
  642. {}
  643. };
  644. #endif
  645. static const struct w1_family_ops w1_ds2760_fops = {
  646. .add_slave = w1_ds2760_add_slave,
  647. .remove_slave = w1_ds2760_remove_slave,
  648. .groups = w1_ds2760_groups,
  649. };
  650. static struct w1_family w1_ds2760_family = {
  651. .fid = W1_FAMILY_DS2760,
  652. .fops = &w1_ds2760_fops,
  653. .of_match_table = of_match_ptr(w1_ds2760_of_ids),
  654. };
  655. module_w1_family(w1_ds2760_family);
  656. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
  657. "Matt Reimer <mreimer@vpop.net>, "
  658. "Anton Vorontsov <cbou@mail.ru>");
  659. MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
  660. MODULE_LICENSE("GPL");
  661. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));