bq27xxx_battery.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * BQ27xxx battery driver
  3. *
  4. * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
  5. * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
  6. * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
  7. * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
  8. *
  9. * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  10. *
  11. * This package is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. * Datasheets:
  20. * http://www.ti.com/product/bq27000
  21. * http://www.ti.com/product/bq27200
  22. * http://www.ti.com/product/bq27010
  23. * http://www.ti.com/product/bq27210
  24. * http://www.ti.com/product/bq27500
  25. * http://www.ti.com/product/bq27510-g3
  26. * http://www.ti.com/product/bq27520-g4
  27. * http://www.ti.com/product/bq27530-g1
  28. * http://www.ti.com/product/bq27531-g1
  29. * http://www.ti.com/product/bq27541-g1
  30. * http://www.ti.com/product/bq27542-g1
  31. * http://www.ti.com/product/bq27546-g1
  32. * http://www.ti.com/product/bq27742-g1
  33. * http://www.ti.com/product/bq27545-g1
  34. * http://www.ti.com/product/bq27421-g1
  35. * http://www.ti.com/product/bq27425-g1
  36. * http://www.ti.com/product/bq27411-g1
  37. * http://www.ti.com/product/bq27621-g1
  38. */
  39. #include <linux/device.h>
  40. #include <linux/module.h>
  41. #include <linux/param.h>
  42. #include <linux/jiffies.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/delay.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/power_supply.h>
  47. #include <linux/idr.h>
  48. #include <linux/i2c.h>
  49. #include <linux/slab.h>
  50. #include <linux/interrupt.h>
  51. #include <asm/unaligned.h>
  52. #include <linux/power/bq27xxx_battery.h>
  53. #define DRIVER_VERSION "1.2.0"
  54. #define BQ27XXX_MANUFACTURER "Texas Instruments"
  55. /* BQ27XXX Flags */
  56. #define BQ27XXX_FLAG_DSC BIT(0)
  57. #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
  58. #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
  59. #define BQ27XXX_FLAG_FC BIT(9)
  60. #define BQ27XXX_FLAG_OTD BIT(14)
  61. #define BQ27XXX_FLAG_OTC BIT(15)
  62. #define BQ27XXX_FLAG_UT BIT(14)
  63. #define BQ27XXX_FLAG_OT BIT(15)
  64. /* BQ27000 has different layout for Flags register */
  65. #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
  66. #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
  67. #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
  68. #define BQ27000_FLAG_FC BIT(5)
  69. #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
  70. #define BQ27XXX_RS (20) /* Resistor sense mOhm */
  71. #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
  72. #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
  73. struct bq27xxx_device_info;
  74. struct bq27xxx_access_methods {
  75. int (*read)(struct bq27xxx_device_info *di, u8 reg, bool single);
  76. };
  77. #define INVALID_REG_ADDR 0xff
  78. /*
  79. * bq27xxx_reg_index - Register names
  80. *
  81. * These are indexes into a device's register mapping array.
  82. */
  83. enum bq27xxx_reg_index {
  84. BQ27XXX_REG_CTRL = 0, /* Control */
  85. BQ27XXX_REG_TEMP, /* Temperature */
  86. BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
  87. BQ27XXX_REG_VOLT, /* Voltage */
  88. BQ27XXX_REG_AI, /* Average Current */
  89. BQ27XXX_REG_FLAGS, /* Flags */
  90. BQ27XXX_REG_TTE, /* Time-to-Empty */
  91. BQ27XXX_REG_TTF, /* Time-to-Full */
  92. BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
  93. BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
  94. BQ27XXX_REG_NAC, /* Nominal Available Capacity */
  95. BQ27XXX_REG_FCC, /* Full Charge Capacity */
  96. BQ27XXX_REG_CYCT, /* Cycle Count */
  97. BQ27XXX_REG_AE, /* Available Energy */
  98. BQ27XXX_REG_SOC, /* State-of-Charge */
  99. BQ27XXX_REG_DCAP, /* Design Capacity */
  100. BQ27XXX_REG_AP, /* Average Power */
  101. };
  102. struct bq27xxx_reg_cache {
  103. int temperature;
  104. int time_to_empty;
  105. int time_to_empty_avg;
  106. int time_to_full;
  107. int charge_full;
  108. int cycle_count;
  109. int capacity;
  110. int energy;
  111. int flags;
  112. int power_avg;
  113. int health;
  114. };
  115. struct bq27xxx_device_info {
  116. struct device *dev;
  117. int id;
  118. enum bq27xxx_chip chip;
  119. struct bq27xxx_reg_cache cache;
  120. int charge_design_full;
  121. unsigned long last_update;
  122. struct delayed_work work;
  123. struct power_supply *bat;
  124. struct bq27xxx_access_methods bus;
  125. struct mutex lock;
  126. u8 *regs;
  127. };
  128. /* Register mappings */
  129. static u8 bq27000_regs[] = {
  130. 0x00, /* CONTROL */
  131. 0x06, /* TEMP */
  132. INVALID_REG_ADDR, /* INT TEMP - NA*/
  133. 0x08, /* VOLT */
  134. 0x14, /* AVG CURR */
  135. 0x0a, /* FLAGS */
  136. 0x16, /* TTE */
  137. 0x18, /* TTF */
  138. 0x1c, /* TTES */
  139. 0x26, /* TTECP */
  140. 0x0c, /* NAC */
  141. 0x12, /* LMD(FCC) */
  142. 0x2a, /* CYCT */
  143. 0x22, /* AE */
  144. 0x0b, /* SOC(RSOC) */
  145. 0x76, /* DCAP(ILMD) */
  146. 0x24, /* AP */
  147. };
  148. static u8 bq27010_regs[] = {
  149. 0x00, /* CONTROL */
  150. 0x06, /* TEMP */
  151. INVALID_REG_ADDR, /* INT TEMP - NA*/
  152. 0x08, /* VOLT */
  153. 0x14, /* AVG CURR */
  154. 0x0a, /* FLAGS */
  155. 0x16, /* TTE */
  156. 0x18, /* TTF */
  157. 0x1c, /* TTES */
  158. 0x26, /* TTECP */
  159. 0x0c, /* NAC */
  160. 0x12, /* LMD(FCC) */
  161. 0x2a, /* CYCT */
  162. INVALID_REG_ADDR, /* AE - NA */
  163. 0x0b, /* SOC(RSOC) */
  164. 0x76, /* DCAP(ILMD) */
  165. INVALID_REG_ADDR, /* AP - NA */
  166. };
  167. static u8 bq27500_regs[] = {
  168. 0x00, /* CONTROL */
  169. 0x06, /* TEMP */
  170. 0x28, /* INT TEMP */
  171. 0x08, /* VOLT */
  172. 0x14, /* AVG CURR */
  173. 0x0a, /* FLAGS */
  174. 0x16, /* TTE */
  175. INVALID_REG_ADDR, /* TTF - NA */
  176. 0x1a, /* TTES */
  177. INVALID_REG_ADDR, /* TTECP - NA */
  178. 0x0c, /* NAC */
  179. 0x12, /* LMD(FCC) */
  180. 0x1e, /* CYCT */
  181. INVALID_REG_ADDR, /* AE - NA */
  182. 0x20, /* SOC(RSOC) */
  183. 0x2e, /* DCAP(ILMD) */
  184. INVALID_REG_ADDR, /* AP - NA */
  185. };
  186. static u8 bq27530_regs[] = {
  187. 0x00, /* CONTROL */
  188. 0x06, /* TEMP */
  189. 0x32, /* INT TEMP */
  190. 0x08, /* VOLT */
  191. 0x14, /* AVG CURR */
  192. 0x0a, /* FLAGS */
  193. 0x16, /* TTE */
  194. INVALID_REG_ADDR, /* TTF - NA */
  195. INVALID_REG_ADDR, /* TTES - NA */
  196. INVALID_REG_ADDR, /* TTECP - NA */
  197. 0x0c, /* NAC */
  198. 0x12, /* LMD(FCC) */
  199. 0x2a, /* CYCT */
  200. INVALID_REG_ADDR, /* AE - NA */
  201. 0x2c, /* SOC(RSOC) */
  202. INVALID_REG_ADDR, /* DCAP - NA */
  203. 0x24, /* AP */
  204. };
  205. static u8 bq27541_regs[] = {
  206. 0x00, /* CONTROL */
  207. 0x06, /* TEMP */
  208. 0x28, /* INT TEMP */
  209. 0x08, /* VOLT */
  210. 0x14, /* AVG CURR */
  211. 0x0a, /* FLAGS */
  212. 0x16, /* TTE */
  213. INVALID_REG_ADDR, /* TTF - NA */
  214. INVALID_REG_ADDR, /* TTES - NA */
  215. INVALID_REG_ADDR, /* TTECP - NA */
  216. 0x0c, /* NAC */
  217. 0x12, /* LMD(FCC) */
  218. 0x2a, /* CYCT */
  219. INVALID_REG_ADDR, /* AE - NA */
  220. 0x2c, /* SOC(RSOC) */
  221. 0x3c, /* DCAP */
  222. 0x76, /* AP */
  223. };
  224. static u8 bq27545_regs[] = {
  225. 0x00, /* CONTROL */
  226. 0x06, /* TEMP */
  227. 0x28, /* INT TEMP */
  228. 0x08, /* VOLT */
  229. 0x14, /* AVG CURR */
  230. 0x0a, /* FLAGS */
  231. 0x16, /* TTE */
  232. INVALID_REG_ADDR, /* TTF - NA */
  233. INVALID_REG_ADDR, /* TTES - NA */
  234. INVALID_REG_ADDR, /* TTECP - NA */
  235. 0x0c, /* NAC */
  236. 0x12, /* LMD(FCC) */
  237. 0x2a, /* CYCT */
  238. INVALID_REG_ADDR, /* AE - NA */
  239. 0x2c, /* SOC(RSOC) */
  240. INVALID_REG_ADDR, /* DCAP - NA */
  241. 0x24, /* AP */
  242. };
  243. static u8 bq27421_regs[] = {
  244. 0x00, /* CONTROL */
  245. 0x02, /* TEMP */
  246. 0x1e, /* INT TEMP */
  247. 0x04, /* VOLT */
  248. 0x10, /* AVG CURR */
  249. 0x06, /* FLAGS */
  250. INVALID_REG_ADDR, /* TTE - NA */
  251. INVALID_REG_ADDR, /* TTF - NA */
  252. INVALID_REG_ADDR, /* TTES - NA */
  253. INVALID_REG_ADDR, /* TTECP - NA */
  254. 0x08, /* NAC */
  255. 0x0e, /* FCC */
  256. INVALID_REG_ADDR, /* CYCT - NA */
  257. INVALID_REG_ADDR, /* AE - NA */
  258. 0x1c, /* SOC */
  259. 0x3c, /* DCAP */
  260. 0x18, /* AP */
  261. };
  262. static u8 *bq27xxx_regs[] __maybe_unused = {
  263. [BQ27000] = bq27000_regs,
  264. [BQ27010] = bq27010_regs,
  265. [BQ27500] = bq27500_regs,
  266. [BQ27530] = bq27530_regs,
  267. [BQ27541] = bq27541_regs,
  268. [BQ27545] = bq27545_regs,
  269. [BQ27421] = bq27421_regs,
  270. };
  271. static enum power_supply_property bq27000_battery_props[] = {
  272. POWER_SUPPLY_PROP_STATUS,
  273. POWER_SUPPLY_PROP_PRESENT,
  274. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  275. POWER_SUPPLY_PROP_CURRENT_NOW,
  276. POWER_SUPPLY_PROP_CAPACITY,
  277. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  278. POWER_SUPPLY_PROP_TEMP,
  279. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  280. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  281. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  282. POWER_SUPPLY_PROP_TECHNOLOGY,
  283. POWER_SUPPLY_PROP_CHARGE_FULL,
  284. POWER_SUPPLY_PROP_CHARGE_NOW,
  285. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  286. POWER_SUPPLY_PROP_CYCLE_COUNT,
  287. POWER_SUPPLY_PROP_ENERGY_NOW,
  288. POWER_SUPPLY_PROP_POWER_AVG,
  289. POWER_SUPPLY_PROP_HEALTH,
  290. POWER_SUPPLY_PROP_MANUFACTURER,
  291. };
  292. static enum power_supply_property bq27010_battery_props[] = {
  293. POWER_SUPPLY_PROP_STATUS,
  294. POWER_SUPPLY_PROP_PRESENT,
  295. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  296. POWER_SUPPLY_PROP_CURRENT_NOW,
  297. POWER_SUPPLY_PROP_CAPACITY,
  298. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  299. POWER_SUPPLY_PROP_TEMP,
  300. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  301. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  302. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  303. POWER_SUPPLY_PROP_TECHNOLOGY,
  304. POWER_SUPPLY_PROP_CHARGE_FULL,
  305. POWER_SUPPLY_PROP_CHARGE_NOW,
  306. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  307. POWER_SUPPLY_PROP_CYCLE_COUNT,
  308. POWER_SUPPLY_PROP_HEALTH,
  309. POWER_SUPPLY_PROP_MANUFACTURER,
  310. };
  311. static enum power_supply_property bq27500_battery_props[] = {
  312. POWER_SUPPLY_PROP_STATUS,
  313. POWER_SUPPLY_PROP_PRESENT,
  314. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  315. POWER_SUPPLY_PROP_CURRENT_NOW,
  316. POWER_SUPPLY_PROP_CAPACITY,
  317. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  318. POWER_SUPPLY_PROP_TEMP,
  319. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  320. POWER_SUPPLY_PROP_TECHNOLOGY,
  321. POWER_SUPPLY_PROP_CHARGE_FULL,
  322. POWER_SUPPLY_PROP_CHARGE_NOW,
  323. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  324. POWER_SUPPLY_PROP_CYCLE_COUNT,
  325. POWER_SUPPLY_PROP_HEALTH,
  326. POWER_SUPPLY_PROP_MANUFACTURER,
  327. };
  328. static enum power_supply_property bq27530_battery_props[] = {
  329. POWER_SUPPLY_PROP_STATUS,
  330. POWER_SUPPLY_PROP_PRESENT,
  331. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  332. POWER_SUPPLY_PROP_CURRENT_NOW,
  333. POWER_SUPPLY_PROP_CAPACITY,
  334. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  335. POWER_SUPPLY_PROP_TEMP,
  336. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  337. POWER_SUPPLY_PROP_TECHNOLOGY,
  338. POWER_SUPPLY_PROP_CHARGE_FULL,
  339. POWER_SUPPLY_PROP_CHARGE_NOW,
  340. POWER_SUPPLY_PROP_POWER_AVG,
  341. POWER_SUPPLY_PROP_HEALTH,
  342. POWER_SUPPLY_PROP_CYCLE_COUNT,
  343. POWER_SUPPLY_PROP_MANUFACTURER,
  344. };
  345. static enum power_supply_property bq27541_battery_props[] = {
  346. POWER_SUPPLY_PROP_STATUS,
  347. POWER_SUPPLY_PROP_PRESENT,
  348. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  349. POWER_SUPPLY_PROP_CURRENT_NOW,
  350. POWER_SUPPLY_PROP_CAPACITY,
  351. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  352. POWER_SUPPLY_PROP_TEMP,
  353. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  354. POWER_SUPPLY_PROP_TECHNOLOGY,
  355. POWER_SUPPLY_PROP_CHARGE_FULL,
  356. POWER_SUPPLY_PROP_CHARGE_NOW,
  357. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  358. POWER_SUPPLY_PROP_CYCLE_COUNT,
  359. POWER_SUPPLY_PROP_POWER_AVG,
  360. POWER_SUPPLY_PROP_HEALTH,
  361. POWER_SUPPLY_PROP_MANUFACTURER,
  362. };
  363. static enum power_supply_property bq27545_battery_props[] = {
  364. POWER_SUPPLY_PROP_STATUS,
  365. POWER_SUPPLY_PROP_PRESENT,
  366. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  367. POWER_SUPPLY_PROP_CURRENT_NOW,
  368. POWER_SUPPLY_PROP_CAPACITY,
  369. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  370. POWER_SUPPLY_PROP_TEMP,
  371. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  372. POWER_SUPPLY_PROP_TECHNOLOGY,
  373. POWER_SUPPLY_PROP_CHARGE_FULL,
  374. POWER_SUPPLY_PROP_CHARGE_NOW,
  375. POWER_SUPPLY_PROP_HEALTH,
  376. POWER_SUPPLY_PROP_CYCLE_COUNT,
  377. POWER_SUPPLY_PROP_POWER_AVG,
  378. POWER_SUPPLY_PROP_MANUFACTURER,
  379. };
  380. static enum power_supply_property bq27421_battery_props[] = {
  381. POWER_SUPPLY_PROP_STATUS,
  382. POWER_SUPPLY_PROP_PRESENT,
  383. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  384. POWER_SUPPLY_PROP_CURRENT_NOW,
  385. POWER_SUPPLY_PROP_CAPACITY,
  386. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  387. POWER_SUPPLY_PROP_TEMP,
  388. POWER_SUPPLY_PROP_TECHNOLOGY,
  389. POWER_SUPPLY_PROP_CHARGE_FULL,
  390. POWER_SUPPLY_PROP_CHARGE_NOW,
  391. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  392. POWER_SUPPLY_PROP_MANUFACTURER,
  393. };
  394. #define BQ27XXX_PROP(_id, _prop) \
  395. [_id] = { \
  396. .props = _prop, \
  397. .size = ARRAY_SIZE(_prop), \
  398. }
  399. static struct {
  400. enum power_supply_property *props;
  401. size_t size;
  402. } bq27xxx_battery_props[] = {
  403. BQ27XXX_PROP(BQ27000, bq27000_battery_props),
  404. BQ27XXX_PROP(BQ27010, bq27010_battery_props),
  405. BQ27XXX_PROP(BQ27500, bq27500_battery_props),
  406. BQ27XXX_PROP(BQ27530, bq27530_battery_props),
  407. BQ27XXX_PROP(BQ27541, bq27541_battery_props),
  408. BQ27XXX_PROP(BQ27545, bq27545_battery_props),
  409. BQ27XXX_PROP(BQ27421, bq27421_battery_props),
  410. };
  411. static unsigned int poll_interval = 360;
  412. module_param(poll_interval, uint, 0644);
  413. MODULE_PARM_DESC(poll_interval,
  414. "battery poll interval in seconds - 0 disables polling");
  415. /*
  416. * Common code for BQ27xxx devices
  417. */
  418. static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
  419. bool single)
  420. {
  421. /* Reports EINVAL for invalid/missing registers */
  422. if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
  423. return -EINVAL;
  424. return di->bus.read(di, di->regs[reg_index], single);
  425. }
  426. /*
  427. * Return the battery State-of-Charge
  428. * Or < 0 if something fails.
  429. */
  430. static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
  431. {
  432. int soc;
  433. soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
  434. if (soc < 0)
  435. dev_dbg(di->dev, "error reading State-of-Charge\n");
  436. return soc;
  437. }
  438. /*
  439. * Return a battery charge value in µAh
  440. * Or < 0 if something fails.
  441. */
  442. static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
  443. {
  444. int charge;
  445. charge = bq27xxx_read(di, reg, false);
  446. if (charge < 0) {
  447. dev_dbg(di->dev, "error reading charge register %02x: %d\n",
  448. reg, charge);
  449. return charge;
  450. }
  451. if (di->chip == BQ27000 || di->chip == BQ27010)
  452. charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
  453. else
  454. charge *= 1000;
  455. return charge;
  456. }
  457. /*
  458. * Return the battery Nominal available capacity in µAh
  459. * Or < 0 if something fails.
  460. */
  461. static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
  462. {
  463. int flags;
  464. if (di->chip == BQ27000 || di->chip == BQ27010) {
  465. flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
  466. if (flags >= 0 && (flags & BQ27000_FLAG_CI))
  467. return -ENODATA;
  468. }
  469. return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
  470. }
  471. /*
  472. * Return the battery Full Charge Capacity in µAh
  473. * Or < 0 if something fails.
  474. */
  475. static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
  476. {
  477. return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
  478. }
  479. /*
  480. * Return the Design Capacity in µAh
  481. * Or < 0 if something fails.
  482. */
  483. static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
  484. {
  485. int dcap;
  486. dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
  487. if (dcap < 0) {
  488. dev_dbg(di->dev, "error reading initial last measured discharge\n");
  489. return dcap;
  490. }
  491. if (di->chip == BQ27000 || di->chip == BQ27010)
  492. dcap *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
  493. else
  494. dcap *= 1000;
  495. return dcap;
  496. }
  497. /*
  498. * Return the battery Available energy in µWh
  499. * Or < 0 if something fails.
  500. */
  501. static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
  502. {
  503. int ae;
  504. ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
  505. if (ae < 0) {
  506. dev_dbg(di->dev, "error reading available energy\n");
  507. return ae;
  508. }
  509. if (di->chip == BQ27000 || di->chip == BQ27010)
  510. ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
  511. else
  512. ae *= 1000;
  513. return ae;
  514. }
  515. /*
  516. * Return the battery temperature in tenths of degree Kelvin
  517. * Or < 0 if something fails.
  518. */
  519. static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
  520. {
  521. int temp;
  522. temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
  523. if (temp < 0) {
  524. dev_err(di->dev, "error reading temperature\n");
  525. return temp;
  526. }
  527. if (di->chip == BQ27000 || di->chip == BQ27010)
  528. temp = 5 * temp / 2;
  529. return temp;
  530. }
  531. /*
  532. * Return the battery Cycle count total
  533. * Or < 0 if something fails.
  534. */
  535. static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
  536. {
  537. int cyct;
  538. cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
  539. if (cyct < 0)
  540. dev_err(di->dev, "error reading cycle count total\n");
  541. return cyct;
  542. }
  543. /*
  544. * Read a time register.
  545. * Return < 0 if something fails.
  546. */
  547. static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
  548. {
  549. int tval;
  550. tval = bq27xxx_read(di, reg, false);
  551. if (tval < 0) {
  552. dev_dbg(di->dev, "error reading time register %02x: %d\n",
  553. reg, tval);
  554. return tval;
  555. }
  556. if (tval == 65535)
  557. return -ENODATA;
  558. return tval * 60;
  559. }
  560. /*
  561. * Read an average power register.
  562. * Return < 0 if something fails.
  563. */
  564. static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
  565. {
  566. int tval;
  567. tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
  568. if (tval < 0) {
  569. dev_err(di->dev, "error reading average power register %02x: %d\n",
  570. BQ27XXX_REG_AP, tval);
  571. return tval;
  572. }
  573. if (di->chip == BQ27000 || di->chip == BQ27010)
  574. return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
  575. else
  576. return tval;
  577. }
  578. /*
  579. * Returns true if a battery over temperature condition is detected
  580. */
  581. static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
  582. {
  583. if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
  584. return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
  585. if (di->chip == BQ27530 || di->chip == BQ27421)
  586. return flags & BQ27XXX_FLAG_OT;
  587. return false;
  588. }
  589. /*
  590. * Returns true if a battery under temperature condition is detected
  591. */
  592. static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
  593. {
  594. if (di->chip == BQ27530 || di->chip == BQ27421)
  595. return flags & BQ27XXX_FLAG_UT;
  596. return false;
  597. }
  598. /*
  599. * Returns true if a low state of charge condition is detected
  600. */
  601. static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
  602. {
  603. if (di->chip == BQ27000 || di->chip == BQ27010)
  604. return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
  605. else
  606. return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
  607. }
  608. /*
  609. * Read flag register.
  610. * Return < 0 if something fails.
  611. */
  612. static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
  613. {
  614. int flags;
  615. flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
  616. if (flags < 0) {
  617. dev_err(di->dev, "error reading flag register:%d\n", flags);
  618. return flags;
  619. }
  620. /* Unlikely but important to return first */
  621. if (unlikely(bq27xxx_battery_overtemp(di, flags)))
  622. return POWER_SUPPLY_HEALTH_OVERHEAT;
  623. if (unlikely(bq27xxx_battery_undertemp(di, flags)))
  624. return POWER_SUPPLY_HEALTH_COLD;
  625. if (unlikely(bq27xxx_battery_dead(di, flags)))
  626. return POWER_SUPPLY_HEALTH_DEAD;
  627. return POWER_SUPPLY_HEALTH_GOOD;
  628. }
  629. static void bq27xxx_battery_update(struct bq27xxx_device_info *di)
  630. {
  631. struct bq27xxx_reg_cache cache = {0, };
  632. bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
  633. bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
  634. cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
  635. if ((cache.flags & 0xff) == 0xff)
  636. cache.flags = -1; /* read error */
  637. if (cache.flags >= 0) {
  638. cache.temperature = bq27xxx_battery_read_temperature(di);
  639. if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
  640. dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
  641. cache.capacity = -ENODATA;
  642. cache.energy = -ENODATA;
  643. cache.time_to_empty = -ENODATA;
  644. cache.time_to_empty_avg = -ENODATA;
  645. cache.time_to_full = -ENODATA;
  646. cache.charge_full = -ENODATA;
  647. cache.health = -ENODATA;
  648. } else {
  649. if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
  650. cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
  651. if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
  652. cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
  653. if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
  654. cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
  655. cache.charge_full = bq27xxx_battery_read_fcc(di);
  656. cache.capacity = bq27xxx_battery_read_soc(di);
  657. if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
  658. cache.energy = bq27xxx_battery_read_energy(di);
  659. cache.health = bq27xxx_battery_read_health(di);
  660. }
  661. if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
  662. cache.cycle_count = bq27xxx_battery_read_cyct(di);
  663. if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
  664. cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
  665. /* We only have to read charge design full once */
  666. if (di->charge_design_full <= 0)
  667. di->charge_design_full = bq27xxx_battery_read_dcap(di);
  668. }
  669. if (di->cache.capacity != cache.capacity)
  670. power_supply_changed(di->bat);
  671. if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
  672. di->cache = cache;
  673. di->last_update = jiffies;
  674. }
  675. static void bq27xxx_battery_poll(struct work_struct *work)
  676. {
  677. struct bq27xxx_device_info *di =
  678. container_of(work, struct bq27xxx_device_info,
  679. work.work);
  680. bq27xxx_battery_update(di);
  681. if (poll_interval > 0) {
  682. /* The timer does not have to be accurate. */
  683. set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
  684. schedule_delayed_work(&di->work, poll_interval * HZ);
  685. }
  686. }
  687. /*
  688. * Return the battery average current in µA
  689. * Note that current can be negative signed as well
  690. * Or 0 if something fails.
  691. */
  692. static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
  693. union power_supply_propval *val)
  694. {
  695. int curr;
  696. int flags;
  697. curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
  698. if (curr < 0) {
  699. dev_err(di->dev, "error reading current\n");
  700. return curr;
  701. }
  702. if (di->chip == BQ27000 || di->chip == BQ27010) {
  703. flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
  704. if (flags & BQ27000_FLAG_CHGS) {
  705. dev_dbg(di->dev, "negative current!\n");
  706. curr = -curr;
  707. }
  708. val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
  709. } else {
  710. /* Other gauges return signed value */
  711. val->intval = (int)((s16)curr) * 1000;
  712. }
  713. return 0;
  714. }
  715. static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
  716. union power_supply_propval *val)
  717. {
  718. int status;
  719. if (di->chip == BQ27000 || di->chip == BQ27010) {
  720. if (di->cache.flags & BQ27000_FLAG_FC)
  721. status = POWER_SUPPLY_STATUS_FULL;
  722. else if (di->cache.flags & BQ27000_FLAG_CHGS)
  723. status = POWER_SUPPLY_STATUS_CHARGING;
  724. else if (power_supply_am_i_supplied(di->bat))
  725. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  726. else
  727. status = POWER_SUPPLY_STATUS_DISCHARGING;
  728. } else {
  729. if (di->cache.flags & BQ27XXX_FLAG_FC)
  730. status = POWER_SUPPLY_STATUS_FULL;
  731. else if (di->cache.flags & BQ27XXX_FLAG_DSC)
  732. status = POWER_SUPPLY_STATUS_DISCHARGING;
  733. else
  734. status = POWER_SUPPLY_STATUS_CHARGING;
  735. }
  736. val->intval = status;
  737. return 0;
  738. }
  739. static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
  740. union power_supply_propval *val)
  741. {
  742. int level;
  743. if (di->chip == BQ27000 || di->chip == BQ27010) {
  744. if (di->cache.flags & BQ27000_FLAG_FC)
  745. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  746. else if (di->cache.flags & BQ27000_FLAG_EDV1)
  747. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  748. else if (di->cache.flags & BQ27000_FLAG_EDVF)
  749. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  750. else
  751. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  752. } else {
  753. if (di->cache.flags & BQ27XXX_FLAG_FC)
  754. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  755. else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
  756. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  757. else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
  758. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  759. else
  760. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  761. }
  762. val->intval = level;
  763. return 0;
  764. }
  765. /*
  766. * Return the battery Voltage in millivolts
  767. * Or < 0 if something fails.
  768. */
  769. static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
  770. union power_supply_propval *val)
  771. {
  772. int volt;
  773. volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
  774. if (volt < 0) {
  775. dev_err(di->dev, "error reading voltage\n");
  776. return volt;
  777. }
  778. val->intval = volt * 1000;
  779. return 0;
  780. }
  781. static int bq27xxx_simple_value(int value,
  782. union power_supply_propval *val)
  783. {
  784. if (value < 0)
  785. return value;
  786. val->intval = value;
  787. return 0;
  788. }
  789. static int bq27xxx_battery_get_property(struct power_supply *psy,
  790. enum power_supply_property psp,
  791. union power_supply_propval *val)
  792. {
  793. int ret = 0;
  794. struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
  795. mutex_lock(&di->lock);
  796. if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
  797. cancel_delayed_work_sync(&di->work);
  798. bq27xxx_battery_poll(&di->work.work);
  799. }
  800. mutex_unlock(&di->lock);
  801. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  802. return -ENODEV;
  803. switch (psp) {
  804. case POWER_SUPPLY_PROP_STATUS:
  805. ret = bq27xxx_battery_status(di, val);
  806. break;
  807. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  808. ret = bq27xxx_battery_voltage(di, val);
  809. break;
  810. case POWER_SUPPLY_PROP_PRESENT:
  811. val->intval = di->cache.flags < 0 ? 0 : 1;
  812. break;
  813. case POWER_SUPPLY_PROP_CURRENT_NOW:
  814. ret = bq27xxx_battery_current(di, val);
  815. break;
  816. case POWER_SUPPLY_PROP_CAPACITY:
  817. ret = bq27xxx_simple_value(di->cache.capacity, val);
  818. break;
  819. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  820. ret = bq27xxx_battery_capacity_level(di, val);
  821. break;
  822. case POWER_SUPPLY_PROP_TEMP:
  823. ret = bq27xxx_simple_value(di->cache.temperature, val);
  824. if (ret == 0)
  825. val->intval -= 2731; /* convert decidegree k to c */
  826. break;
  827. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  828. ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
  829. break;
  830. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  831. ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
  832. break;
  833. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  834. ret = bq27xxx_simple_value(di->cache.time_to_full, val);
  835. break;
  836. case POWER_SUPPLY_PROP_TECHNOLOGY:
  837. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  838. break;
  839. case POWER_SUPPLY_PROP_CHARGE_NOW:
  840. ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
  841. break;
  842. case POWER_SUPPLY_PROP_CHARGE_FULL:
  843. ret = bq27xxx_simple_value(di->cache.charge_full, val);
  844. break;
  845. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  846. ret = bq27xxx_simple_value(di->charge_design_full, val);
  847. break;
  848. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  849. ret = bq27xxx_simple_value(di->cache.cycle_count, val);
  850. break;
  851. case POWER_SUPPLY_PROP_ENERGY_NOW:
  852. ret = bq27xxx_simple_value(di->cache.energy, val);
  853. break;
  854. case POWER_SUPPLY_PROP_POWER_AVG:
  855. ret = bq27xxx_simple_value(di->cache.power_avg, val);
  856. break;
  857. case POWER_SUPPLY_PROP_HEALTH:
  858. ret = bq27xxx_simple_value(di->cache.health, val);
  859. break;
  860. case POWER_SUPPLY_PROP_MANUFACTURER:
  861. val->strval = BQ27XXX_MANUFACTURER;
  862. break;
  863. default:
  864. return -EINVAL;
  865. }
  866. return ret;
  867. }
  868. static void bq27xxx_external_power_changed(struct power_supply *psy)
  869. {
  870. struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
  871. cancel_delayed_work_sync(&di->work);
  872. schedule_delayed_work(&di->work, 0);
  873. }
  874. static int __maybe_unused bq27xxx_powersupply_init(struct bq27xxx_device_info *di,
  875. const char *name)
  876. {
  877. int ret;
  878. struct power_supply_desc *psy_desc;
  879. struct power_supply_config psy_cfg = { .drv_data = di, };
  880. psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
  881. if (!psy_desc)
  882. return -ENOMEM;
  883. psy_desc->name = name;
  884. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  885. psy_desc->properties = bq27xxx_battery_props[di->chip].props;
  886. psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
  887. psy_desc->get_property = bq27xxx_battery_get_property;
  888. psy_desc->external_power_changed = bq27xxx_external_power_changed;
  889. INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
  890. mutex_init(&di->lock);
  891. di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
  892. if (IS_ERR(di->bat)) {
  893. ret = PTR_ERR(di->bat);
  894. dev_err(di->dev, "failed to register battery: %d\n", ret);
  895. return ret;
  896. }
  897. dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  898. bq27xxx_battery_update(di);
  899. return 0;
  900. }
  901. static void __maybe_unused bq27xxx_powersupply_unregister(struct bq27xxx_device_info *di)
  902. {
  903. /*
  904. * power_supply_unregister call bq27xxx_battery_get_property which
  905. * call bq27xxx_battery_poll.
  906. * Make sure that bq27xxx_battery_poll will not call
  907. * schedule_delayed_work again after unregister (which cause OOPS).
  908. */
  909. poll_interval = 0;
  910. cancel_delayed_work_sync(&di->work);
  911. power_supply_unregister(di->bat);
  912. mutex_destroy(&di->lock);
  913. }
  914. /* i2c specific code */
  915. #ifdef CONFIG_BATTERY_BQ27XXX_I2C
  916. /* If the system has several batteries we need a different name for each
  917. * of them...
  918. */
  919. static DEFINE_IDR(battery_id);
  920. static DEFINE_MUTEX(battery_mutex);
  921. static irqreturn_t bq27xxx_battery_irq_handler_thread(int irq, void *data)
  922. {
  923. struct bq27xxx_device_info *di = data;
  924. bq27xxx_battery_update(di);
  925. return IRQ_HANDLED;
  926. }
  927. static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg,
  928. bool single)
  929. {
  930. struct i2c_client *client = to_i2c_client(di->dev);
  931. struct i2c_msg msg[2];
  932. unsigned char data[2];
  933. int ret;
  934. if (!client->adapter)
  935. return -ENODEV;
  936. msg[0].addr = client->addr;
  937. msg[0].flags = 0;
  938. msg[0].buf = &reg;
  939. msg[0].len = sizeof(reg);
  940. msg[1].addr = client->addr;
  941. msg[1].flags = I2C_M_RD;
  942. msg[1].buf = data;
  943. if (single)
  944. msg[1].len = 1;
  945. else
  946. msg[1].len = 2;
  947. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  948. if (ret < 0)
  949. return ret;
  950. if (!single)
  951. ret = get_unaligned_le16(data);
  952. else
  953. ret = data[0];
  954. return ret;
  955. }
  956. static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
  957. const struct i2c_device_id *id)
  958. {
  959. char *name;
  960. struct bq27xxx_device_info *di;
  961. int num;
  962. int retval = 0;
  963. /* Get new ID for the new battery device */
  964. mutex_lock(&battery_mutex);
  965. num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
  966. mutex_unlock(&battery_mutex);
  967. if (num < 0)
  968. return num;
  969. name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
  970. if (!name) {
  971. retval = -ENOMEM;
  972. goto batt_failed;
  973. }
  974. di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
  975. if (!di) {
  976. retval = -ENOMEM;
  977. goto batt_failed;
  978. }
  979. di->id = num;
  980. di->dev = &client->dev;
  981. di->chip = id->driver_data;
  982. di->bus.read = &bq27xxx_battery_i2c_read;
  983. di->regs = bq27xxx_regs[di->chip];
  984. retval = bq27xxx_powersupply_init(di, name);
  985. if (retval)
  986. goto batt_failed;
  987. /* Schedule a polling after about 1 min */
  988. schedule_delayed_work(&di->work, 60 * HZ);
  989. i2c_set_clientdata(client, di);
  990. if (client->irq) {
  991. retval = devm_request_threaded_irq(&client->dev, client->irq,
  992. NULL, bq27xxx_battery_irq_handler_thread,
  993. IRQF_ONESHOT,
  994. name, di);
  995. if (retval) {
  996. dev_err(&client->dev,
  997. "Unable to register IRQ %d error %d\n",
  998. client->irq, retval);
  999. return retval;
  1000. }
  1001. }
  1002. return 0;
  1003. batt_failed:
  1004. mutex_lock(&battery_mutex);
  1005. idr_remove(&battery_id, num);
  1006. mutex_unlock(&battery_mutex);
  1007. return retval;
  1008. }
  1009. static int bq27xxx_battery_i2c_remove(struct i2c_client *client)
  1010. {
  1011. struct bq27xxx_device_info *di = i2c_get_clientdata(client);
  1012. bq27xxx_powersupply_unregister(di);
  1013. mutex_lock(&battery_mutex);
  1014. idr_remove(&battery_id, di->id);
  1015. mutex_unlock(&battery_mutex);
  1016. return 0;
  1017. }
  1018. static const struct i2c_device_id bq27xxx_id[] = {
  1019. { "bq27200", BQ27000 },
  1020. { "bq27210", BQ27010 },
  1021. { "bq27500", BQ27500 },
  1022. { "bq27510", BQ27500 },
  1023. { "bq27520", BQ27500 },
  1024. { "bq27530", BQ27530 },
  1025. { "bq27531", BQ27530 },
  1026. { "bq27541", BQ27541 },
  1027. { "bq27542", BQ27541 },
  1028. { "bq27546", BQ27541 },
  1029. { "bq27742", BQ27541 },
  1030. { "bq27545", BQ27545 },
  1031. { "bq27421", BQ27421 },
  1032. { "bq27425", BQ27421 },
  1033. { "bq27441", BQ27421 },
  1034. { "bq27621", BQ27421 },
  1035. {},
  1036. };
  1037. MODULE_DEVICE_TABLE(i2c, bq27xxx_id);
  1038. static struct i2c_driver bq27xxx_battery_i2c_driver = {
  1039. .driver = {
  1040. .name = "bq27xxx-battery",
  1041. },
  1042. .probe = bq27xxx_battery_i2c_probe,
  1043. .remove = bq27xxx_battery_i2c_remove,
  1044. .id_table = bq27xxx_id,
  1045. };
  1046. static inline int bq27xxx_battery_i2c_init(void)
  1047. {
  1048. int ret = i2c_add_driver(&bq27xxx_battery_i2c_driver);
  1049. if (ret)
  1050. pr_err("Unable to register BQ27xxx i2c driver\n");
  1051. return ret;
  1052. }
  1053. static inline void bq27xxx_battery_i2c_exit(void)
  1054. {
  1055. i2c_del_driver(&bq27xxx_battery_i2c_driver);
  1056. }
  1057. #else
  1058. static inline int bq27xxx_battery_i2c_init(void) { return 0; }
  1059. static inline void bq27xxx_battery_i2c_exit(void) {};
  1060. #endif
  1061. /* platform specific code */
  1062. #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
  1063. static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
  1064. bool single)
  1065. {
  1066. struct device *dev = di->dev;
  1067. struct bq27xxx_platform_data *pdata = dev->platform_data;
  1068. unsigned int timeout = 3;
  1069. int upper, lower;
  1070. int temp;
  1071. if (!single) {
  1072. /* Make sure the value has not changed in between reading the
  1073. * lower and the upper part */
  1074. upper = pdata->read(dev, reg + 1);
  1075. do {
  1076. temp = upper;
  1077. if (upper < 0)
  1078. return upper;
  1079. lower = pdata->read(dev, reg);
  1080. if (lower < 0)
  1081. return lower;
  1082. upper = pdata->read(dev, reg + 1);
  1083. } while (temp != upper && --timeout);
  1084. if (timeout == 0)
  1085. return -EIO;
  1086. return (upper << 8) | lower;
  1087. }
  1088. return pdata->read(dev, reg);
  1089. }
  1090. static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
  1091. {
  1092. struct bq27xxx_device_info *di;
  1093. struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
  1094. const char *name;
  1095. if (!pdata) {
  1096. dev_err(&pdev->dev, "no platform_data supplied\n");
  1097. return -EINVAL;
  1098. }
  1099. if (!pdata->read) {
  1100. dev_err(&pdev->dev, "no hdq read callback supplied\n");
  1101. return -EINVAL;
  1102. }
  1103. if (!pdata->chip) {
  1104. dev_err(&pdev->dev, "no device supplied\n");
  1105. return -EINVAL;
  1106. }
  1107. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  1108. if (!di)
  1109. return -ENOMEM;
  1110. platform_set_drvdata(pdev, di);
  1111. di->dev = &pdev->dev;
  1112. di->chip = pdata->chip;
  1113. di->regs = bq27xxx_regs[di->chip];
  1114. name = pdata->name ?: dev_name(&pdev->dev);
  1115. di->bus.read = &bq27xxx_battery_platform_read;
  1116. return bq27xxx_powersupply_init(di, name);
  1117. }
  1118. static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
  1119. {
  1120. struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
  1121. bq27xxx_powersupply_unregister(di);
  1122. return 0;
  1123. }
  1124. static struct platform_driver bq27xxx_battery_platform_driver = {
  1125. .probe = bq27xxx_battery_platform_probe,
  1126. .remove = bq27xxx_battery_platform_remove,
  1127. .driver = {
  1128. .name = "bq27000-battery",
  1129. },
  1130. };
  1131. static inline int bq27xxx_battery_platform_init(void)
  1132. {
  1133. int ret = platform_driver_register(&bq27xxx_battery_platform_driver);
  1134. if (ret)
  1135. pr_err("Unable to register BQ27xxx platform driver\n");
  1136. return ret;
  1137. }
  1138. static inline void bq27xxx_battery_platform_exit(void)
  1139. {
  1140. platform_driver_unregister(&bq27xxx_battery_platform_driver);
  1141. }
  1142. #else
  1143. static inline int bq27xxx_battery_platform_init(void) { return 0; }
  1144. static inline void bq27xxx_battery_platform_exit(void) {};
  1145. #endif
  1146. /*
  1147. * Module stuff
  1148. */
  1149. static int __init bq27xxx_battery_init(void)
  1150. {
  1151. int ret;
  1152. ret = bq27xxx_battery_i2c_init();
  1153. if (ret)
  1154. return ret;
  1155. ret = bq27xxx_battery_platform_init();
  1156. if (ret)
  1157. bq27xxx_battery_i2c_exit();
  1158. return ret;
  1159. }
  1160. module_init(bq27xxx_battery_init);
  1161. static void __exit bq27xxx_battery_exit(void)
  1162. {
  1163. bq27xxx_battery_platform_exit();
  1164. bq27xxx_battery_i2c_exit();
  1165. }
  1166. module_exit(bq27xxx_battery_exit);
  1167. #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
  1168. MODULE_ALIAS("platform:bq27000-battery");
  1169. #endif
  1170. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  1171. MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
  1172. MODULE_LICENSE("GPL");