ds2760_battery.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 = container_of(kobj, struct device, 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. #ifdef CONFIG_MACH_H4700
  196. 1800, /* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */
  197. #else
  198. 1440, /* Lishen */
  199. #endif
  200. 1440, /* NEC */
  201. 2880, /* Samsung */
  202. 2880, /* BYD */
  203. 2880, /* Lishen */
  204. 2880, /* NEC */
  205. #ifdef CONFIG_MACH_H4700
  206. 0,
  207. 3600, /* HP iPAQ hx4700 3.7V 3600mAh (359114-001) */
  208. #endif
  209. };
  210. /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
  211. * temp is in Celsius */
  212. static int battery_interpolate(int array[], int temp)
  213. {
  214. int index, dt;
  215. if (temp <= 0)
  216. return array[0];
  217. if (temp >= 40)
  218. return array[4];
  219. index = temp / 10;
  220. dt = temp % 10;
  221. return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
  222. }
  223. static int ds2760_battery_read_status(struct ds2760_device_info *di)
  224. {
  225. int ret, i, start, count, scale[5];
  226. if (di->update_time && time_before(jiffies, di->update_time +
  227. msecs_to_jiffies(cache_time)))
  228. return 0;
  229. /* The first time we read the entire contents of SRAM/EEPROM,
  230. * but after that we just read the interesting bits that change. */
  231. if (di->update_time == 0) {
  232. start = 0;
  233. count = DS2760_DATA_SIZE;
  234. } else {
  235. start = DS2760_VOLTAGE_MSB;
  236. count = DS2760_TEMP_LSB - start + 1;
  237. }
  238. ret = w1_ds2760_read(di->dev, di->raw + start, start, count);
  239. if (ret != count) {
  240. dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
  241. di->dev);
  242. return 1;
  243. }
  244. di->update_time = jiffies;
  245. /* DS2760 reports voltage in units of 4.88mV, but the battery class
  246. * reports in units of uV, so convert by multiplying by 4880. */
  247. di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
  248. (di->raw[DS2760_VOLTAGE_LSB] >> 5);
  249. di->voltage_uV = di->voltage_raw * 4880;
  250. /* DS2760 reports current in signed units of 0.625mA, but the battery
  251. * class reports in units of µA, so convert by multiplying by 625. */
  252. di->current_raw =
  253. (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
  254. (di->raw[DS2760_CURRENT_LSB] >> 3);
  255. di->current_uA = di->current_raw * 625;
  256. /* DS2760 reports accumulated current in signed units of 0.25mAh. */
  257. di->accum_current_raw =
  258. (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
  259. di->raw[DS2760_CURRENT_ACCUM_LSB];
  260. di->accum_current_uAh = di->accum_current_raw * 250;
  261. /* DS2760 reports temperature in signed units of 0.125°C, but the
  262. * battery class reports in units of 1/10 °C, so we convert by
  263. * multiplying by .125 * 10 = 1.25. */
  264. di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
  265. (di->raw[DS2760_TEMP_LSB] >> 5);
  266. di->temp_C = di->temp_raw + (di->temp_raw / 4);
  267. /* At least some battery monitors (e.g. HP iPAQ) store the battery's
  268. * maximum rated capacity. */
  269. if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
  270. di->rated_capacity = rated_capacities[
  271. (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
  272. else
  273. di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
  274. di->rated_capacity *= 1000; /* convert to µAh */
  275. /* Calculate the full level at the present temperature. */
  276. di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
  277. di->raw[DS2760_ACTIVE_FULL + 1];
  278. /* If the full_active_uAh value is not given, fall back to the rated
  279. * capacity. This is likely to happen when chips are not part of the
  280. * battery pack and is therefore not bootstrapped. */
  281. if (di->full_active_uAh == 0)
  282. di->full_active_uAh = di->rated_capacity / 1000L;
  283. scale[0] = di->full_active_uAh;
  284. for (i = 1; i < 5; i++)
  285. scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i];
  286. di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
  287. di->full_active_uAh *= 1000; /* convert to µAh */
  288. /* Calculate the empty level at the present temperature. */
  289. scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
  290. for (i = 3; i >= 0; i--)
  291. scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
  292. di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
  293. di->empty_uAh *= 1000; /* convert to µAh */
  294. if (di->full_active_uAh == di->empty_uAh)
  295. di->rem_capacity = 0;
  296. else
  297. /* From Maxim Application Note 131: remaining capacity =
  298. * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
  299. di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
  300. (di->full_active_uAh - di->empty_uAh);
  301. if (di->rem_capacity < 0)
  302. di->rem_capacity = 0;
  303. if (di->rem_capacity > 100)
  304. di->rem_capacity = 100;
  305. if (di->current_uA < -100L)
  306. di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
  307. / (di->current_uA / 100L);
  308. else
  309. di->life_sec = 0;
  310. return 0;
  311. }
  312. static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
  313. unsigned int acr_val)
  314. {
  315. unsigned char acr[2];
  316. /* acr is in units of 0.25 mAh */
  317. acr_val *= 4L;
  318. acr_val /= 1000;
  319. acr[0] = acr_val >> 8;
  320. acr[1] = acr_val & 0xff;
  321. if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
  322. dev_warn(di->dev, "ACR write failed\n");
  323. }
  324. static void ds2760_battery_update_status(struct ds2760_device_info *di)
  325. {
  326. int old_charge_status = di->charge_status;
  327. ds2760_battery_read_status(di);
  328. if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
  329. di->full_counter = 0;
  330. if (power_supply_am_i_supplied(di->bat)) {
  331. if (di->current_uA > 10000) {
  332. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  333. di->full_counter = 0;
  334. } else if (di->current_uA < -5000) {
  335. if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
  336. dev_notice(di->dev, "not enough power to "
  337. "charge\n");
  338. di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  339. di->full_counter = 0;
  340. } else if (di->current_uA < 10000 &&
  341. di->charge_status != POWER_SUPPLY_STATUS_FULL) {
  342. /* Don't consider the battery to be full unless
  343. * we've seen the current < 10 mA at least two
  344. * consecutive times. */
  345. di->full_counter++;
  346. if (di->full_counter < 2) {
  347. di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
  348. } else {
  349. di->charge_status = POWER_SUPPLY_STATUS_FULL;
  350. ds2760_battery_set_current_accum(di,
  351. di->full_active_uAh);
  352. }
  353. }
  354. } else {
  355. di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
  356. di->full_counter = 0;
  357. }
  358. if (di->charge_status != old_charge_status)
  359. power_supply_changed(di->bat);
  360. }
  361. static void ds2760_battery_write_status(struct ds2760_device_info *di,
  362. char status)
  363. {
  364. if (status == di->raw[DS2760_STATUS_REG])
  365. return;
  366. w1_ds2760_write(di->dev, &status, DS2760_STATUS_WRITE_REG, 1);
  367. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  368. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  369. }
  370. static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
  371. unsigned char rated_capacity)
  372. {
  373. if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
  374. return;
  375. w1_ds2760_write(di->dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
  376. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  377. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  378. }
  379. static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
  380. int active_full)
  381. {
  382. unsigned char tmp[2] = {
  383. active_full >> 8,
  384. active_full & 0xff
  385. };
  386. if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
  387. tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
  388. return;
  389. w1_ds2760_write(di->dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
  390. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
  391. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK0);
  392. /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
  393. * values won't be read back by ds2760_battery_read_status() */
  394. di->raw[DS2760_ACTIVE_FULL] = tmp[0];
  395. di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
  396. }
  397. static void ds2760_battery_work(struct work_struct *work)
  398. {
  399. struct ds2760_device_info *di = container_of(work,
  400. struct ds2760_device_info, monitor_work.work);
  401. const int interval = HZ * 60;
  402. dev_dbg(di->dev, "%s\n", __func__);
  403. ds2760_battery_update_status(di);
  404. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
  405. }
  406. static void ds2760_battery_external_power_changed(struct power_supply *psy)
  407. {
  408. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  409. dev_dbg(di->dev, "%s\n", __func__);
  410. mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
  411. }
  412. static void ds2760_battery_set_charged_work(struct work_struct *work)
  413. {
  414. char bias;
  415. struct ds2760_device_info *di = container_of(work,
  416. struct ds2760_device_info, set_charged_work.work);
  417. dev_dbg(di->dev, "%s\n", __func__);
  418. ds2760_battery_read_status(di);
  419. /* When we get notified by external circuitry that the battery is
  420. * considered fully charged now, we know that there is no current
  421. * flow any more. However, the ds2760's internal current meter is
  422. * too inaccurate to rely on - spec say something ~15% failure.
  423. * Hence, we use the current offset bias register to compensate
  424. * that error.
  425. */
  426. if (!power_supply_am_i_supplied(di->bat))
  427. return;
  428. bias = (signed char) di->current_raw +
  429. (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
  430. dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
  431. w1_ds2760_write(di->dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1);
  432. w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  433. w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1);
  434. /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
  435. * value won't be read back by ds2760_battery_read_status() */
  436. di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias;
  437. }
  438. static void ds2760_battery_set_charged(struct power_supply *psy)
  439. {
  440. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  441. /* postpone the actual work by 20 secs. This is for debouncing GPIO
  442. * signals and to let the current value settle. See AN4188. */
  443. mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20);
  444. }
  445. static int ds2760_battery_get_property(struct power_supply *psy,
  446. enum power_supply_property psp,
  447. union power_supply_propval *val)
  448. {
  449. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  450. switch (psp) {
  451. case POWER_SUPPLY_PROP_STATUS:
  452. val->intval = di->charge_status;
  453. return 0;
  454. default:
  455. break;
  456. }
  457. ds2760_battery_read_status(di);
  458. switch (psp) {
  459. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  460. val->intval = di->voltage_uV;
  461. break;
  462. case POWER_SUPPLY_PROP_CURRENT_NOW:
  463. val->intval = di->current_uA;
  464. break;
  465. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  466. val->intval = di->rated_capacity;
  467. break;
  468. case POWER_SUPPLY_PROP_CHARGE_FULL:
  469. val->intval = di->full_active_uAh;
  470. break;
  471. case POWER_SUPPLY_PROP_CHARGE_EMPTY:
  472. val->intval = di->empty_uAh;
  473. break;
  474. case POWER_SUPPLY_PROP_CHARGE_NOW:
  475. val->intval = di->accum_current_uAh;
  476. break;
  477. case POWER_SUPPLY_PROP_TEMP:
  478. val->intval = di->temp_C;
  479. break;
  480. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  481. val->intval = di->life_sec;
  482. break;
  483. case POWER_SUPPLY_PROP_CAPACITY:
  484. val->intval = di->rem_capacity;
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. return 0;
  490. }
  491. static int ds2760_battery_set_property(struct power_supply *psy,
  492. enum power_supply_property psp,
  493. const union power_supply_propval *val)
  494. {
  495. struct ds2760_device_info *di = power_supply_get_drvdata(psy);
  496. switch (psp) {
  497. case POWER_SUPPLY_PROP_CHARGE_FULL:
  498. /* the interface counts in uAh, convert the value */
  499. ds2760_battery_write_active_full(di, val->intval / 1000L);
  500. break;
  501. case POWER_SUPPLY_PROP_CHARGE_NOW:
  502. /* ds2760_battery_set_current_accum() does the conversion */
  503. ds2760_battery_set_current_accum(di, val->intval);
  504. break;
  505. default:
  506. return -EPERM;
  507. }
  508. return 0;
  509. }
  510. static int ds2760_battery_property_is_writeable(struct power_supply *psy,
  511. enum power_supply_property psp)
  512. {
  513. switch (psp) {
  514. case POWER_SUPPLY_PROP_CHARGE_FULL:
  515. case POWER_SUPPLY_PROP_CHARGE_NOW:
  516. return 1;
  517. default:
  518. break;
  519. }
  520. return 0;
  521. }
  522. static enum power_supply_property ds2760_battery_props[] = {
  523. POWER_SUPPLY_PROP_STATUS,
  524. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  525. POWER_SUPPLY_PROP_CURRENT_NOW,
  526. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  527. POWER_SUPPLY_PROP_CHARGE_FULL,
  528. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  529. POWER_SUPPLY_PROP_CHARGE_NOW,
  530. POWER_SUPPLY_PROP_TEMP,
  531. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  532. POWER_SUPPLY_PROP_CAPACITY,
  533. };
  534. static int ds2760_pm_notifier(struct notifier_block *notifier,
  535. unsigned long pm_event,
  536. void *unused)
  537. {
  538. struct ds2760_device_info *di =
  539. container_of(notifier, struct ds2760_device_info, pm_notifier);
  540. switch (pm_event) {
  541. case PM_HIBERNATION_PREPARE:
  542. case PM_SUSPEND_PREPARE:
  543. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  544. break;
  545. case PM_POST_RESTORE:
  546. case PM_POST_HIBERNATION:
  547. case PM_POST_SUSPEND:
  548. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  549. power_supply_changed(di->bat);
  550. mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
  551. break;
  552. case PM_RESTORE_PREPARE:
  553. default:
  554. break;
  555. }
  556. return NOTIFY_DONE;
  557. }
  558. static int w1_ds2760_add_slave(struct w1_slave *sl)
  559. {
  560. struct power_supply_config psy_cfg = {};
  561. struct ds2760_device_info *di;
  562. struct device *dev = &sl->dev;
  563. int retval = 0;
  564. char name[32];
  565. char status;
  566. di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
  567. if (!di) {
  568. retval = -ENOMEM;
  569. goto di_alloc_failed;
  570. }
  571. snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id);
  572. di->dev = dev;
  573. di->bat_desc.name = name;
  574. di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  575. di->bat_desc.properties = ds2760_battery_props;
  576. di->bat_desc.num_properties = ARRAY_SIZE(ds2760_battery_props);
  577. di->bat_desc.get_property = ds2760_battery_get_property;
  578. di->bat_desc.set_property = ds2760_battery_set_property;
  579. di->bat_desc.property_is_writeable =
  580. ds2760_battery_property_is_writeable;
  581. di->bat_desc.set_charged = ds2760_battery_set_charged;
  582. di->bat_desc.external_power_changed =
  583. ds2760_battery_external_power_changed;
  584. psy_cfg.drv_data = di;
  585. if (dev->of_node) {
  586. u32 tmp;
  587. psy_cfg.of_node = dev->of_node;
  588. if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled"))
  589. pmod_enabled = true;
  590. if (!of_property_read_u32(dev->of_node,
  591. "maxim,cache-time-ms", &tmp))
  592. cache_time = tmp;
  593. if (!of_property_read_u32(dev->of_node,
  594. "rated-capacity-microamp-hours",
  595. &tmp))
  596. rated_capacity = tmp / 10; /* property is in mAh */
  597. }
  598. di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
  599. sl->family_data = di;
  600. /* enable sleep mode feature */
  601. ds2760_battery_read_status(di);
  602. status = di->raw[DS2760_STATUS_REG];
  603. if (pmod_enabled)
  604. status |= DS2760_STATUS_PMOD;
  605. else
  606. status &= ~DS2760_STATUS_PMOD;
  607. ds2760_battery_write_status(di, status);
  608. /* set rated capacity from module param or device tree */
  609. if (rated_capacity)
  610. ds2760_battery_write_rated_capacity(di, rated_capacity);
  611. /* set current accumulator if given as parameter.
  612. * this should only be done for bootstrapping the value */
  613. if (current_accum)
  614. ds2760_battery_set_current_accum(di, current_accum);
  615. di->bat = power_supply_register(dev, &di->bat_desc, &psy_cfg);
  616. if (IS_ERR(di->bat)) {
  617. dev_err(di->dev, "failed to register battery\n");
  618. retval = PTR_ERR(di->bat);
  619. goto batt_failed;
  620. }
  621. INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
  622. INIT_DELAYED_WORK(&di->set_charged_work,
  623. ds2760_battery_set_charged_work);
  624. di->monitor_wqueue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
  625. if (!di->monitor_wqueue) {
  626. retval = -ESRCH;
  627. goto workqueue_failed;
  628. }
  629. queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
  630. di->pm_notifier.notifier_call = ds2760_pm_notifier;
  631. register_pm_notifier(&di->pm_notifier);
  632. goto success;
  633. workqueue_failed:
  634. power_supply_unregister(di->bat);
  635. batt_failed:
  636. di_alloc_failed:
  637. success:
  638. return retval;
  639. }
  640. static void w1_ds2760_remove_slave(struct w1_slave *sl)
  641. {
  642. struct ds2760_device_info *di = sl->family_data;
  643. unregister_pm_notifier(&di->pm_notifier);
  644. cancel_delayed_work_sync(&di->monitor_work);
  645. cancel_delayed_work_sync(&di->set_charged_work);
  646. destroy_workqueue(di->monitor_wqueue);
  647. power_supply_unregister(di->bat);
  648. }
  649. #ifdef CONFIG_OF
  650. static const struct of_device_id w1_ds2760_of_ids[] = {
  651. { .compatible = "maxim,ds2760" },
  652. {}
  653. };
  654. #endif
  655. static const struct w1_family_ops w1_ds2760_fops = {
  656. .add_slave = w1_ds2760_add_slave,
  657. .remove_slave = w1_ds2760_remove_slave,
  658. .groups = w1_ds2760_groups,
  659. };
  660. static struct w1_family w1_ds2760_family = {
  661. .fid = W1_FAMILY_DS2760,
  662. .fops = &w1_ds2760_fops,
  663. .of_match_table = of_match_ptr(w1_ds2760_of_ids),
  664. };
  665. module_w1_family(w1_ds2760_family);
  666. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
  667. "Matt Reimer <mreimer@vpop.net>, "
  668. "Anton Vorontsov <cbou@mail.ru>");
  669. MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
  670. MODULE_LICENSE("GPL");
  671. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));