bq27xxx_battery.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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. * Copyright (C) 2017 Liam Breck <kernel@networkimprov.net>
  9. *
  10. * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  11. *
  12. * This package is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. *
  20. * Datasheets:
  21. * http://www.ti.com/product/bq27000
  22. * http://www.ti.com/product/bq27200
  23. * http://www.ti.com/product/bq27010
  24. * http://www.ti.com/product/bq27210
  25. * http://www.ti.com/product/bq27500
  26. * http://www.ti.com/product/bq27510-g1
  27. * http://www.ti.com/product/bq27510-g2
  28. * http://www.ti.com/product/bq27510-g3
  29. * http://www.ti.com/product/bq27520-g4
  30. * http://www.ti.com/product/bq27520-g1
  31. * http://www.ti.com/product/bq27520-g2
  32. * http://www.ti.com/product/bq27520-g3
  33. * http://www.ti.com/product/bq27520-g4
  34. * http://www.ti.com/product/bq27530-g1
  35. * http://www.ti.com/product/bq27531-g1
  36. * http://www.ti.com/product/bq27541-g1
  37. * http://www.ti.com/product/bq27542-g1
  38. * http://www.ti.com/product/bq27546-g1
  39. * http://www.ti.com/product/bq27742-g1
  40. * http://www.ti.com/product/bq27545-g1
  41. * http://www.ti.com/product/bq27421-g1
  42. * http://www.ti.com/product/bq27425-g1
  43. * http://www.ti.com/product/bq27411-g1
  44. * http://www.ti.com/product/bq27621-g1
  45. */
  46. #include <linux/device.h>
  47. #include <linux/module.h>
  48. #include <linux/mutex.h>
  49. #include <linux/param.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/workqueue.h>
  52. #include <linux/delay.h>
  53. #include <linux/platform_device.h>
  54. #include <linux/power_supply.h>
  55. #include <linux/slab.h>
  56. #include <linux/of.h>
  57. #include <linux/power/bq27xxx_battery.h>
  58. #define BQ27XXX_MANUFACTURER "Texas Instruments"
  59. /* BQ27XXX Flags */
  60. #define BQ27XXX_FLAG_DSC BIT(0)
  61. #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
  62. #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
  63. #define BQ27XXX_FLAG_CFGUP BIT(4)
  64. #define BQ27XXX_FLAG_FC BIT(9)
  65. #define BQ27XXX_FLAG_OTD BIT(14)
  66. #define BQ27XXX_FLAG_OTC BIT(15)
  67. #define BQ27XXX_FLAG_UT BIT(14)
  68. #define BQ27XXX_FLAG_OT BIT(15)
  69. /* BQ27000 has different layout for Flags register */
  70. #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
  71. #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
  72. #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
  73. #define BQ27000_FLAG_FC BIT(5)
  74. #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
  75. /* control register params */
  76. #define BQ27XXX_SEALED 0x20
  77. #define BQ27XXX_SET_CFGUPDATE 0x13
  78. #define BQ27XXX_SOFT_RESET 0x42
  79. #define BQ27XXX_RESET 0x41
  80. #define BQ27XXX_RS (20) /* Resistor sense mOhm */
  81. #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
  82. #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
  83. #define INVALID_REG_ADDR 0xff
  84. /*
  85. * bq27xxx_reg_index - Register names
  86. *
  87. * These are indexes into a device's register mapping array.
  88. */
  89. enum bq27xxx_reg_index {
  90. BQ27XXX_REG_CTRL = 0, /* Control */
  91. BQ27XXX_REG_TEMP, /* Temperature */
  92. BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
  93. BQ27XXX_REG_VOLT, /* Voltage */
  94. BQ27XXX_REG_AI, /* Average Current */
  95. BQ27XXX_REG_FLAGS, /* Flags */
  96. BQ27XXX_REG_TTE, /* Time-to-Empty */
  97. BQ27XXX_REG_TTF, /* Time-to-Full */
  98. BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
  99. BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
  100. BQ27XXX_REG_NAC, /* Nominal Available Capacity */
  101. BQ27XXX_REG_FCC, /* Full Charge Capacity */
  102. BQ27XXX_REG_CYCT, /* Cycle Count */
  103. BQ27XXX_REG_AE, /* Available Energy */
  104. BQ27XXX_REG_SOC, /* State-of-Charge */
  105. BQ27XXX_REG_DCAP, /* Design Capacity */
  106. BQ27XXX_REG_AP, /* Average Power */
  107. BQ27XXX_DM_CTRL, /* Block Data Control */
  108. BQ27XXX_DM_CLASS, /* Data Class */
  109. BQ27XXX_DM_BLOCK, /* Data Block */
  110. BQ27XXX_DM_DATA, /* Block Data */
  111. BQ27XXX_DM_CKSUM, /* Block Data Checksum */
  112. BQ27XXX_REG_MAX, /* sentinel */
  113. };
  114. #define BQ27XXX_DM_REG_ROWS \
  115. [BQ27XXX_DM_CTRL] = 0x61, \
  116. [BQ27XXX_DM_CLASS] = 0x3e, \
  117. [BQ27XXX_DM_BLOCK] = 0x3f, \
  118. [BQ27XXX_DM_DATA] = 0x40, \
  119. [BQ27XXX_DM_CKSUM] = 0x60
  120. /* Register mappings */
  121. static u8
  122. bq27000_regs[BQ27XXX_REG_MAX] = {
  123. [BQ27XXX_REG_CTRL] = 0x00,
  124. [BQ27XXX_REG_TEMP] = 0x06,
  125. [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
  126. [BQ27XXX_REG_VOLT] = 0x08,
  127. [BQ27XXX_REG_AI] = 0x14,
  128. [BQ27XXX_REG_FLAGS] = 0x0a,
  129. [BQ27XXX_REG_TTE] = 0x16,
  130. [BQ27XXX_REG_TTF] = 0x18,
  131. [BQ27XXX_REG_TTES] = 0x1c,
  132. [BQ27XXX_REG_TTECP] = 0x26,
  133. [BQ27XXX_REG_NAC] = 0x0c,
  134. [BQ27XXX_REG_FCC] = 0x12,
  135. [BQ27XXX_REG_CYCT] = 0x2a,
  136. [BQ27XXX_REG_AE] = 0x22,
  137. [BQ27XXX_REG_SOC] = 0x0b,
  138. [BQ27XXX_REG_DCAP] = 0x76,
  139. [BQ27XXX_REG_AP] = 0x24,
  140. [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
  141. [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
  142. [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
  143. [BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
  144. [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
  145. },
  146. bq27010_regs[BQ27XXX_REG_MAX] = {
  147. [BQ27XXX_REG_CTRL] = 0x00,
  148. [BQ27XXX_REG_TEMP] = 0x06,
  149. [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
  150. [BQ27XXX_REG_VOLT] = 0x08,
  151. [BQ27XXX_REG_AI] = 0x14,
  152. [BQ27XXX_REG_FLAGS] = 0x0a,
  153. [BQ27XXX_REG_TTE] = 0x16,
  154. [BQ27XXX_REG_TTF] = 0x18,
  155. [BQ27XXX_REG_TTES] = 0x1c,
  156. [BQ27XXX_REG_TTECP] = 0x26,
  157. [BQ27XXX_REG_NAC] = 0x0c,
  158. [BQ27XXX_REG_FCC] = 0x12,
  159. [BQ27XXX_REG_CYCT] = 0x2a,
  160. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  161. [BQ27XXX_REG_SOC] = 0x0b,
  162. [BQ27XXX_REG_DCAP] = 0x76,
  163. [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
  164. [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
  165. [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
  166. [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
  167. [BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
  168. [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
  169. },
  170. bq2750x_regs[BQ27XXX_REG_MAX] = {
  171. [BQ27XXX_REG_CTRL] = 0x00,
  172. [BQ27XXX_REG_TEMP] = 0x06,
  173. [BQ27XXX_REG_INT_TEMP] = 0x28,
  174. [BQ27XXX_REG_VOLT] = 0x08,
  175. [BQ27XXX_REG_AI] = 0x14,
  176. [BQ27XXX_REG_FLAGS] = 0x0a,
  177. [BQ27XXX_REG_TTE] = 0x16,
  178. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  179. [BQ27XXX_REG_TTES] = 0x1a,
  180. [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
  181. [BQ27XXX_REG_NAC] = 0x0c,
  182. [BQ27XXX_REG_FCC] = 0x12,
  183. [BQ27XXX_REG_CYCT] = 0x2a,
  184. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  185. [BQ27XXX_REG_SOC] = 0x2c,
  186. [BQ27XXX_REG_DCAP] = 0x3c,
  187. [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
  188. BQ27XXX_DM_REG_ROWS,
  189. },
  190. #define bq2751x_regs bq27510g3_regs
  191. #define bq2752x_regs bq27510g3_regs
  192. bq27500_regs[BQ27XXX_REG_MAX] = {
  193. [BQ27XXX_REG_CTRL] = 0x00,
  194. [BQ27XXX_REG_TEMP] = 0x06,
  195. [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
  196. [BQ27XXX_REG_VOLT] = 0x08,
  197. [BQ27XXX_REG_AI] = 0x14,
  198. [BQ27XXX_REG_FLAGS] = 0x0a,
  199. [BQ27XXX_REG_TTE] = 0x16,
  200. [BQ27XXX_REG_TTF] = 0x18,
  201. [BQ27XXX_REG_TTES] = 0x1c,
  202. [BQ27XXX_REG_TTECP] = 0x26,
  203. [BQ27XXX_REG_NAC] = 0x0c,
  204. [BQ27XXX_REG_FCC] = 0x12,
  205. [BQ27XXX_REG_CYCT] = 0x2a,
  206. [BQ27XXX_REG_AE] = 0x22,
  207. [BQ27XXX_REG_SOC] = 0x2c,
  208. [BQ27XXX_REG_DCAP] = 0x3c,
  209. [BQ27XXX_REG_AP] = 0x24,
  210. BQ27XXX_DM_REG_ROWS,
  211. },
  212. #define bq27510g1_regs bq27500_regs
  213. #define bq27510g2_regs bq27500_regs
  214. bq27510g3_regs[BQ27XXX_REG_MAX] = {
  215. [BQ27XXX_REG_CTRL] = 0x00,
  216. [BQ27XXX_REG_TEMP] = 0x06,
  217. [BQ27XXX_REG_INT_TEMP] = 0x28,
  218. [BQ27XXX_REG_VOLT] = 0x08,
  219. [BQ27XXX_REG_AI] = 0x14,
  220. [BQ27XXX_REG_FLAGS] = 0x0a,
  221. [BQ27XXX_REG_TTE] = 0x16,
  222. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  223. [BQ27XXX_REG_TTES] = 0x1a,
  224. [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
  225. [BQ27XXX_REG_NAC] = 0x0c,
  226. [BQ27XXX_REG_FCC] = 0x12,
  227. [BQ27XXX_REG_CYCT] = 0x1e,
  228. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  229. [BQ27XXX_REG_SOC] = 0x20,
  230. [BQ27XXX_REG_DCAP] = 0x2e,
  231. [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
  232. BQ27XXX_DM_REG_ROWS,
  233. },
  234. bq27520g1_regs[BQ27XXX_REG_MAX] = {
  235. [BQ27XXX_REG_CTRL] = 0x00,
  236. [BQ27XXX_REG_TEMP] = 0x06,
  237. [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
  238. [BQ27XXX_REG_VOLT] = 0x08,
  239. [BQ27XXX_REG_AI] = 0x14,
  240. [BQ27XXX_REG_FLAGS] = 0x0a,
  241. [BQ27XXX_REG_TTE] = 0x16,
  242. [BQ27XXX_REG_TTF] = 0x18,
  243. [BQ27XXX_REG_TTES] = 0x1c,
  244. [BQ27XXX_REG_TTECP] = 0x26,
  245. [BQ27XXX_REG_NAC] = 0x0c,
  246. [BQ27XXX_REG_FCC] = 0x12,
  247. [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
  248. [BQ27XXX_REG_AE] = 0x22,
  249. [BQ27XXX_REG_SOC] = 0x2c,
  250. [BQ27XXX_REG_DCAP] = 0x3c,
  251. [BQ27XXX_REG_AP] = 0x24,
  252. BQ27XXX_DM_REG_ROWS,
  253. },
  254. bq27520g2_regs[BQ27XXX_REG_MAX] = {
  255. [BQ27XXX_REG_CTRL] = 0x00,
  256. [BQ27XXX_REG_TEMP] = 0x06,
  257. [BQ27XXX_REG_INT_TEMP] = 0x36,
  258. [BQ27XXX_REG_VOLT] = 0x08,
  259. [BQ27XXX_REG_AI] = 0x14,
  260. [BQ27XXX_REG_FLAGS] = 0x0a,
  261. [BQ27XXX_REG_TTE] = 0x16,
  262. [BQ27XXX_REG_TTF] = 0x18,
  263. [BQ27XXX_REG_TTES] = 0x1c,
  264. [BQ27XXX_REG_TTECP] = 0x26,
  265. [BQ27XXX_REG_NAC] = 0x0c,
  266. [BQ27XXX_REG_FCC] = 0x12,
  267. [BQ27XXX_REG_CYCT] = 0x2a,
  268. [BQ27XXX_REG_AE] = 0x22,
  269. [BQ27XXX_REG_SOC] = 0x2c,
  270. [BQ27XXX_REG_DCAP] = 0x3c,
  271. [BQ27XXX_REG_AP] = 0x24,
  272. BQ27XXX_DM_REG_ROWS,
  273. },
  274. bq27520g3_regs[BQ27XXX_REG_MAX] = {
  275. [BQ27XXX_REG_CTRL] = 0x00,
  276. [BQ27XXX_REG_TEMP] = 0x06,
  277. [BQ27XXX_REG_INT_TEMP] = 0x36,
  278. [BQ27XXX_REG_VOLT] = 0x08,
  279. [BQ27XXX_REG_AI] = 0x14,
  280. [BQ27XXX_REG_FLAGS] = 0x0a,
  281. [BQ27XXX_REG_TTE] = 0x16,
  282. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  283. [BQ27XXX_REG_TTES] = 0x1c,
  284. [BQ27XXX_REG_TTECP] = 0x26,
  285. [BQ27XXX_REG_NAC] = 0x0c,
  286. [BQ27XXX_REG_FCC] = 0x12,
  287. [BQ27XXX_REG_CYCT] = 0x2a,
  288. [BQ27XXX_REG_AE] = 0x22,
  289. [BQ27XXX_REG_SOC] = 0x2c,
  290. [BQ27XXX_REG_DCAP] = 0x3c,
  291. [BQ27XXX_REG_AP] = 0x24,
  292. BQ27XXX_DM_REG_ROWS,
  293. },
  294. bq27520g4_regs[BQ27XXX_REG_MAX] = {
  295. [BQ27XXX_REG_CTRL] = 0x00,
  296. [BQ27XXX_REG_TEMP] = 0x06,
  297. [BQ27XXX_REG_INT_TEMP] = 0x28,
  298. [BQ27XXX_REG_VOLT] = 0x08,
  299. [BQ27XXX_REG_AI] = 0x14,
  300. [BQ27XXX_REG_FLAGS] = 0x0a,
  301. [BQ27XXX_REG_TTE] = 0x16,
  302. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  303. [BQ27XXX_REG_TTES] = 0x1c,
  304. [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
  305. [BQ27XXX_REG_NAC] = 0x0c,
  306. [BQ27XXX_REG_FCC] = 0x12,
  307. [BQ27XXX_REG_CYCT] = 0x1e,
  308. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  309. [BQ27XXX_REG_SOC] = 0x20,
  310. [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
  311. [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
  312. BQ27XXX_DM_REG_ROWS,
  313. },
  314. bq27530_regs[BQ27XXX_REG_MAX] = {
  315. [BQ27XXX_REG_CTRL] = 0x00,
  316. [BQ27XXX_REG_TEMP] = 0x06,
  317. [BQ27XXX_REG_INT_TEMP] = 0x32,
  318. [BQ27XXX_REG_VOLT] = 0x08,
  319. [BQ27XXX_REG_AI] = 0x14,
  320. [BQ27XXX_REG_FLAGS] = 0x0a,
  321. [BQ27XXX_REG_TTE] = 0x16,
  322. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  323. [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
  324. [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
  325. [BQ27XXX_REG_NAC] = 0x0c,
  326. [BQ27XXX_REG_FCC] = 0x12,
  327. [BQ27XXX_REG_CYCT] = 0x2a,
  328. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  329. [BQ27XXX_REG_SOC] = 0x2c,
  330. [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
  331. [BQ27XXX_REG_AP] = 0x24,
  332. BQ27XXX_DM_REG_ROWS,
  333. },
  334. #define bq27531_regs bq27530_regs
  335. bq27541_regs[BQ27XXX_REG_MAX] = {
  336. [BQ27XXX_REG_CTRL] = 0x00,
  337. [BQ27XXX_REG_TEMP] = 0x06,
  338. [BQ27XXX_REG_INT_TEMP] = 0x28,
  339. [BQ27XXX_REG_VOLT] = 0x08,
  340. [BQ27XXX_REG_AI] = 0x14,
  341. [BQ27XXX_REG_FLAGS] = 0x0a,
  342. [BQ27XXX_REG_TTE] = 0x16,
  343. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  344. [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
  345. [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
  346. [BQ27XXX_REG_NAC] = 0x0c,
  347. [BQ27XXX_REG_FCC] = 0x12,
  348. [BQ27XXX_REG_CYCT] = 0x2a,
  349. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  350. [BQ27XXX_REG_SOC] = 0x2c,
  351. [BQ27XXX_REG_DCAP] = 0x3c,
  352. [BQ27XXX_REG_AP] = 0x24,
  353. BQ27XXX_DM_REG_ROWS,
  354. },
  355. #define bq27542_regs bq27541_regs
  356. #define bq27546_regs bq27541_regs
  357. #define bq27742_regs bq27541_regs
  358. bq27545_regs[BQ27XXX_REG_MAX] = {
  359. [BQ27XXX_REG_CTRL] = 0x00,
  360. [BQ27XXX_REG_TEMP] = 0x06,
  361. [BQ27XXX_REG_INT_TEMP] = 0x28,
  362. [BQ27XXX_REG_VOLT] = 0x08,
  363. [BQ27XXX_REG_AI] = 0x14,
  364. [BQ27XXX_REG_FLAGS] = 0x0a,
  365. [BQ27XXX_REG_TTE] = 0x16,
  366. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  367. [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
  368. [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
  369. [BQ27XXX_REG_NAC] = 0x0c,
  370. [BQ27XXX_REG_FCC] = 0x12,
  371. [BQ27XXX_REG_CYCT] = 0x2a,
  372. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  373. [BQ27XXX_REG_SOC] = 0x2c,
  374. [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
  375. [BQ27XXX_REG_AP] = 0x24,
  376. BQ27XXX_DM_REG_ROWS,
  377. },
  378. bq27421_regs[BQ27XXX_REG_MAX] = {
  379. [BQ27XXX_REG_CTRL] = 0x00,
  380. [BQ27XXX_REG_TEMP] = 0x02,
  381. [BQ27XXX_REG_INT_TEMP] = 0x1e,
  382. [BQ27XXX_REG_VOLT] = 0x04,
  383. [BQ27XXX_REG_AI] = 0x10,
  384. [BQ27XXX_REG_FLAGS] = 0x06,
  385. [BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
  386. [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
  387. [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
  388. [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
  389. [BQ27XXX_REG_NAC] = 0x08,
  390. [BQ27XXX_REG_FCC] = 0x0e,
  391. [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
  392. [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
  393. [BQ27XXX_REG_SOC] = 0x1c,
  394. [BQ27XXX_REG_DCAP] = 0x3c,
  395. [BQ27XXX_REG_AP] = 0x18,
  396. BQ27XXX_DM_REG_ROWS,
  397. };
  398. #define bq27425_regs bq27421_regs
  399. #define bq27441_regs bq27421_regs
  400. #define bq27621_regs bq27421_regs
  401. static enum power_supply_property bq27000_props[] = {
  402. POWER_SUPPLY_PROP_STATUS,
  403. POWER_SUPPLY_PROP_PRESENT,
  404. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  405. POWER_SUPPLY_PROP_CURRENT_NOW,
  406. POWER_SUPPLY_PROP_CAPACITY,
  407. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  408. POWER_SUPPLY_PROP_TEMP,
  409. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  410. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  411. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  412. POWER_SUPPLY_PROP_TECHNOLOGY,
  413. POWER_SUPPLY_PROP_CHARGE_FULL,
  414. POWER_SUPPLY_PROP_CHARGE_NOW,
  415. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  416. POWER_SUPPLY_PROP_CYCLE_COUNT,
  417. POWER_SUPPLY_PROP_ENERGY_NOW,
  418. POWER_SUPPLY_PROP_POWER_AVG,
  419. POWER_SUPPLY_PROP_HEALTH,
  420. POWER_SUPPLY_PROP_MANUFACTURER,
  421. };
  422. static enum power_supply_property bq27010_props[] = {
  423. POWER_SUPPLY_PROP_STATUS,
  424. POWER_SUPPLY_PROP_PRESENT,
  425. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  426. POWER_SUPPLY_PROP_CURRENT_NOW,
  427. POWER_SUPPLY_PROP_CAPACITY,
  428. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  429. POWER_SUPPLY_PROP_TEMP,
  430. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  431. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  432. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  433. POWER_SUPPLY_PROP_TECHNOLOGY,
  434. POWER_SUPPLY_PROP_CHARGE_FULL,
  435. POWER_SUPPLY_PROP_CHARGE_NOW,
  436. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  437. POWER_SUPPLY_PROP_CYCLE_COUNT,
  438. POWER_SUPPLY_PROP_HEALTH,
  439. POWER_SUPPLY_PROP_MANUFACTURER,
  440. };
  441. #define bq2750x_props bq27510g3_props
  442. #define bq2751x_props bq27510g3_props
  443. #define bq2752x_props bq27510g3_props
  444. static enum power_supply_property bq27500_props[] = {
  445. POWER_SUPPLY_PROP_STATUS,
  446. POWER_SUPPLY_PROP_PRESENT,
  447. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  448. POWER_SUPPLY_PROP_CURRENT_NOW,
  449. POWER_SUPPLY_PROP_CAPACITY,
  450. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  451. POWER_SUPPLY_PROP_TEMP,
  452. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  453. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  454. POWER_SUPPLY_PROP_TECHNOLOGY,
  455. POWER_SUPPLY_PROP_CHARGE_FULL,
  456. POWER_SUPPLY_PROP_CHARGE_NOW,
  457. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  458. POWER_SUPPLY_PROP_CYCLE_COUNT,
  459. POWER_SUPPLY_PROP_ENERGY_NOW,
  460. POWER_SUPPLY_PROP_POWER_AVG,
  461. POWER_SUPPLY_PROP_HEALTH,
  462. POWER_SUPPLY_PROP_MANUFACTURER,
  463. };
  464. #define bq27510g1_props bq27500_props
  465. #define bq27510g2_props bq27500_props
  466. static enum power_supply_property bq27510g3_props[] = {
  467. POWER_SUPPLY_PROP_STATUS,
  468. POWER_SUPPLY_PROP_PRESENT,
  469. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  470. POWER_SUPPLY_PROP_CURRENT_NOW,
  471. POWER_SUPPLY_PROP_CAPACITY,
  472. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  473. POWER_SUPPLY_PROP_TEMP,
  474. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  475. POWER_SUPPLY_PROP_TECHNOLOGY,
  476. POWER_SUPPLY_PROP_CHARGE_FULL,
  477. POWER_SUPPLY_PROP_CHARGE_NOW,
  478. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  479. POWER_SUPPLY_PROP_CYCLE_COUNT,
  480. POWER_SUPPLY_PROP_HEALTH,
  481. POWER_SUPPLY_PROP_MANUFACTURER,
  482. };
  483. static enum power_supply_property bq27520g1_props[] = {
  484. POWER_SUPPLY_PROP_STATUS,
  485. POWER_SUPPLY_PROP_PRESENT,
  486. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  487. POWER_SUPPLY_PROP_CURRENT_NOW,
  488. POWER_SUPPLY_PROP_CAPACITY,
  489. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  490. POWER_SUPPLY_PROP_TEMP,
  491. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  492. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  493. POWER_SUPPLY_PROP_TECHNOLOGY,
  494. POWER_SUPPLY_PROP_CHARGE_FULL,
  495. POWER_SUPPLY_PROP_CHARGE_NOW,
  496. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  497. POWER_SUPPLY_PROP_ENERGY_NOW,
  498. POWER_SUPPLY_PROP_POWER_AVG,
  499. POWER_SUPPLY_PROP_HEALTH,
  500. POWER_SUPPLY_PROP_MANUFACTURER,
  501. };
  502. #define bq27520g2_props bq27500_props
  503. static enum power_supply_property bq27520g3_props[] = {
  504. POWER_SUPPLY_PROP_STATUS,
  505. POWER_SUPPLY_PROP_PRESENT,
  506. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  507. POWER_SUPPLY_PROP_CURRENT_NOW,
  508. POWER_SUPPLY_PROP_CAPACITY,
  509. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  510. POWER_SUPPLY_PROP_TEMP,
  511. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  512. POWER_SUPPLY_PROP_TECHNOLOGY,
  513. POWER_SUPPLY_PROP_CHARGE_FULL,
  514. POWER_SUPPLY_PROP_CHARGE_NOW,
  515. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  516. POWER_SUPPLY_PROP_CYCLE_COUNT,
  517. POWER_SUPPLY_PROP_ENERGY_NOW,
  518. POWER_SUPPLY_PROP_POWER_AVG,
  519. POWER_SUPPLY_PROP_HEALTH,
  520. POWER_SUPPLY_PROP_MANUFACTURER,
  521. };
  522. static enum power_supply_property bq27520g4_props[] = {
  523. POWER_SUPPLY_PROP_STATUS,
  524. POWER_SUPPLY_PROP_PRESENT,
  525. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  526. POWER_SUPPLY_PROP_CURRENT_NOW,
  527. POWER_SUPPLY_PROP_CAPACITY,
  528. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  529. POWER_SUPPLY_PROP_TEMP,
  530. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  531. POWER_SUPPLY_PROP_TECHNOLOGY,
  532. POWER_SUPPLY_PROP_CHARGE_FULL,
  533. POWER_SUPPLY_PROP_CHARGE_NOW,
  534. POWER_SUPPLY_PROP_CYCLE_COUNT,
  535. POWER_SUPPLY_PROP_HEALTH,
  536. POWER_SUPPLY_PROP_MANUFACTURER,
  537. };
  538. static enum power_supply_property bq27530_props[] = {
  539. POWER_SUPPLY_PROP_STATUS,
  540. POWER_SUPPLY_PROP_PRESENT,
  541. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  542. POWER_SUPPLY_PROP_CURRENT_NOW,
  543. POWER_SUPPLY_PROP_CAPACITY,
  544. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  545. POWER_SUPPLY_PROP_TEMP,
  546. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  547. POWER_SUPPLY_PROP_TECHNOLOGY,
  548. POWER_SUPPLY_PROP_CHARGE_FULL,
  549. POWER_SUPPLY_PROP_CHARGE_NOW,
  550. POWER_SUPPLY_PROP_POWER_AVG,
  551. POWER_SUPPLY_PROP_HEALTH,
  552. POWER_SUPPLY_PROP_CYCLE_COUNT,
  553. POWER_SUPPLY_PROP_MANUFACTURER,
  554. };
  555. #define bq27531_props bq27530_props
  556. static enum power_supply_property bq27541_props[] = {
  557. POWER_SUPPLY_PROP_STATUS,
  558. POWER_SUPPLY_PROP_PRESENT,
  559. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  560. POWER_SUPPLY_PROP_CURRENT_NOW,
  561. POWER_SUPPLY_PROP_CAPACITY,
  562. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  563. POWER_SUPPLY_PROP_TEMP,
  564. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  565. POWER_SUPPLY_PROP_TECHNOLOGY,
  566. POWER_SUPPLY_PROP_CHARGE_FULL,
  567. POWER_SUPPLY_PROP_CHARGE_NOW,
  568. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  569. POWER_SUPPLY_PROP_CYCLE_COUNT,
  570. POWER_SUPPLY_PROP_POWER_AVG,
  571. POWER_SUPPLY_PROP_HEALTH,
  572. POWER_SUPPLY_PROP_MANUFACTURER,
  573. };
  574. #define bq27542_props bq27541_props
  575. #define bq27546_props bq27541_props
  576. #define bq27742_props bq27541_props
  577. static enum power_supply_property bq27545_props[] = {
  578. POWER_SUPPLY_PROP_STATUS,
  579. POWER_SUPPLY_PROP_PRESENT,
  580. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  581. POWER_SUPPLY_PROP_CURRENT_NOW,
  582. POWER_SUPPLY_PROP_CAPACITY,
  583. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  584. POWER_SUPPLY_PROP_TEMP,
  585. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  586. POWER_SUPPLY_PROP_TECHNOLOGY,
  587. POWER_SUPPLY_PROP_CHARGE_FULL,
  588. POWER_SUPPLY_PROP_CHARGE_NOW,
  589. POWER_SUPPLY_PROP_HEALTH,
  590. POWER_SUPPLY_PROP_CYCLE_COUNT,
  591. POWER_SUPPLY_PROP_POWER_AVG,
  592. POWER_SUPPLY_PROP_MANUFACTURER,
  593. };
  594. static enum power_supply_property bq27421_props[] = {
  595. POWER_SUPPLY_PROP_STATUS,
  596. POWER_SUPPLY_PROP_PRESENT,
  597. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  598. POWER_SUPPLY_PROP_CURRENT_NOW,
  599. POWER_SUPPLY_PROP_CAPACITY,
  600. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  601. POWER_SUPPLY_PROP_TEMP,
  602. POWER_SUPPLY_PROP_TECHNOLOGY,
  603. POWER_SUPPLY_PROP_CHARGE_FULL,
  604. POWER_SUPPLY_PROP_CHARGE_NOW,
  605. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  606. POWER_SUPPLY_PROP_MANUFACTURER,
  607. };
  608. #define bq27425_props bq27421_props
  609. #define bq27441_props bq27421_props
  610. #define bq27621_props bq27421_props
  611. struct bq27xxx_dm_reg {
  612. u8 subclass_id;
  613. u8 offset;
  614. u8 bytes;
  615. u16 min, max;
  616. };
  617. enum bq27xxx_dm_reg_id {
  618. BQ27XXX_DM_DESIGN_CAPACITY = 0,
  619. BQ27XXX_DM_DESIGN_ENERGY,
  620. BQ27XXX_DM_TERMINATE_VOLTAGE,
  621. };
  622. #define bq27000_dm_regs 0
  623. #define bq27010_dm_regs 0
  624. #define bq2750x_dm_regs 0
  625. #define bq2751x_dm_regs 0
  626. #define bq2752x_dm_regs 0
  627. #if 0 /* not yet tested */
  628. static struct bq27xxx_dm_reg bq27500_dm_regs[] = {
  629. [BQ27XXX_DM_DESIGN_CAPACITY] = { 48, 10, 2, 0, 65535 },
  630. [BQ27XXX_DM_DESIGN_ENERGY] = { }, /* missing on chip */
  631. [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 48, 2, 1000, 32767 },
  632. };
  633. #else
  634. #define bq27500_dm_regs 0
  635. #endif
  636. /* todo create data memory definitions from datasheets and test on chips */
  637. #define bq27510g1_dm_regs 0
  638. #define bq27510g2_dm_regs 0
  639. #define bq27510g3_dm_regs 0
  640. #define bq27520g1_dm_regs 0
  641. #define bq27520g2_dm_regs 0
  642. #define bq27520g3_dm_regs 0
  643. #define bq27520g4_dm_regs 0
  644. #define bq27530_dm_regs 0
  645. #define bq27531_dm_regs 0
  646. #define bq27541_dm_regs 0
  647. #define bq27542_dm_regs 0
  648. #define bq27546_dm_regs 0
  649. #define bq27742_dm_regs 0
  650. #if 0 /* not yet tested */
  651. static struct bq27xxx_dm_reg bq27545_dm_regs[] = {
  652. [BQ27XXX_DM_DESIGN_CAPACITY] = { 48, 23, 2, 0, 32767 },
  653. [BQ27XXX_DM_DESIGN_ENERGY] = { 48, 25, 2, 0, 32767 },
  654. [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 67, 2, 2800, 3700 },
  655. };
  656. #else
  657. #define bq27545_dm_regs 0
  658. #endif
  659. static struct bq27xxx_dm_reg bq27421_dm_regs[] = {
  660. [BQ27XXX_DM_DESIGN_CAPACITY] = { 82, 10, 2, 0, 8000 },
  661. [BQ27XXX_DM_DESIGN_ENERGY] = { 82, 12, 2, 0, 32767 },
  662. [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 16, 2, 2500, 3700 },
  663. };
  664. static struct bq27xxx_dm_reg bq27425_dm_regs[] = {
  665. [BQ27XXX_DM_DESIGN_CAPACITY] = { 82, 12, 2, 0, 32767 },
  666. [BQ27XXX_DM_DESIGN_ENERGY] = { 82, 14, 2, 0, 32767 },
  667. [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 18, 2, 2800, 3700 },
  668. };
  669. #if 0 /* not yet tested */
  670. #define bq27441_dm_regs bq27421_dm_regs
  671. #else
  672. #define bq27441_dm_regs 0
  673. #endif
  674. #if 0 /* not yet tested */
  675. static struct bq27xxx_dm_reg bq27621_dm_regs[] = {
  676. [BQ27XXX_DM_DESIGN_CAPACITY] = { 82, 3, 2, 0, 8000 },
  677. [BQ27XXX_DM_DESIGN_ENERGY] = { 82, 5, 2, 0, 32767 },
  678. [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 9, 2, 2500, 3700 },
  679. };
  680. #else
  681. #define bq27621_dm_regs 0
  682. #endif
  683. #define BQ27XXX_O_ZERO 0x00000001
  684. #define BQ27XXX_O_OTDC 0x00000002
  685. #define BQ27XXX_O_UTOT 0x00000004
  686. #define BQ27XXX_O_CFGUP 0x00000008
  687. #define BQ27XXX_O_RAM 0x00000010
  688. #define BQ27XXX_DATA(ref, key, opt) { \
  689. .opts = (opt), \
  690. .unseal_key = key, \
  691. .regs = ref##_regs, \
  692. .dm_regs = ref##_dm_regs, \
  693. .props = ref##_props, \
  694. .props_size = ARRAY_SIZE(ref##_props) }
  695. static struct {
  696. u32 opts;
  697. u32 unseal_key;
  698. u8 *regs;
  699. struct bq27xxx_dm_reg *dm_regs;
  700. enum power_supply_property *props;
  701. size_t props_size;
  702. } bq27xxx_chip_data[] = {
  703. [BQ27000] = BQ27XXX_DATA(bq27000, 0 , BQ27XXX_O_ZERO),
  704. [BQ27010] = BQ27XXX_DATA(bq27010, 0 , BQ27XXX_O_ZERO),
  705. [BQ2750X] = BQ27XXX_DATA(bq2750x, 0 , BQ27XXX_O_OTDC),
  706. [BQ2751X] = BQ27XXX_DATA(bq2751x, 0 , BQ27XXX_O_OTDC),
  707. [BQ2752X] = BQ27XXX_DATA(bq2752x, 0 , BQ27XXX_O_OTDC),
  708. [BQ27500] = BQ27XXX_DATA(bq27500, 0x04143672, BQ27XXX_O_OTDC),
  709. [BQ27510G1] = BQ27XXX_DATA(bq27510g1, 0 , BQ27XXX_O_OTDC),
  710. [BQ27510G2] = BQ27XXX_DATA(bq27510g2, 0 , BQ27XXX_O_OTDC),
  711. [BQ27510G3] = BQ27XXX_DATA(bq27510g3, 0 , BQ27XXX_O_OTDC),
  712. [BQ27520G1] = BQ27XXX_DATA(bq27520g1, 0 , BQ27XXX_O_OTDC),
  713. [BQ27520G2] = BQ27XXX_DATA(bq27520g2, 0 , BQ27XXX_O_OTDC),
  714. [BQ27520G3] = BQ27XXX_DATA(bq27520g3, 0 , BQ27XXX_O_OTDC),
  715. [BQ27520G4] = BQ27XXX_DATA(bq27520g4, 0 , BQ27XXX_O_OTDC),
  716. [BQ27530] = BQ27XXX_DATA(bq27530, 0 , BQ27XXX_O_UTOT),
  717. [BQ27531] = BQ27XXX_DATA(bq27531, 0 , BQ27XXX_O_UTOT),
  718. [BQ27541] = BQ27XXX_DATA(bq27541, 0 , BQ27XXX_O_OTDC),
  719. [BQ27542] = BQ27XXX_DATA(bq27542, 0 , BQ27XXX_O_OTDC),
  720. [BQ27546] = BQ27XXX_DATA(bq27546, 0 , BQ27XXX_O_OTDC),
  721. [BQ27742] = BQ27XXX_DATA(bq27742, 0 , BQ27XXX_O_OTDC),
  722. [BQ27545] = BQ27XXX_DATA(bq27545, 0x04143672, BQ27XXX_O_OTDC),
  723. [BQ27421] = BQ27XXX_DATA(bq27421, 0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
  724. [BQ27425] = BQ27XXX_DATA(bq27425, 0x04143672, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP),
  725. [BQ27441] = BQ27XXX_DATA(bq27441, 0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
  726. [BQ27621] = BQ27XXX_DATA(bq27621, 0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
  727. };
  728. static DEFINE_MUTEX(bq27xxx_list_lock);
  729. static LIST_HEAD(bq27xxx_battery_devices);
  730. #define BQ27XXX_MSLEEP(i) usleep_range((i)*1000, (i)*1000+500)
  731. #define BQ27XXX_DM_SZ 32
  732. /**
  733. * struct bq27xxx_dm_buf - chip data memory buffer
  734. * @class: data memory subclass_id
  735. * @block: data memory block number
  736. * @data: data from/for the block
  737. * @has_data: true if data has been filled by read
  738. * @dirty: true if data has changed since last read/write
  739. *
  740. * Encapsulates info required to manage chip data memory blocks.
  741. */
  742. struct bq27xxx_dm_buf {
  743. u8 class;
  744. u8 block;
  745. u8 data[BQ27XXX_DM_SZ];
  746. bool has_data, dirty;
  747. };
  748. #define BQ27XXX_DM_BUF(di, i) { \
  749. .class = (di)->dm_regs[i].subclass_id, \
  750. .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
  751. }
  752. static inline u16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf,
  753. struct bq27xxx_dm_reg *reg)
  754. {
  755. if (buf->class == reg->subclass_id &&
  756. buf->block == reg->offset / BQ27XXX_DM_SZ)
  757. return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ);
  758. return NULL;
  759. }
  760. static const char * const bq27xxx_dm_reg_name[] = {
  761. [BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
  762. [BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
  763. [BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
  764. };
  765. static bool bq27xxx_dt_to_nvm = true;
  766. module_param_named(dt_monitored_battery_updates_nvm, bq27xxx_dt_to_nvm, bool, 0444);
  767. MODULE_PARM_DESC(dt_monitored_battery_updates_nvm,
  768. "Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
  769. "Users must set this =0 when installing a different type of battery!\n"
  770. "Default is =1."
  771. #ifndef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
  772. "\nSetting this affects future kernel updates, not the current configuration."
  773. #endif
  774. );
  775. static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
  776. {
  777. struct bq27xxx_device_info *di;
  778. unsigned int prev_val = *(unsigned int *) kp->arg;
  779. int ret;
  780. ret = param_set_uint(val, kp);
  781. if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
  782. return ret;
  783. mutex_lock(&bq27xxx_list_lock);
  784. list_for_each_entry(di, &bq27xxx_battery_devices, list) {
  785. cancel_delayed_work_sync(&di->work);
  786. schedule_delayed_work(&di->work, 0);
  787. }
  788. mutex_unlock(&bq27xxx_list_lock);
  789. return ret;
  790. }
  791. static const struct kernel_param_ops param_ops_poll_interval = {
  792. .get = param_get_uint,
  793. .set = poll_interval_param_set,
  794. };
  795. static unsigned int poll_interval = 360;
  796. module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
  797. MODULE_PARM_DESC(poll_interval,
  798. "battery poll interval in seconds - 0 disables polling");
  799. /*
  800. * Common code for BQ27xxx devices
  801. */
  802. static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
  803. bool single)
  804. {
  805. int ret;
  806. if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
  807. return -EINVAL;
  808. ret = di->bus.read(di, di->regs[reg_index], single);
  809. if (ret < 0)
  810. dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n",
  811. di->regs[reg_index], reg_index);
  812. return ret;
  813. }
  814. static inline int bq27xxx_write(struct bq27xxx_device_info *di, int reg_index,
  815. u16 value, bool single)
  816. {
  817. int ret;
  818. if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
  819. return -EINVAL;
  820. if (!di->bus.write)
  821. return -EPERM;
  822. ret = di->bus.write(di, di->regs[reg_index], value, single);
  823. if (ret < 0)
  824. dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n",
  825. di->regs[reg_index], reg_index);
  826. return ret;
  827. }
  828. static inline int bq27xxx_read_block(struct bq27xxx_device_info *di, int reg_index,
  829. u8 *data, int len)
  830. {
  831. int ret;
  832. if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
  833. return -EINVAL;
  834. if (!di->bus.read_bulk)
  835. return -EPERM;
  836. ret = di->bus.read_bulk(di, di->regs[reg_index], data, len);
  837. if (ret < 0)
  838. dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n",
  839. di->regs[reg_index], reg_index);
  840. return ret;
  841. }
  842. static inline int bq27xxx_write_block(struct bq27xxx_device_info *di, int reg_index,
  843. u8 *data, int len)
  844. {
  845. int ret;
  846. if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
  847. return -EINVAL;
  848. if (!di->bus.write_bulk)
  849. return -EPERM;
  850. ret = di->bus.write_bulk(di, di->regs[reg_index], data, len);
  851. if (ret < 0)
  852. dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n",
  853. di->regs[reg_index], reg_index);
  854. return ret;
  855. }
  856. static int bq27xxx_battery_seal(struct bq27xxx_device_info *di)
  857. {
  858. int ret;
  859. ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_SEALED, false);
  860. if (ret < 0) {
  861. dev_err(di->dev, "bus error on seal: %d\n", ret);
  862. return ret;
  863. }
  864. return 0;
  865. }
  866. static int bq27xxx_battery_unseal(struct bq27xxx_device_info *di)
  867. {
  868. int ret;
  869. if (di->unseal_key == 0) {
  870. dev_err(di->dev, "unseal failed due to missing key\n");
  871. return -EINVAL;
  872. }
  873. ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false);
  874. if (ret < 0)
  875. goto out;
  876. ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false);
  877. if (ret < 0)
  878. goto out;
  879. return 0;
  880. out:
  881. dev_err(di->dev, "bus error on unseal: %d\n", ret);
  882. return ret;
  883. }
  884. static u8 bq27xxx_battery_checksum_dm_block(struct bq27xxx_dm_buf *buf)
  885. {
  886. u16 sum = 0;
  887. int i;
  888. for (i = 0; i < BQ27XXX_DM_SZ; i++)
  889. sum += buf->data[i];
  890. sum &= 0xff;
  891. return 0xff - sum;
  892. }
  893. static int bq27xxx_battery_read_dm_block(struct bq27xxx_device_info *di,
  894. struct bq27xxx_dm_buf *buf)
  895. {
  896. int ret;
  897. buf->has_data = false;
  898. ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
  899. if (ret < 0)
  900. goto out;
  901. ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
  902. if (ret < 0)
  903. goto out;
  904. BQ27XXX_MSLEEP(1);
  905. ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
  906. if (ret < 0)
  907. goto out;
  908. ret = bq27xxx_read(di, BQ27XXX_DM_CKSUM, true);
  909. if (ret < 0)
  910. goto out;
  911. if ((u8)ret != bq27xxx_battery_checksum_dm_block(buf)) {
  912. ret = -EINVAL;
  913. goto out;
  914. }
  915. buf->has_data = true;
  916. buf->dirty = false;
  917. return 0;
  918. out:
  919. dev_err(di->dev, "bus error reading chip memory: %d\n", ret);
  920. return ret;
  921. }
  922. static void bq27xxx_battery_update_dm_block(struct bq27xxx_device_info *di,
  923. struct bq27xxx_dm_buf *buf,
  924. enum bq27xxx_dm_reg_id reg_id,
  925. unsigned int val)
  926. {
  927. struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
  928. const char *str = bq27xxx_dm_reg_name[reg_id];
  929. u16 *prev = bq27xxx_dm_reg_ptr(buf, reg);
  930. if (prev == NULL) {
  931. dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
  932. return;
  933. }
  934. if (reg->bytes != 2) {
  935. dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
  936. return;
  937. }
  938. if (!buf->has_data)
  939. return;
  940. if (be16_to_cpup(prev) == val) {
  941. dev_info(di->dev, "%s has %u\n", str, val);
  942. return;
  943. }
  944. #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
  945. if (!(di->opts & BQ27XXX_O_RAM) && !bq27xxx_dt_to_nvm) {
  946. #else
  947. if (!(di->opts & BQ27XXX_O_RAM)) {
  948. #endif
  949. /* devicetree and NVM differ; defer to NVM */
  950. dev_warn(di->dev, "%s has %u; update to %u disallowed "
  951. #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
  952. "by dt_monitored_battery_updates_nvm=0"
  953. #else
  954. "for flash/NVM data memory"
  955. #endif
  956. "\n", str, be16_to_cpup(prev), val);
  957. return;
  958. }
  959. dev_info(di->dev, "update %s to %u\n", str, val);
  960. *prev = cpu_to_be16(val);
  961. buf->dirty = true;
  962. }
  963. static int bq27xxx_battery_cfgupdate_priv(struct bq27xxx_device_info *di, bool active)
  964. {
  965. const int limit = 100;
  966. u16 cmd = active ? BQ27XXX_SET_CFGUPDATE : BQ27XXX_SOFT_RESET;
  967. int ret, try = limit;
  968. ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, cmd, false);
  969. if (ret < 0)
  970. return ret;
  971. do {
  972. BQ27XXX_MSLEEP(25);
  973. ret = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
  974. if (ret < 0)
  975. return ret;
  976. } while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try);
  977. if (!try && di->chip != BQ27425) { // 425 has a bug
  978. dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active);
  979. return -EINVAL;
  980. }
  981. if (limit - try > 3)
  982. dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try);
  983. return 0;
  984. }
  985. static inline int bq27xxx_battery_set_cfgupdate(struct bq27xxx_device_info *di)
  986. {
  987. int ret = bq27xxx_battery_cfgupdate_priv(di, true);
  988. if (ret < 0 && ret != -EINVAL)
  989. dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret);
  990. return ret;
  991. }
  992. static inline int bq27xxx_battery_soft_reset(struct bq27xxx_device_info *di)
  993. {
  994. int ret = bq27xxx_battery_cfgupdate_priv(di, false);
  995. if (ret < 0 && ret != -EINVAL)
  996. dev_err(di->dev, "bus error on soft_reset: %d\n", ret);
  997. return ret;
  998. }
  999. static int bq27xxx_battery_write_dm_block(struct bq27xxx_device_info *di,
  1000. struct bq27xxx_dm_buf *buf)
  1001. {
  1002. bool cfgup = di->opts & BQ27XXX_O_CFGUP;
  1003. int ret;
  1004. if (!buf->dirty)
  1005. return 0;
  1006. if (cfgup) {
  1007. ret = bq27xxx_battery_set_cfgupdate(di);
  1008. if (ret < 0)
  1009. return ret;
  1010. }
  1011. ret = bq27xxx_write(di, BQ27XXX_DM_CTRL, 0, true);
  1012. if (ret < 0)
  1013. goto out;
  1014. ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
  1015. if (ret < 0)
  1016. goto out;
  1017. ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
  1018. if (ret < 0)
  1019. goto out;
  1020. BQ27XXX_MSLEEP(1);
  1021. ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
  1022. if (ret < 0)
  1023. goto out;
  1024. ret = bq27xxx_write(di, BQ27XXX_DM_CKSUM,
  1025. bq27xxx_battery_checksum_dm_block(buf), true);
  1026. if (ret < 0)
  1027. goto out;
  1028. /* DO NOT read BQ27XXX_DM_CKSUM here to verify it! That may cause NVM
  1029. * corruption on the '425 chip (and perhaps others), which can damage
  1030. * the chip.
  1031. */
  1032. if (cfgup) {
  1033. BQ27XXX_MSLEEP(1);
  1034. ret = bq27xxx_battery_soft_reset(di);
  1035. if (ret < 0)
  1036. return ret;
  1037. } else {
  1038. BQ27XXX_MSLEEP(100); /* flash DM updates in <100ms */
  1039. }
  1040. buf->dirty = false;
  1041. return 0;
  1042. out:
  1043. if (cfgup)
  1044. bq27xxx_battery_soft_reset(di);
  1045. dev_err(di->dev, "bus error writing chip memory: %d\n", ret);
  1046. return ret;
  1047. }
  1048. static void bq27xxx_battery_set_config(struct bq27xxx_device_info *di,
  1049. struct power_supply_battery_info *info)
  1050. {
  1051. struct bq27xxx_dm_buf bd = BQ27XXX_DM_BUF(di, BQ27XXX_DM_DESIGN_CAPACITY);
  1052. struct bq27xxx_dm_buf bt = BQ27XXX_DM_BUF(di, BQ27XXX_DM_TERMINATE_VOLTAGE);
  1053. bool updated;
  1054. if (bq27xxx_battery_unseal(di) < 0)
  1055. return;
  1056. if (info->charge_full_design_uah != -EINVAL &&
  1057. info->energy_full_design_uwh != -EINVAL) {
  1058. bq27xxx_battery_read_dm_block(di, &bd);
  1059. /* assume design energy & capacity are in same block */
  1060. bq27xxx_battery_update_dm_block(di, &bd,
  1061. BQ27XXX_DM_DESIGN_CAPACITY,
  1062. info->charge_full_design_uah / 1000);
  1063. bq27xxx_battery_update_dm_block(di, &bd,
  1064. BQ27XXX_DM_DESIGN_ENERGY,
  1065. info->energy_full_design_uwh / 1000);
  1066. }
  1067. if (info->voltage_min_design_uv != -EINVAL) {
  1068. bool same = bd.class == bt.class && bd.block == bt.block;
  1069. if (!same)
  1070. bq27xxx_battery_read_dm_block(di, &bt);
  1071. bq27xxx_battery_update_dm_block(di, same ? &bd : &bt,
  1072. BQ27XXX_DM_TERMINATE_VOLTAGE,
  1073. info->voltage_min_design_uv / 1000);
  1074. }
  1075. updated = bd.dirty || bt.dirty;
  1076. bq27xxx_battery_write_dm_block(di, &bd);
  1077. bq27xxx_battery_write_dm_block(di, &bt);
  1078. bq27xxx_battery_seal(di);
  1079. if (updated && !(di->opts & BQ27XXX_O_CFGUP)) {
  1080. bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_RESET, false);
  1081. BQ27XXX_MSLEEP(300); /* reset time is not documented */
  1082. }
  1083. /* assume bq27xxx_battery_update() is called hereafter */
  1084. }
  1085. static void bq27xxx_battery_settings(struct bq27xxx_device_info *di)
  1086. {
  1087. struct power_supply_battery_info info = {};
  1088. unsigned int min, max;
  1089. if (power_supply_get_battery_info(di->bat, &info) < 0)
  1090. return;
  1091. if (!di->dm_regs) {
  1092. dev_warn(di->dev, "data memory update not supported for chip\n");
  1093. return;
  1094. }
  1095. if (info.energy_full_design_uwh != info.charge_full_design_uah) {
  1096. if (info.energy_full_design_uwh == -EINVAL)
  1097. dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
  1098. else if (info.charge_full_design_uah == -EINVAL)
  1099. dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
  1100. }
  1101. /* assume min == 0 */
  1102. max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
  1103. if (info.energy_full_design_uwh > max * 1000) {
  1104. dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
  1105. info.energy_full_design_uwh);
  1106. info.energy_full_design_uwh = -EINVAL;
  1107. }
  1108. /* assume min == 0 */
  1109. max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
  1110. if (info.charge_full_design_uah > max * 1000) {
  1111. dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
  1112. info.charge_full_design_uah);
  1113. info.charge_full_design_uah = -EINVAL;
  1114. }
  1115. min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
  1116. max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
  1117. if ((info.voltage_min_design_uv < min * 1000 ||
  1118. info.voltage_min_design_uv > max * 1000) &&
  1119. info.voltage_min_design_uv != -EINVAL) {
  1120. dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
  1121. info.voltage_min_design_uv);
  1122. info.voltage_min_design_uv = -EINVAL;
  1123. }
  1124. if ((info.energy_full_design_uwh != -EINVAL &&
  1125. info.charge_full_design_uah != -EINVAL) ||
  1126. info.voltage_min_design_uv != -EINVAL)
  1127. bq27xxx_battery_set_config(di, &info);
  1128. }
  1129. /*
  1130. * Return the battery State-of-Charge
  1131. * Or < 0 if something fails.
  1132. */
  1133. static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
  1134. {
  1135. int soc;
  1136. if (di->opts & BQ27XXX_O_ZERO)
  1137. soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
  1138. else
  1139. soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
  1140. if (soc < 0)
  1141. dev_dbg(di->dev, "error reading State-of-Charge\n");
  1142. return soc;
  1143. }
  1144. /*
  1145. * Return a battery charge value in µAh
  1146. * Or < 0 if something fails.
  1147. */
  1148. static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
  1149. {
  1150. int charge;
  1151. charge = bq27xxx_read(di, reg, false);
  1152. if (charge < 0) {
  1153. dev_dbg(di->dev, "error reading charge register %02x: %d\n",
  1154. reg, charge);
  1155. return charge;
  1156. }
  1157. if (di->opts & BQ27XXX_O_ZERO)
  1158. charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
  1159. else
  1160. charge *= 1000;
  1161. return charge;
  1162. }
  1163. /*
  1164. * Return the battery Nominal available capacity in µAh
  1165. * Or < 0 if something fails.
  1166. */
  1167. static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
  1168. {
  1169. int flags;
  1170. if (di->opts & BQ27XXX_O_ZERO) {
  1171. flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
  1172. if (flags >= 0 && (flags & BQ27000_FLAG_CI))
  1173. return -ENODATA;
  1174. }
  1175. return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
  1176. }
  1177. /*
  1178. * Return the battery Full Charge Capacity in µAh
  1179. * Or < 0 if something fails.
  1180. */
  1181. static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
  1182. {
  1183. return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
  1184. }
  1185. /*
  1186. * Return the Design Capacity in µAh
  1187. * Or < 0 if something fails.
  1188. */
  1189. static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
  1190. {
  1191. int dcap;
  1192. if (di->opts & BQ27XXX_O_ZERO)
  1193. dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
  1194. else
  1195. dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
  1196. if (dcap < 0) {
  1197. dev_dbg(di->dev, "error reading initial last measured discharge\n");
  1198. return dcap;
  1199. }
  1200. if (di->opts & BQ27XXX_O_ZERO)
  1201. dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
  1202. else
  1203. dcap *= 1000;
  1204. return dcap;
  1205. }
  1206. /*
  1207. * Return the battery Available energy in µWh
  1208. * Or < 0 if something fails.
  1209. */
  1210. static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
  1211. {
  1212. int ae;
  1213. ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
  1214. if (ae < 0) {
  1215. dev_dbg(di->dev, "error reading available energy\n");
  1216. return ae;
  1217. }
  1218. if (di->opts & BQ27XXX_O_ZERO)
  1219. ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
  1220. else
  1221. ae *= 1000;
  1222. return ae;
  1223. }
  1224. /*
  1225. * Return the battery temperature in tenths of degree Kelvin
  1226. * Or < 0 if something fails.
  1227. */
  1228. static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
  1229. {
  1230. int temp;
  1231. temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
  1232. if (temp < 0) {
  1233. dev_err(di->dev, "error reading temperature\n");
  1234. return temp;
  1235. }
  1236. if (di->opts & BQ27XXX_O_ZERO)
  1237. temp = 5 * temp / 2;
  1238. return temp;
  1239. }
  1240. /*
  1241. * Return the battery Cycle count total
  1242. * Or < 0 if something fails.
  1243. */
  1244. static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
  1245. {
  1246. int cyct;
  1247. cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
  1248. if (cyct < 0)
  1249. dev_err(di->dev, "error reading cycle count total\n");
  1250. return cyct;
  1251. }
  1252. /*
  1253. * Read a time register.
  1254. * Return < 0 if something fails.
  1255. */
  1256. static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
  1257. {
  1258. int tval;
  1259. tval = bq27xxx_read(di, reg, false);
  1260. if (tval < 0) {
  1261. dev_dbg(di->dev, "error reading time register %02x: %d\n",
  1262. reg, tval);
  1263. return tval;
  1264. }
  1265. if (tval == 65535)
  1266. return -ENODATA;
  1267. return tval * 60;
  1268. }
  1269. /*
  1270. * Returns true if a battery over temperature condition is detected
  1271. */
  1272. static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
  1273. {
  1274. if (di->opts & BQ27XXX_O_OTDC)
  1275. return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
  1276. if (di->opts & BQ27XXX_O_UTOT)
  1277. return flags & BQ27XXX_FLAG_OT;
  1278. return false;
  1279. }
  1280. /*
  1281. * Returns true if a battery under temperature condition is detected
  1282. */
  1283. static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
  1284. {
  1285. if (di->opts & BQ27XXX_O_UTOT)
  1286. return flags & BQ27XXX_FLAG_UT;
  1287. return false;
  1288. }
  1289. /*
  1290. * Returns true if a low state of charge condition is detected
  1291. */
  1292. static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
  1293. {
  1294. if (di->opts & BQ27XXX_O_ZERO)
  1295. return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
  1296. else
  1297. return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
  1298. }
  1299. /*
  1300. * Read flag register.
  1301. * Return < 0 if something fails.
  1302. */
  1303. static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
  1304. {
  1305. int flags;
  1306. bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
  1307. flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
  1308. if (flags < 0) {
  1309. dev_err(di->dev, "error reading flag register:%d\n", flags);
  1310. return flags;
  1311. }
  1312. /* Unlikely but important to return first */
  1313. if (unlikely(bq27xxx_battery_overtemp(di, flags)))
  1314. return POWER_SUPPLY_HEALTH_OVERHEAT;
  1315. if (unlikely(bq27xxx_battery_undertemp(di, flags)))
  1316. return POWER_SUPPLY_HEALTH_COLD;
  1317. if (unlikely(bq27xxx_battery_dead(di, flags)))
  1318. return POWER_SUPPLY_HEALTH_DEAD;
  1319. return POWER_SUPPLY_HEALTH_GOOD;
  1320. }
  1321. void bq27xxx_battery_update(struct bq27xxx_device_info *di)
  1322. {
  1323. struct bq27xxx_reg_cache cache = {0, };
  1324. bool has_ci_flag = di->opts & BQ27XXX_O_ZERO;
  1325. bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
  1326. cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
  1327. if ((cache.flags & 0xff) == 0xff)
  1328. cache.flags = -1; /* read error */
  1329. if (cache.flags >= 0) {
  1330. cache.temperature = bq27xxx_battery_read_temperature(di);
  1331. if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
  1332. dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
  1333. cache.capacity = -ENODATA;
  1334. cache.energy = -ENODATA;
  1335. cache.time_to_empty = -ENODATA;
  1336. cache.time_to_empty_avg = -ENODATA;
  1337. cache.time_to_full = -ENODATA;
  1338. cache.charge_full = -ENODATA;
  1339. cache.health = -ENODATA;
  1340. } else {
  1341. if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
  1342. cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
  1343. if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
  1344. cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
  1345. if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
  1346. cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
  1347. cache.charge_full = bq27xxx_battery_read_fcc(di);
  1348. cache.capacity = bq27xxx_battery_read_soc(di);
  1349. if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
  1350. cache.energy = bq27xxx_battery_read_energy(di);
  1351. cache.health = bq27xxx_battery_read_health(di);
  1352. }
  1353. if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
  1354. cache.cycle_count = bq27xxx_battery_read_cyct(di);
  1355. /* We only have to read charge design full once */
  1356. if (di->charge_design_full <= 0)
  1357. di->charge_design_full = bq27xxx_battery_read_dcap(di);
  1358. }
  1359. if (di->cache.capacity != cache.capacity)
  1360. power_supply_changed(di->bat);
  1361. if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
  1362. di->cache = cache;
  1363. di->last_update = jiffies;
  1364. }
  1365. EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
  1366. static void bq27xxx_battery_poll(struct work_struct *work)
  1367. {
  1368. struct bq27xxx_device_info *di =
  1369. container_of(work, struct bq27xxx_device_info,
  1370. work.work);
  1371. bq27xxx_battery_update(di);
  1372. if (poll_interval > 0)
  1373. schedule_delayed_work(&di->work, poll_interval * HZ);
  1374. }
  1375. /*
  1376. * Return the battery average current in µA
  1377. * Note that current can be negative signed as well
  1378. * Or 0 if something fails.
  1379. */
  1380. static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
  1381. union power_supply_propval *val)
  1382. {
  1383. int curr;
  1384. int flags;
  1385. curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
  1386. if (curr < 0) {
  1387. dev_err(di->dev, "error reading current\n");
  1388. return curr;
  1389. }
  1390. if (di->opts & BQ27XXX_O_ZERO) {
  1391. flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
  1392. if (flags & BQ27000_FLAG_CHGS) {
  1393. dev_dbg(di->dev, "negative current!\n");
  1394. curr = -curr;
  1395. }
  1396. val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
  1397. } else {
  1398. /* Other gauges return signed value */
  1399. val->intval = (int)((s16)curr) * 1000;
  1400. }
  1401. return 0;
  1402. }
  1403. /*
  1404. * Get the average power in µW
  1405. * Return < 0 if something fails.
  1406. */
  1407. static int bq27xxx_battery_pwr_avg(struct bq27xxx_device_info *di,
  1408. union power_supply_propval *val)
  1409. {
  1410. int power;
  1411. power = bq27xxx_read(di, BQ27XXX_REG_AP, false);
  1412. if (power < 0) {
  1413. dev_err(di->dev,
  1414. "error reading average power register %02x: %d\n",
  1415. BQ27XXX_REG_AP, power);
  1416. return power;
  1417. }
  1418. if (di->opts & BQ27XXX_O_ZERO)
  1419. val->intval = (power * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
  1420. else
  1421. /* Other gauges return a signed value in units of 10mW */
  1422. val->intval = (int)((s16)power) * 10000;
  1423. return 0;
  1424. }
  1425. static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
  1426. union power_supply_propval *val)
  1427. {
  1428. int status;
  1429. if (di->opts & BQ27XXX_O_ZERO) {
  1430. if (di->cache.flags & BQ27000_FLAG_FC)
  1431. status = POWER_SUPPLY_STATUS_FULL;
  1432. else if (di->cache.flags & BQ27000_FLAG_CHGS)
  1433. status = POWER_SUPPLY_STATUS_CHARGING;
  1434. else if (power_supply_am_i_supplied(di->bat))
  1435. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  1436. else
  1437. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1438. } else {
  1439. if (di->cache.flags & BQ27XXX_FLAG_FC)
  1440. status = POWER_SUPPLY_STATUS_FULL;
  1441. else if (di->cache.flags & BQ27XXX_FLAG_DSC)
  1442. status = POWER_SUPPLY_STATUS_DISCHARGING;
  1443. else
  1444. status = POWER_SUPPLY_STATUS_CHARGING;
  1445. }
  1446. val->intval = status;
  1447. return 0;
  1448. }
  1449. static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
  1450. union power_supply_propval *val)
  1451. {
  1452. int level;
  1453. if (di->opts & BQ27XXX_O_ZERO) {
  1454. if (di->cache.flags & BQ27000_FLAG_FC)
  1455. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  1456. else if (di->cache.flags & BQ27000_FLAG_EDV1)
  1457. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  1458. else if (di->cache.flags & BQ27000_FLAG_EDVF)
  1459. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  1460. else
  1461. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  1462. } else {
  1463. if (di->cache.flags & BQ27XXX_FLAG_FC)
  1464. level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  1465. else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
  1466. level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  1467. else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
  1468. level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  1469. else
  1470. level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  1471. }
  1472. val->intval = level;
  1473. return 0;
  1474. }
  1475. /*
  1476. * Return the battery Voltage in millivolts
  1477. * Or < 0 if something fails.
  1478. */
  1479. static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
  1480. union power_supply_propval *val)
  1481. {
  1482. int volt;
  1483. volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
  1484. if (volt < 0) {
  1485. dev_err(di->dev, "error reading voltage\n");
  1486. return volt;
  1487. }
  1488. val->intval = volt * 1000;
  1489. return 0;
  1490. }
  1491. static int bq27xxx_simple_value(int value,
  1492. union power_supply_propval *val)
  1493. {
  1494. if (value < 0)
  1495. return value;
  1496. val->intval = value;
  1497. return 0;
  1498. }
  1499. static int bq27xxx_battery_get_property(struct power_supply *psy,
  1500. enum power_supply_property psp,
  1501. union power_supply_propval *val)
  1502. {
  1503. int ret = 0;
  1504. struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
  1505. mutex_lock(&di->lock);
  1506. if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
  1507. cancel_delayed_work_sync(&di->work);
  1508. bq27xxx_battery_poll(&di->work.work);
  1509. }
  1510. mutex_unlock(&di->lock);
  1511. if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
  1512. return -ENODEV;
  1513. switch (psp) {
  1514. case POWER_SUPPLY_PROP_STATUS:
  1515. ret = bq27xxx_battery_status(di, val);
  1516. break;
  1517. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1518. ret = bq27xxx_battery_voltage(di, val);
  1519. break;
  1520. case POWER_SUPPLY_PROP_PRESENT:
  1521. val->intval = di->cache.flags < 0 ? 0 : 1;
  1522. break;
  1523. case POWER_SUPPLY_PROP_CURRENT_NOW:
  1524. ret = bq27xxx_battery_current(di, val);
  1525. break;
  1526. case POWER_SUPPLY_PROP_CAPACITY:
  1527. ret = bq27xxx_simple_value(di->cache.capacity, val);
  1528. break;
  1529. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  1530. ret = bq27xxx_battery_capacity_level(di, val);
  1531. break;
  1532. case POWER_SUPPLY_PROP_TEMP:
  1533. ret = bq27xxx_simple_value(di->cache.temperature, val);
  1534. if (ret == 0)
  1535. val->intval -= 2731; /* convert decidegree k to c */
  1536. break;
  1537. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
  1538. ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
  1539. break;
  1540. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  1541. ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
  1542. break;
  1543. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  1544. ret = bq27xxx_simple_value(di->cache.time_to_full, val);
  1545. break;
  1546. case POWER_SUPPLY_PROP_TECHNOLOGY:
  1547. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  1548. break;
  1549. case POWER_SUPPLY_PROP_CHARGE_NOW:
  1550. ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
  1551. break;
  1552. case POWER_SUPPLY_PROP_CHARGE_FULL:
  1553. ret = bq27xxx_simple_value(di->cache.charge_full, val);
  1554. break;
  1555. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  1556. ret = bq27xxx_simple_value(di->charge_design_full, val);
  1557. break;
  1558. /*
  1559. * TODO: Implement these to make registers set from
  1560. * power_supply_battery_info visible in sysfs.
  1561. */
  1562. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  1563. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  1564. return -EINVAL;
  1565. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  1566. ret = bq27xxx_simple_value(di->cache.cycle_count, val);
  1567. break;
  1568. case POWER_SUPPLY_PROP_ENERGY_NOW:
  1569. ret = bq27xxx_simple_value(di->cache.energy, val);
  1570. break;
  1571. case POWER_SUPPLY_PROP_POWER_AVG:
  1572. ret = bq27xxx_battery_pwr_avg(di, val);
  1573. break;
  1574. case POWER_SUPPLY_PROP_HEALTH:
  1575. ret = bq27xxx_simple_value(di->cache.health, val);
  1576. break;
  1577. case POWER_SUPPLY_PROP_MANUFACTURER:
  1578. val->strval = BQ27XXX_MANUFACTURER;
  1579. break;
  1580. default:
  1581. return -EINVAL;
  1582. }
  1583. return ret;
  1584. }
  1585. static void bq27xxx_external_power_changed(struct power_supply *psy)
  1586. {
  1587. struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
  1588. cancel_delayed_work_sync(&di->work);
  1589. schedule_delayed_work(&di->work, 0);
  1590. }
  1591. int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
  1592. {
  1593. struct power_supply_desc *psy_desc;
  1594. struct power_supply_config psy_cfg = {
  1595. .of_node = di->dev->of_node,
  1596. .drv_data = di,
  1597. };
  1598. INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
  1599. mutex_init(&di->lock);
  1600. di->regs = bq27xxx_chip_data[di->chip].regs;
  1601. di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
  1602. di->dm_regs = bq27xxx_chip_data[di->chip].dm_regs;
  1603. di->opts = bq27xxx_chip_data[di->chip].opts;
  1604. psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
  1605. if (!psy_desc)
  1606. return -ENOMEM;
  1607. psy_desc->name = di->name;
  1608. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  1609. psy_desc->properties = bq27xxx_chip_data[di->chip].props;
  1610. psy_desc->num_properties = bq27xxx_chip_data[di->chip].props_size;
  1611. psy_desc->get_property = bq27xxx_battery_get_property;
  1612. psy_desc->external_power_changed = bq27xxx_external_power_changed;
  1613. di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
  1614. if (IS_ERR(di->bat)) {
  1615. if (PTR_ERR(di->bat) == -EPROBE_DEFER)
  1616. dev_dbg(di->dev, "failed to register battery, deferring probe\n");
  1617. else
  1618. dev_err(di->dev, "failed to register battery\n");
  1619. return PTR_ERR(di->bat);
  1620. }
  1621. bq27xxx_battery_settings(di);
  1622. bq27xxx_battery_update(di);
  1623. mutex_lock(&bq27xxx_list_lock);
  1624. list_add(&di->list, &bq27xxx_battery_devices);
  1625. mutex_unlock(&bq27xxx_list_lock);
  1626. return 0;
  1627. }
  1628. EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
  1629. void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
  1630. {
  1631. /*
  1632. * power_supply_unregister call bq27xxx_battery_get_property which
  1633. * call bq27xxx_battery_poll.
  1634. * Make sure that bq27xxx_battery_poll will not call
  1635. * schedule_delayed_work again after unregister (which cause OOPS).
  1636. */
  1637. poll_interval = 0;
  1638. cancel_delayed_work_sync(&di->work);
  1639. power_supply_unregister(di->bat);
  1640. mutex_lock(&bq27xxx_list_lock);
  1641. list_del(&di->list);
  1642. mutex_unlock(&bq27xxx_list_lock);
  1643. mutex_destroy(&di->lock);
  1644. }
  1645. EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
  1646. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  1647. MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
  1648. MODULE_LICENSE("GPL");