bq27xxx_battery.c 32 KB

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