bq27xxx_battery.c 61 KB

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