bq24190_charger.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /*
  2. * Driver for the TI bq24190 battery charger.
  3. *
  4. * Author: Mark A. Greer <mgreer@animalcreek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/gpio.h>
  18. #include <linux/i2c.h>
  19. #include <linux/power/bq24190_charger.h>
  20. #define BQ24190_MANUFACTURER "Texas Instruments"
  21. #define BQ24190_REG_ISC 0x00 /* Input Source Control */
  22. #define BQ24190_REG_ISC_EN_HIZ_MASK BIT(7)
  23. #define BQ24190_REG_ISC_EN_HIZ_SHIFT 7
  24. #define BQ24190_REG_ISC_VINDPM_MASK (BIT(6) | BIT(5) | BIT(4) | \
  25. BIT(3))
  26. #define BQ24190_REG_ISC_VINDPM_SHIFT 3
  27. #define BQ24190_REG_ISC_IINLIM_MASK (BIT(2) | BIT(1) | BIT(0))
  28. #define BQ24190_REG_ISC_IINLIM_SHIFT 0
  29. #define BQ24190_REG_POC 0x01 /* Power-On Configuration */
  30. #define BQ24190_REG_POC_RESET_MASK BIT(7)
  31. #define BQ24190_REG_POC_RESET_SHIFT 7
  32. #define BQ24190_REG_POC_WDT_RESET_MASK BIT(6)
  33. #define BQ24190_REG_POC_WDT_RESET_SHIFT 6
  34. #define BQ24190_REG_POC_CHG_CONFIG_MASK (BIT(5) | BIT(4))
  35. #define BQ24190_REG_POC_CHG_CONFIG_SHIFT 4
  36. #define BQ24190_REG_POC_SYS_MIN_MASK (BIT(3) | BIT(2) | BIT(1))
  37. #define BQ24190_REG_POC_SYS_MIN_SHIFT 1
  38. #define BQ24190_REG_POC_BOOST_LIM_MASK BIT(0)
  39. #define BQ24190_REG_POC_BOOST_LIM_SHIFT 0
  40. #define BQ24190_REG_CCC 0x02 /* Charge Current Control */
  41. #define BQ24190_REG_CCC_ICHG_MASK (BIT(7) | BIT(6) | BIT(5) | \
  42. BIT(4) | BIT(3) | BIT(2))
  43. #define BQ24190_REG_CCC_ICHG_SHIFT 2
  44. #define BQ24190_REG_CCC_FORCE_20PCT_MASK BIT(0)
  45. #define BQ24190_REG_CCC_FORCE_20PCT_SHIFT 0
  46. #define BQ24190_REG_PCTCC 0x03 /* Pre-charge/Termination Current Cntl */
  47. #define BQ24190_REG_PCTCC_IPRECHG_MASK (BIT(7) | BIT(6) | BIT(5) | \
  48. BIT(4))
  49. #define BQ24190_REG_PCTCC_IPRECHG_SHIFT 4
  50. #define BQ24190_REG_PCTCC_ITERM_MASK (BIT(3) | BIT(2) | BIT(1) | \
  51. BIT(0))
  52. #define BQ24190_REG_PCTCC_ITERM_SHIFT 0
  53. #define BQ24190_REG_CVC 0x04 /* Charge Voltage Control */
  54. #define BQ24190_REG_CVC_VREG_MASK (BIT(7) | BIT(6) | BIT(5) | \
  55. BIT(4) | BIT(3) | BIT(2))
  56. #define BQ24190_REG_CVC_VREG_SHIFT 2
  57. #define BQ24190_REG_CVC_BATLOWV_MASK BIT(1)
  58. #define BQ24190_REG_CVC_BATLOWV_SHIFT 1
  59. #define BQ24190_REG_CVC_VRECHG_MASK BIT(0)
  60. #define BQ24190_REG_CVC_VRECHG_SHIFT 0
  61. #define BQ24190_REG_CTTC 0x05 /* Charge Term/Timer Control */
  62. #define BQ24190_REG_CTTC_EN_TERM_MASK BIT(7)
  63. #define BQ24190_REG_CTTC_EN_TERM_SHIFT 7
  64. #define BQ24190_REG_CTTC_TERM_STAT_MASK BIT(6)
  65. #define BQ24190_REG_CTTC_TERM_STAT_SHIFT 6
  66. #define BQ24190_REG_CTTC_WATCHDOG_MASK (BIT(5) | BIT(4))
  67. #define BQ24190_REG_CTTC_WATCHDOG_SHIFT 4
  68. #define BQ24190_REG_CTTC_EN_TIMER_MASK BIT(3)
  69. #define BQ24190_REG_CTTC_EN_TIMER_SHIFT 3
  70. #define BQ24190_REG_CTTC_CHG_TIMER_MASK (BIT(2) | BIT(1))
  71. #define BQ24190_REG_CTTC_CHG_TIMER_SHIFT 1
  72. #define BQ24190_REG_CTTC_JEITA_ISET_MASK BIT(0)
  73. #define BQ24190_REG_CTTC_JEITA_ISET_SHIFT 0
  74. #define BQ24190_REG_ICTRC 0x06 /* IR Comp/Thermal Regulation Control */
  75. #define BQ24190_REG_ICTRC_BAT_COMP_MASK (BIT(7) | BIT(6) | BIT(5))
  76. #define BQ24190_REG_ICTRC_BAT_COMP_SHIFT 5
  77. #define BQ24190_REG_ICTRC_VCLAMP_MASK (BIT(4) | BIT(3) | BIT(2))
  78. #define BQ24190_REG_ICTRC_VCLAMP_SHIFT 2
  79. #define BQ24190_REG_ICTRC_TREG_MASK (BIT(1) | BIT(0))
  80. #define BQ24190_REG_ICTRC_TREG_SHIFT 0
  81. #define BQ24190_REG_MOC 0x07 /* Misc. Operation Control */
  82. #define BQ24190_REG_MOC_DPDM_EN_MASK BIT(7)
  83. #define BQ24190_REG_MOC_DPDM_EN_SHIFT 7
  84. #define BQ24190_REG_MOC_TMR2X_EN_MASK BIT(6)
  85. #define BQ24190_REG_MOC_TMR2X_EN_SHIFT 6
  86. #define BQ24190_REG_MOC_BATFET_DISABLE_MASK BIT(5)
  87. #define BQ24190_REG_MOC_BATFET_DISABLE_SHIFT 5
  88. #define BQ24190_REG_MOC_JEITA_VSET_MASK BIT(4)
  89. #define BQ24190_REG_MOC_JEITA_VSET_SHIFT 4
  90. #define BQ24190_REG_MOC_INT_MASK_MASK (BIT(1) | BIT(0))
  91. #define BQ24190_REG_MOC_INT_MASK_SHIFT 0
  92. #define BQ24190_REG_SS 0x08 /* System Status */
  93. #define BQ24190_REG_SS_VBUS_STAT_MASK (BIT(7) | BIT(6))
  94. #define BQ24190_REG_SS_VBUS_STAT_SHIFT 6
  95. #define BQ24190_REG_SS_CHRG_STAT_MASK (BIT(5) | BIT(4))
  96. #define BQ24190_REG_SS_CHRG_STAT_SHIFT 4
  97. #define BQ24190_REG_SS_DPM_STAT_MASK BIT(3)
  98. #define BQ24190_REG_SS_DPM_STAT_SHIFT 3
  99. #define BQ24190_REG_SS_PG_STAT_MASK BIT(2)
  100. #define BQ24190_REG_SS_PG_STAT_SHIFT 2
  101. #define BQ24190_REG_SS_THERM_STAT_MASK BIT(1)
  102. #define BQ24190_REG_SS_THERM_STAT_SHIFT 1
  103. #define BQ24190_REG_SS_VSYS_STAT_MASK BIT(0)
  104. #define BQ24190_REG_SS_VSYS_STAT_SHIFT 0
  105. #define BQ24190_REG_F 0x09 /* Fault */
  106. #define BQ24190_REG_F_WATCHDOG_FAULT_MASK BIT(7)
  107. #define BQ24190_REG_F_WATCHDOG_FAULT_SHIFT 7
  108. #define BQ24190_REG_F_BOOST_FAULT_MASK BIT(6)
  109. #define BQ24190_REG_F_BOOST_FAULT_SHIFT 6
  110. #define BQ24190_REG_F_CHRG_FAULT_MASK (BIT(5) | BIT(4))
  111. #define BQ24190_REG_F_CHRG_FAULT_SHIFT 4
  112. #define BQ24190_REG_F_BAT_FAULT_MASK BIT(3)
  113. #define BQ24190_REG_F_BAT_FAULT_SHIFT 3
  114. #define BQ24190_REG_F_NTC_FAULT_MASK (BIT(2) | BIT(1) | BIT(0))
  115. #define BQ24190_REG_F_NTC_FAULT_SHIFT 0
  116. #define BQ24190_REG_VPRS 0x0A /* Vendor/Part/Revision Status */
  117. #define BQ24190_REG_VPRS_PN_MASK (BIT(5) | BIT(4) | BIT(3))
  118. #define BQ24190_REG_VPRS_PN_SHIFT 3
  119. #define BQ24190_REG_VPRS_PN_24190 0x4
  120. #define BQ24190_REG_VPRS_PN_24192 0x5 /* Also 24193 */
  121. #define BQ24190_REG_VPRS_PN_24192I 0x3
  122. #define BQ24190_REG_VPRS_TS_PROFILE_MASK BIT(2)
  123. #define BQ24190_REG_VPRS_TS_PROFILE_SHIFT 2
  124. #define BQ24190_REG_VPRS_DEV_REG_MASK (BIT(1) | BIT(0))
  125. #define BQ24190_REG_VPRS_DEV_REG_SHIFT 0
  126. /*
  127. * The FAULT register is latched by the bq24190 (except for NTC_FAULT)
  128. * so the first read after a fault returns the latched value and subsequent
  129. * reads return the current value. In order to return the fault status
  130. * to the user, have the interrupt handler save the reg's value and retrieve
  131. * it in the appropriate health/status routine.
  132. */
  133. struct bq24190_dev_info {
  134. struct i2c_client *client;
  135. struct device *dev;
  136. struct power_supply *charger;
  137. struct power_supply *battery;
  138. char model_name[I2C_NAME_SIZE];
  139. kernel_ulong_t model;
  140. unsigned int gpio_int;
  141. unsigned int irq;
  142. struct mutex f_reg_lock;
  143. u8 f_reg;
  144. u8 ss_reg;
  145. u8 watchdog;
  146. };
  147. /*
  148. * The tables below provide a 2-way mapping for the value that goes in
  149. * the register field and the real-world value that it represents.
  150. * The index of the array is the value that goes in the register; the
  151. * number at that index in the array is the real-world value that it
  152. * represents.
  153. */
  154. /* REG02[7:2] (ICHG) in uAh */
  155. static const int bq24190_ccc_ichg_values[] = {
  156. 512000, 576000, 640000, 704000, 768000, 832000, 896000, 960000,
  157. 1024000, 1088000, 1152000, 1216000, 1280000, 1344000, 1408000, 1472000,
  158. 1536000, 1600000, 1664000, 1728000, 1792000, 1856000, 1920000, 1984000,
  159. 2048000, 2112000, 2176000, 2240000, 2304000, 2368000, 2432000, 2496000,
  160. 2560000, 2624000, 2688000, 2752000, 2816000, 2880000, 2944000, 3008000,
  161. 3072000, 3136000, 3200000, 3264000, 3328000, 3392000, 3456000, 3520000,
  162. 3584000, 3648000, 3712000, 3776000, 3840000, 3904000, 3968000, 4032000,
  163. 4096000, 4160000, 4224000, 4288000, 4352000, 4416000, 4480000, 4544000
  164. };
  165. /* REG04[7:2] (VREG) in uV */
  166. static const int bq24190_cvc_vreg_values[] = {
  167. 3504000, 3520000, 3536000, 3552000, 3568000, 3584000, 3600000, 3616000,
  168. 3632000, 3648000, 3664000, 3680000, 3696000, 3712000, 3728000, 3744000,
  169. 3760000, 3776000, 3792000, 3808000, 3824000, 3840000, 3856000, 3872000,
  170. 3888000, 3904000, 3920000, 3936000, 3952000, 3968000, 3984000, 4000000,
  171. 4016000, 4032000, 4048000, 4064000, 4080000, 4096000, 4112000, 4128000,
  172. 4144000, 4160000, 4176000, 4192000, 4208000, 4224000, 4240000, 4256000,
  173. 4272000, 4288000, 4304000, 4320000, 4336000, 4352000, 4368000, 4384000,
  174. 4400000
  175. };
  176. /* REG06[1:0] (TREG) in tenths of degrees Celcius */
  177. static const int bq24190_ictrc_treg_values[] = {
  178. 600, 800, 1000, 1200
  179. };
  180. /*
  181. * Return the index in 'tbl' of greatest value that is less than or equal to
  182. * 'val'. The index range returned is 0 to 'tbl_size' - 1. Assumes that
  183. * the values in 'tbl' are sorted from smallest to largest and 'tbl_size'
  184. * is less than 2^8.
  185. */
  186. static u8 bq24190_find_idx(const int tbl[], int tbl_size, int v)
  187. {
  188. int i;
  189. for (i = 1; i < tbl_size; i++)
  190. if (v < tbl[i])
  191. break;
  192. return i - 1;
  193. }
  194. /* Basic driver I/O routines */
  195. static int bq24190_read(struct bq24190_dev_info *bdi, u8 reg, u8 *data)
  196. {
  197. int ret;
  198. ret = i2c_smbus_read_byte_data(bdi->client, reg);
  199. if (ret < 0)
  200. return ret;
  201. *data = ret;
  202. return 0;
  203. }
  204. static int bq24190_write(struct bq24190_dev_info *bdi, u8 reg, u8 data)
  205. {
  206. return i2c_smbus_write_byte_data(bdi->client, reg, data);
  207. }
  208. static int bq24190_read_mask(struct bq24190_dev_info *bdi, u8 reg,
  209. u8 mask, u8 shift, u8 *data)
  210. {
  211. u8 v;
  212. int ret;
  213. ret = bq24190_read(bdi, reg, &v);
  214. if (ret < 0)
  215. return ret;
  216. v &= mask;
  217. v >>= shift;
  218. *data = v;
  219. return 0;
  220. }
  221. static int bq24190_write_mask(struct bq24190_dev_info *bdi, u8 reg,
  222. u8 mask, u8 shift, u8 data)
  223. {
  224. u8 v;
  225. int ret;
  226. ret = bq24190_read(bdi, reg, &v);
  227. if (ret < 0)
  228. return ret;
  229. v &= ~mask;
  230. v |= ((data << shift) & mask);
  231. return bq24190_write(bdi, reg, v);
  232. }
  233. static int bq24190_get_field_val(struct bq24190_dev_info *bdi,
  234. u8 reg, u8 mask, u8 shift,
  235. const int tbl[], int tbl_size,
  236. int *val)
  237. {
  238. u8 v;
  239. int ret;
  240. ret = bq24190_read_mask(bdi, reg, mask, shift, &v);
  241. if (ret < 0)
  242. return ret;
  243. v = (v >= tbl_size) ? (tbl_size - 1) : v;
  244. *val = tbl[v];
  245. return 0;
  246. }
  247. static int bq24190_set_field_val(struct bq24190_dev_info *bdi,
  248. u8 reg, u8 mask, u8 shift,
  249. const int tbl[], int tbl_size,
  250. int val)
  251. {
  252. u8 idx;
  253. idx = bq24190_find_idx(tbl, tbl_size, val);
  254. return bq24190_write_mask(bdi, reg, mask, shift, idx);
  255. }
  256. #ifdef CONFIG_SYSFS
  257. /*
  258. * There are a numerous options that are configurable on the bq24190
  259. * that go well beyond what the power_supply properties provide access to.
  260. * Provide sysfs access to them so they can be examined and possibly modified
  261. * on the fly. They will be provided for the charger power_supply object only
  262. * and will be prefixed by 'f_' to make them easier to recognize.
  263. */
  264. #define BQ24190_SYSFS_FIELD(_name, r, f, m, store) \
  265. { \
  266. .attr = __ATTR(f_##_name, m, bq24190_sysfs_show, store), \
  267. .reg = BQ24190_REG_##r, \
  268. .mask = BQ24190_REG_##r##_##f##_MASK, \
  269. .shift = BQ24190_REG_##r##_##f##_SHIFT, \
  270. }
  271. #define BQ24190_SYSFS_FIELD_RW(_name, r, f) \
  272. BQ24190_SYSFS_FIELD(_name, r, f, S_IWUSR | S_IRUGO, \
  273. bq24190_sysfs_store)
  274. #define BQ24190_SYSFS_FIELD_RO(_name, r, f) \
  275. BQ24190_SYSFS_FIELD(_name, r, f, S_IRUGO, NULL)
  276. static ssize_t bq24190_sysfs_show(struct device *dev,
  277. struct device_attribute *attr, char *buf);
  278. static ssize_t bq24190_sysfs_store(struct device *dev,
  279. struct device_attribute *attr, const char *buf, size_t count);
  280. struct bq24190_sysfs_field_info {
  281. struct device_attribute attr;
  282. u8 reg;
  283. u8 mask;
  284. u8 shift;
  285. };
  286. /* On i386 ptrace-abi.h defines SS that breaks the macro calls below. */
  287. #undef SS
  288. static struct bq24190_sysfs_field_info bq24190_sysfs_field_tbl[] = {
  289. /* sysfs name reg field in reg */
  290. BQ24190_SYSFS_FIELD_RW(en_hiz, ISC, EN_HIZ),
  291. BQ24190_SYSFS_FIELD_RW(vindpm, ISC, VINDPM),
  292. BQ24190_SYSFS_FIELD_RW(iinlim, ISC, IINLIM),
  293. BQ24190_SYSFS_FIELD_RW(chg_config, POC, CHG_CONFIG),
  294. BQ24190_SYSFS_FIELD_RW(sys_min, POC, SYS_MIN),
  295. BQ24190_SYSFS_FIELD_RW(boost_lim, POC, BOOST_LIM),
  296. BQ24190_SYSFS_FIELD_RW(ichg, CCC, ICHG),
  297. BQ24190_SYSFS_FIELD_RW(force_20_pct, CCC, FORCE_20PCT),
  298. BQ24190_SYSFS_FIELD_RW(iprechg, PCTCC, IPRECHG),
  299. BQ24190_SYSFS_FIELD_RW(iterm, PCTCC, ITERM),
  300. BQ24190_SYSFS_FIELD_RW(vreg, CVC, VREG),
  301. BQ24190_SYSFS_FIELD_RW(batlowv, CVC, BATLOWV),
  302. BQ24190_SYSFS_FIELD_RW(vrechg, CVC, VRECHG),
  303. BQ24190_SYSFS_FIELD_RW(en_term, CTTC, EN_TERM),
  304. BQ24190_SYSFS_FIELD_RW(term_stat, CTTC, TERM_STAT),
  305. BQ24190_SYSFS_FIELD_RO(watchdog, CTTC, WATCHDOG),
  306. BQ24190_SYSFS_FIELD_RW(en_timer, CTTC, EN_TIMER),
  307. BQ24190_SYSFS_FIELD_RW(chg_timer, CTTC, CHG_TIMER),
  308. BQ24190_SYSFS_FIELD_RW(jeta_iset, CTTC, JEITA_ISET),
  309. BQ24190_SYSFS_FIELD_RW(bat_comp, ICTRC, BAT_COMP),
  310. BQ24190_SYSFS_FIELD_RW(vclamp, ICTRC, VCLAMP),
  311. BQ24190_SYSFS_FIELD_RW(treg, ICTRC, TREG),
  312. BQ24190_SYSFS_FIELD_RW(dpdm_en, MOC, DPDM_EN),
  313. BQ24190_SYSFS_FIELD_RW(tmr2x_en, MOC, TMR2X_EN),
  314. BQ24190_SYSFS_FIELD_RW(batfet_disable, MOC, BATFET_DISABLE),
  315. BQ24190_SYSFS_FIELD_RW(jeita_vset, MOC, JEITA_VSET),
  316. BQ24190_SYSFS_FIELD_RO(int_mask, MOC, INT_MASK),
  317. BQ24190_SYSFS_FIELD_RO(vbus_stat, SS, VBUS_STAT),
  318. BQ24190_SYSFS_FIELD_RO(chrg_stat, SS, CHRG_STAT),
  319. BQ24190_SYSFS_FIELD_RO(dpm_stat, SS, DPM_STAT),
  320. BQ24190_SYSFS_FIELD_RO(pg_stat, SS, PG_STAT),
  321. BQ24190_SYSFS_FIELD_RO(therm_stat, SS, THERM_STAT),
  322. BQ24190_SYSFS_FIELD_RO(vsys_stat, SS, VSYS_STAT),
  323. BQ24190_SYSFS_FIELD_RO(watchdog_fault, F, WATCHDOG_FAULT),
  324. BQ24190_SYSFS_FIELD_RO(boost_fault, F, BOOST_FAULT),
  325. BQ24190_SYSFS_FIELD_RO(chrg_fault, F, CHRG_FAULT),
  326. BQ24190_SYSFS_FIELD_RO(bat_fault, F, BAT_FAULT),
  327. BQ24190_SYSFS_FIELD_RO(ntc_fault, F, NTC_FAULT),
  328. BQ24190_SYSFS_FIELD_RO(pn, VPRS, PN),
  329. BQ24190_SYSFS_FIELD_RO(ts_profile, VPRS, TS_PROFILE),
  330. BQ24190_SYSFS_FIELD_RO(dev_reg, VPRS, DEV_REG),
  331. };
  332. static struct attribute *
  333. bq24190_sysfs_attrs[ARRAY_SIZE(bq24190_sysfs_field_tbl) + 1];
  334. static const struct attribute_group bq24190_sysfs_attr_group = {
  335. .attrs = bq24190_sysfs_attrs,
  336. };
  337. static void bq24190_sysfs_init_attrs(void)
  338. {
  339. int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl);
  340. for (i = 0; i < limit; i++)
  341. bq24190_sysfs_attrs[i] = &bq24190_sysfs_field_tbl[i].attr.attr;
  342. bq24190_sysfs_attrs[limit] = NULL; /* Has additional entry for this */
  343. }
  344. static struct bq24190_sysfs_field_info *bq24190_sysfs_field_lookup(
  345. const char *name)
  346. {
  347. int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl);
  348. for (i = 0; i < limit; i++)
  349. if (!strcmp(name, bq24190_sysfs_field_tbl[i].attr.attr.name))
  350. break;
  351. if (i >= limit)
  352. return NULL;
  353. return &bq24190_sysfs_field_tbl[i];
  354. }
  355. static ssize_t bq24190_sysfs_show(struct device *dev,
  356. struct device_attribute *attr, char *buf)
  357. {
  358. struct power_supply *psy = dev_get_drvdata(dev);
  359. struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
  360. struct bq24190_sysfs_field_info *info;
  361. int ret;
  362. u8 v;
  363. info = bq24190_sysfs_field_lookup(attr->attr.name);
  364. if (!info)
  365. return -EINVAL;
  366. ret = bq24190_read_mask(bdi, info->reg, info->mask, info->shift, &v);
  367. if (ret)
  368. return ret;
  369. return scnprintf(buf, PAGE_SIZE, "%hhx\n", v);
  370. }
  371. static ssize_t bq24190_sysfs_store(struct device *dev,
  372. struct device_attribute *attr, const char *buf, size_t count)
  373. {
  374. struct power_supply *psy = dev_get_drvdata(dev);
  375. struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
  376. struct bq24190_sysfs_field_info *info;
  377. int ret;
  378. u8 v;
  379. info = bq24190_sysfs_field_lookup(attr->attr.name);
  380. if (!info)
  381. return -EINVAL;
  382. ret = kstrtou8(buf, 0, &v);
  383. if (ret < 0)
  384. return ret;
  385. ret = bq24190_write_mask(bdi, info->reg, info->mask, info->shift, v);
  386. if (ret)
  387. return ret;
  388. return count;
  389. }
  390. static int bq24190_sysfs_create_group(struct bq24190_dev_info *bdi)
  391. {
  392. bq24190_sysfs_init_attrs();
  393. return sysfs_create_group(&bdi->charger->dev.kobj,
  394. &bq24190_sysfs_attr_group);
  395. }
  396. static void bq24190_sysfs_remove_group(struct bq24190_dev_info *bdi)
  397. {
  398. sysfs_remove_group(&bdi->charger->dev.kobj, &bq24190_sysfs_attr_group);
  399. }
  400. #else
  401. static int bq24190_sysfs_create_group(struct bq24190_dev_info *bdi)
  402. {
  403. return 0;
  404. }
  405. static inline void bq24190_sysfs_remove_group(struct bq24190_dev_info *bdi) {}
  406. #endif
  407. /*
  408. * According to the "Host Mode and default Mode" section of the
  409. * manual, a write to any register causes the bq24190 to switch
  410. * from default mode to host mode. It will switch back to default
  411. * mode after a WDT timeout unless the WDT is turned off as well.
  412. * So, by simply turning off the WDT, we accomplish both with the
  413. * same write.
  414. */
  415. static int bq24190_set_mode_host(struct bq24190_dev_info *bdi)
  416. {
  417. int ret;
  418. u8 v;
  419. ret = bq24190_read(bdi, BQ24190_REG_CTTC, &v);
  420. if (ret < 0)
  421. return ret;
  422. bdi->watchdog = ((v & BQ24190_REG_CTTC_WATCHDOG_MASK) >>
  423. BQ24190_REG_CTTC_WATCHDOG_SHIFT);
  424. v &= ~BQ24190_REG_CTTC_WATCHDOG_MASK;
  425. return bq24190_write(bdi, BQ24190_REG_CTTC, v);
  426. }
  427. static int bq24190_register_reset(struct bq24190_dev_info *bdi)
  428. {
  429. int ret, limit = 100;
  430. u8 v;
  431. if (device_property_read_bool(bdi->dev, "disable-reset"))
  432. return 0;
  433. /* Reset the registers */
  434. ret = bq24190_write_mask(bdi, BQ24190_REG_POC,
  435. BQ24190_REG_POC_RESET_MASK,
  436. BQ24190_REG_POC_RESET_SHIFT,
  437. 0x1);
  438. if (ret < 0)
  439. return ret;
  440. /* Reset bit will be cleared by hardware so poll until it is */
  441. do {
  442. ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
  443. BQ24190_REG_POC_RESET_MASK,
  444. BQ24190_REG_POC_RESET_SHIFT,
  445. &v);
  446. if (ret < 0)
  447. return ret;
  448. if (!v)
  449. break;
  450. udelay(10);
  451. } while (--limit);
  452. if (!limit)
  453. return -EIO;
  454. return 0;
  455. }
  456. /* Charger power supply property routines */
  457. static int bq24190_charger_get_charge_type(struct bq24190_dev_info *bdi,
  458. union power_supply_propval *val)
  459. {
  460. u8 v;
  461. int type, ret;
  462. ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
  463. BQ24190_REG_POC_CHG_CONFIG_MASK,
  464. BQ24190_REG_POC_CHG_CONFIG_SHIFT,
  465. &v);
  466. if (ret < 0)
  467. return ret;
  468. /* If POC[CHG_CONFIG] (REG01[5:4]) == 0, charge is disabled */
  469. if (!v) {
  470. type = POWER_SUPPLY_CHARGE_TYPE_NONE;
  471. } else {
  472. ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
  473. BQ24190_REG_CCC_FORCE_20PCT_MASK,
  474. BQ24190_REG_CCC_FORCE_20PCT_SHIFT,
  475. &v);
  476. if (ret < 0)
  477. return ret;
  478. type = (v) ? POWER_SUPPLY_CHARGE_TYPE_TRICKLE :
  479. POWER_SUPPLY_CHARGE_TYPE_FAST;
  480. }
  481. val->intval = type;
  482. return 0;
  483. }
  484. static int bq24190_charger_set_charge_type(struct bq24190_dev_info *bdi,
  485. const union power_supply_propval *val)
  486. {
  487. u8 chg_config, force_20pct, en_term;
  488. int ret;
  489. /*
  490. * According to the "Termination when REG02[0] = 1" section of
  491. * the bq24190 manual, the trickle charge could be less than the
  492. * termination current so it recommends turning off the termination
  493. * function.
  494. *
  495. * Note: AFAICT from the datasheet, the user will have to manually
  496. * turn off the charging when in 20% mode. If its not turned off,
  497. * there could be battery damage. So, use this mode at your own risk.
  498. */
  499. switch (val->intval) {
  500. case POWER_SUPPLY_CHARGE_TYPE_NONE:
  501. chg_config = 0x0;
  502. break;
  503. case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
  504. chg_config = 0x1;
  505. force_20pct = 0x1;
  506. en_term = 0x0;
  507. break;
  508. case POWER_SUPPLY_CHARGE_TYPE_FAST:
  509. chg_config = 0x1;
  510. force_20pct = 0x0;
  511. en_term = 0x1;
  512. break;
  513. default:
  514. return -EINVAL;
  515. }
  516. if (chg_config) { /* Enabling the charger */
  517. ret = bq24190_write_mask(bdi, BQ24190_REG_CCC,
  518. BQ24190_REG_CCC_FORCE_20PCT_MASK,
  519. BQ24190_REG_CCC_FORCE_20PCT_SHIFT,
  520. force_20pct);
  521. if (ret < 0)
  522. return ret;
  523. ret = bq24190_write_mask(bdi, BQ24190_REG_CTTC,
  524. BQ24190_REG_CTTC_EN_TERM_MASK,
  525. BQ24190_REG_CTTC_EN_TERM_SHIFT,
  526. en_term);
  527. if (ret < 0)
  528. return ret;
  529. }
  530. return bq24190_write_mask(bdi, BQ24190_REG_POC,
  531. BQ24190_REG_POC_CHG_CONFIG_MASK,
  532. BQ24190_REG_POC_CHG_CONFIG_SHIFT, chg_config);
  533. }
  534. static int bq24190_charger_get_health(struct bq24190_dev_info *bdi,
  535. union power_supply_propval *val)
  536. {
  537. u8 v;
  538. int health;
  539. mutex_lock(&bdi->f_reg_lock);
  540. v = bdi->f_reg;
  541. mutex_unlock(&bdi->f_reg_lock);
  542. if (v & BQ24190_REG_F_BOOST_FAULT_MASK) {
  543. /*
  544. * This could be over-current or over-voltage but there's
  545. * no way to tell which. Return 'OVERVOLTAGE' since there
  546. * isn't an 'OVERCURRENT' value defined that we can return
  547. * even if it was over-current.
  548. */
  549. health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  550. } else {
  551. v &= BQ24190_REG_F_CHRG_FAULT_MASK;
  552. v >>= BQ24190_REG_F_CHRG_FAULT_SHIFT;
  553. switch (v) {
  554. case 0x0: /* Normal */
  555. health = POWER_SUPPLY_HEALTH_GOOD;
  556. break;
  557. case 0x1: /* Input Fault (VBUS OVP or VBAT<VBUS<3.8V) */
  558. /*
  559. * This could be over-voltage or under-voltage
  560. * and there's no way to tell which. Instead
  561. * of looking foolish and returning 'OVERVOLTAGE'
  562. * when its really under-voltage, just return
  563. * 'UNSPEC_FAILURE'.
  564. */
  565. health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  566. break;
  567. case 0x2: /* Thermal Shutdown */
  568. health = POWER_SUPPLY_HEALTH_OVERHEAT;
  569. break;
  570. case 0x3: /* Charge Safety Timer Expiration */
  571. health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  572. break;
  573. default:
  574. health = POWER_SUPPLY_HEALTH_UNKNOWN;
  575. }
  576. }
  577. val->intval = health;
  578. return 0;
  579. }
  580. static int bq24190_charger_get_online(struct bq24190_dev_info *bdi,
  581. union power_supply_propval *val)
  582. {
  583. u8 v;
  584. int ret;
  585. ret = bq24190_read_mask(bdi, BQ24190_REG_SS,
  586. BQ24190_REG_SS_PG_STAT_MASK,
  587. BQ24190_REG_SS_PG_STAT_SHIFT, &v);
  588. if (ret < 0)
  589. return ret;
  590. val->intval = v;
  591. return 0;
  592. }
  593. static int bq24190_charger_get_current(struct bq24190_dev_info *bdi,
  594. union power_supply_propval *val)
  595. {
  596. u8 v;
  597. int curr, ret;
  598. ret = bq24190_get_field_val(bdi, BQ24190_REG_CCC,
  599. BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
  600. bq24190_ccc_ichg_values,
  601. ARRAY_SIZE(bq24190_ccc_ichg_values), &curr);
  602. if (ret < 0)
  603. return ret;
  604. ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
  605. BQ24190_REG_CCC_FORCE_20PCT_MASK,
  606. BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
  607. if (ret < 0)
  608. return ret;
  609. /* If FORCE_20PCT is enabled, then current is 20% of ICHG value */
  610. if (v)
  611. curr /= 5;
  612. val->intval = curr;
  613. return 0;
  614. }
  615. static int bq24190_charger_get_current_max(struct bq24190_dev_info *bdi,
  616. union power_supply_propval *val)
  617. {
  618. int idx = ARRAY_SIZE(bq24190_ccc_ichg_values) - 1;
  619. val->intval = bq24190_ccc_ichg_values[idx];
  620. return 0;
  621. }
  622. static int bq24190_charger_set_current(struct bq24190_dev_info *bdi,
  623. const union power_supply_propval *val)
  624. {
  625. u8 v;
  626. int ret, curr = val->intval;
  627. ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
  628. BQ24190_REG_CCC_FORCE_20PCT_MASK,
  629. BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
  630. if (ret < 0)
  631. return ret;
  632. /* If FORCE_20PCT is enabled, have to multiply value passed in by 5 */
  633. if (v)
  634. curr *= 5;
  635. return bq24190_set_field_val(bdi, BQ24190_REG_CCC,
  636. BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
  637. bq24190_ccc_ichg_values,
  638. ARRAY_SIZE(bq24190_ccc_ichg_values), curr);
  639. }
  640. static int bq24190_charger_get_voltage(struct bq24190_dev_info *bdi,
  641. union power_supply_propval *val)
  642. {
  643. int voltage, ret;
  644. ret = bq24190_get_field_val(bdi, BQ24190_REG_CVC,
  645. BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
  646. bq24190_cvc_vreg_values,
  647. ARRAY_SIZE(bq24190_cvc_vreg_values), &voltage);
  648. if (ret < 0)
  649. return ret;
  650. val->intval = voltage;
  651. return 0;
  652. }
  653. static int bq24190_charger_get_voltage_max(struct bq24190_dev_info *bdi,
  654. union power_supply_propval *val)
  655. {
  656. int idx = ARRAY_SIZE(bq24190_cvc_vreg_values) - 1;
  657. val->intval = bq24190_cvc_vreg_values[idx];
  658. return 0;
  659. }
  660. static int bq24190_charger_set_voltage(struct bq24190_dev_info *bdi,
  661. const union power_supply_propval *val)
  662. {
  663. return bq24190_set_field_val(bdi, BQ24190_REG_CVC,
  664. BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
  665. bq24190_cvc_vreg_values,
  666. ARRAY_SIZE(bq24190_cvc_vreg_values), val->intval);
  667. }
  668. static int bq24190_charger_get_property(struct power_supply *psy,
  669. enum power_supply_property psp, union power_supply_propval *val)
  670. {
  671. struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
  672. int ret;
  673. dev_dbg(bdi->dev, "prop: %d\n", psp);
  674. pm_runtime_get_sync(bdi->dev);
  675. switch (psp) {
  676. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  677. ret = bq24190_charger_get_charge_type(bdi, val);
  678. break;
  679. case POWER_SUPPLY_PROP_HEALTH:
  680. ret = bq24190_charger_get_health(bdi, val);
  681. break;
  682. case POWER_SUPPLY_PROP_ONLINE:
  683. ret = bq24190_charger_get_online(bdi, val);
  684. break;
  685. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  686. ret = bq24190_charger_get_current(bdi, val);
  687. break;
  688. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  689. ret = bq24190_charger_get_current_max(bdi, val);
  690. break;
  691. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  692. ret = bq24190_charger_get_voltage(bdi, val);
  693. break;
  694. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  695. ret = bq24190_charger_get_voltage_max(bdi, val);
  696. break;
  697. case POWER_SUPPLY_PROP_SCOPE:
  698. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  699. ret = 0;
  700. break;
  701. case POWER_SUPPLY_PROP_MODEL_NAME:
  702. val->strval = bdi->model_name;
  703. ret = 0;
  704. break;
  705. case POWER_SUPPLY_PROP_MANUFACTURER:
  706. val->strval = BQ24190_MANUFACTURER;
  707. ret = 0;
  708. break;
  709. default:
  710. ret = -ENODATA;
  711. }
  712. pm_runtime_put_sync(bdi->dev);
  713. return ret;
  714. }
  715. static int bq24190_charger_set_property(struct power_supply *psy,
  716. enum power_supply_property psp,
  717. const union power_supply_propval *val)
  718. {
  719. struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
  720. int ret;
  721. dev_dbg(bdi->dev, "prop: %d\n", psp);
  722. pm_runtime_get_sync(bdi->dev);
  723. switch (psp) {
  724. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  725. ret = bq24190_charger_set_charge_type(bdi, val);
  726. break;
  727. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  728. ret = bq24190_charger_set_current(bdi, val);
  729. break;
  730. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  731. ret = bq24190_charger_set_voltage(bdi, val);
  732. break;
  733. default:
  734. ret = -EINVAL;
  735. }
  736. pm_runtime_put_sync(bdi->dev);
  737. return ret;
  738. }
  739. static int bq24190_charger_property_is_writeable(struct power_supply *psy,
  740. enum power_supply_property psp)
  741. {
  742. int ret;
  743. switch (psp) {
  744. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  745. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  746. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  747. ret = 1;
  748. break;
  749. default:
  750. ret = 0;
  751. }
  752. return ret;
  753. }
  754. static enum power_supply_property bq24190_charger_properties[] = {
  755. POWER_SUPPLY_PROP_CHARGE_TYPE,
  756. POWER_SUPPLY_PROP_HEALTH,
  757. POWER_SUPPLY_PROP_ONLINE,
  758. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  759. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  760. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  761. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  762. POWER_SUPPLY_PROP_SCOPE,
  763. POWER_SUPPLY_PROP_MODEL_NAME,
  764. POWER_SUPPLY_PROP_MANUFACTURER,
  765. };
  766. static char *bq24190_charger_supplied_to[] = {
  767. "main-battery",
  768. };
  769. static const struct power_supply_desc bq24190_charger_desc = {
  770. .name = "bq24190-charger",
  771. .type = POWER_SUPPLY_TYPE_USB,
  772. .properties = bq24190_charger_properties,
  773. .num_properties = ARRAY_SIZE(bq24190_charger_properties),
  774. .get_property = bq24190_charger_get_property,
  775. .set_property = bq24190_charger_set_property,
  776. .property_is_writeable = bq24190_charger_property_is_writeable,
  777. };
  778. /* Battery power supply property routines */
  779. static int bq24190_battery_get_status(struct bq24190_dev_info *bdi,
  780. union power_supply_propval *val)
  781. {
  782. u8 ss_reg, chrg_fault;
  783. int status, ret;
  784. mutex_lock(&bdi->f_reg_lock);
  785. chrg_fault = bdi->f_reg;
  786. mutex_unlock(&bdi->f_reg_lock);
  787. chrg_fault &= BQ24190_REG_F_CHRG_FAULT_MASK;
  788. chrg_fault >>= BQ24190_REG_F_CHRG_FAULT_SHIFT;
  789. ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
  790. if (ret < 0)
  791. return ret;
  792. /*
  793. * The battery must be discharging when any of these are true:
  794. * - there is no good power source;
  795. * - there is a charge fault.
  796. * Could also be discharging when in "supplement mode" but
  797. * there is no way to tell when its in that mode.
  798. */
  799. if (!(ss_reg & BQ24190_REG_SS_PG_STAT_MASK) || chrg_fault) {
  800. status = POWER_SUPPLY_STATUS_DISCHARGING;
  801. } else {
  802. ss_reg &= BQ24190_REG_SS_CHRG_STAT_MASK;
  803. ss_reg >>= BQ24190_REG_SS_CHRG_STAT_SHIFT;
  804. switch (ss_reg) {
  805. case 0x0: /* Not Charging */
  806. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  807. break;
  808. case 0x1: /* Pre-charge */
  809. case 0x2: /* Fast Charging */
  810. status = POWER_SUPPLY_STATUS_CHARGING;
  811. break;
  812. case 0x3: /* Charge Termination Done */
  813. status = POWER_SUPPLY_STATUS_FULL;
  814. break;
  815. default:
  816. ret = -EIO;
  817. }
  818. }
  819. if (!ret)
  820. val->intval = status;
  821. return ret;
  822. }
  823. static int bq24190_battery_get_health(struct bq24190_dev_info *bdi,
  824. union power_supply_propval *val)
  825. {
  826. u8 v;
  827. int health;
  828. mutex_lock(&bdi->f_reg_lock);
  829. v = bdi->f_reg;
  830. mutex_unlock(&bdi->f_reg_lock);
  831. if (v & BQ24190_REG_F_BAT_FAULT_MASK) {
  832. health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  833. } else {
  834. v &= BQ24190_REG_F_NTC_FAULT_MASK;
  835. v >>= BQ24190_REG_F_NTC_FAULT_SHIFT;
  836. switch (v) {
  837. case 0x0: /* Normal */
  838. health = POWER_SUPPLY_HEALTH_GOOD;
  839. break;
  840. case 0x1: /* TS1 Cold */
  841. case 0x3: /* TS2 Cold */
  842. case 0x5: /* Both Cold */
  843. health = POWER_SUPPLY_HEALTH_COLD;
  844. break;
  845. case 0x2: /* TS1 Hot */
  846. case 0x4: /* TS2 Hot */
  847. case 0x6: /* Both Hot */
  848. health = POWER_SUPPLY_HEALTH_OVERHEAT;
  849. break;
  850. default:
  851. health = POWER_SUPPLY_HEALTH_UNKNOWN;
  852. }
  853. }
  854. val->intval = health;
  855. return 0;
  856. }
  857. static int bq24190_battery_get_online(struct bq24190_dev_info *bdi,
  858. union power_supply_propval *val)
  859. {
  860. u8 batfet_disable;
  861. int ret;
  862. ret = bq24190_read_mask(bdi, BQ24190_REG_MOC,
  863. BQ24190_REG_MOC_BATFET_DISABLE_MASK,
  864. BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable);
  865. if (ret < 0)
  866. return ret;
  867. val->intval = !batfet_disable;
  868. return 0;
  869. }
  870. static int bq24190_battery_set_online(struct bq24190_dev_info *bdi,
  871. const union power_supply_propval *val)
  872. {
  873. return bq24190_write_mask(bdi, BQ24190_REG_MOC,
  874. BQ24190_REG_MOC_BATFET_DISABLE_MASK,
  875. BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, !val->intval);
  876. }
  877. static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi,
  878. union power_supply_propval *val)
  879. {
  880. int temp, ret;
  881. ret = bq24190_get_field_val(bdi, BQ24190_REG_ICTRC,
  882. BQ24190_REG_ICTRC_TREG_MASK,
  883. BQ24190_REG_ICTRC_TREG_SHIFT,
  884. bq24190_ictrc_treg_values,
  885. ARRAY_SIZE(bq24190_ictrc_treg_values), &temp);
  886. if (ret < 0)
  887. return ret;
  888. val->intval = temp;
  889. return 0;
  890. }
  891. static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi,
  892. const union power_supply_propval *val)
  893. {
  894. return bq24190_set_field_val(bdi, BQ24190_REG_ICTRC,
  895. BQ24190_REG_ICTRC_TREG_MASK,
  896. BQ24190_REG_ICTRC_TREG_SHIFT,
  897. bq24190_ictrc_treg_values,
  898. ARRAY_SIZE(bq24190_ictrc_treg_values), val->intval);
  899. }
  900. static int bq24190_battery_get_property(struct power_supply *psy,
  901. enum power_supply_property psp, union power_supply_propval *val)
  902. {
  903. struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
  904. int ret;
  905. dev_dbg(bdi->dev, "prop: %d\n", psp);
  906. pm_runtime_get_sync(bdi->dev);
  907. switch (psp) {
  908. case POWER_SUPPLY_PROP_STATUS:
  909. ret = bq24190_battery_get_status(bdi, val);
  910. break;
  911. case POWER_SUPPLY_PROP_HEALTH:
  912. ret = bq24190_battery_get_health(bdi, val);
  913. break;
  914. case POWER_SUPPLY_PROP_ONLINE:
  915. ret = bq24190_battery_get_online(bdi, val);
  916. break;
  917. case POWER_SUPPLY_PROP_TECHNOLOGY:
  918. /* Could be Li-on or Li-polymer but no way to tell which */
  919. val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  920. ret = 0;
  921. break;
  922. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  923. ret = bq24190_battery_get_temp_alert_max(bdi, val);
  924. break;
  925. case POWER_SUPPLY_PROP_SCOPE:
  926. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  927. ret = 0;
  928. break;
  929. default:
  930. ret = -ENODATA;
  931. }
  932. pm_runtime_put_sync(bdi->dev);
  933. return ret;
  934. }
  935. static int bq24190_battery_set_property(struct power_supply *psy,
  936. enum power_supply_property psp,
  937. const union power_supply_propval *val)
  938. {
  939. struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
  940. int ret;
  941. dev_dbg(bdi->dev, "prop: %d\n", psp);
  942. pm_runtime_get_sync(bdi->dev);
  943. switch (psp) {
  944. case POWER_SUPPLY_PROP_ONLINE:
  945. ret = bq24190_battery_set_online(bdi, val);
  946. break;
  947. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  948. ret = bq24190_battery_set_temp_alert_max(bdi, val);
  949. break;
  950. default:
  951. ret = -EINVAL;
  952. }
  953. pm_runtime_put_sync(bdi->dev);
  954. return ret;
  955. }
  956. static int bq24190_battery_property_is_writeable(struct power_supply *psy,
  957. enum power_supply_property psp)
  958. {
  959. int ret;
  960. switch (psp) {
  961. case POWER_SUPPLY_PROP_ONLINE:
  962. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  963. ret = 1;
  964. break;
  965. default:
  966. ret = 0;
  967. }
  968. return ret;
  969. }
  970. static enum power_supply_property bq24190_battery_properties[] = {
  971. POWER_SUPPLY_PROP_STATUS,
  972. POWER_SUPPLY_PROP_HEALTH,
  973. POWER_SUPPLY_PROP_ONLINE,
  974. POWER_SUPPLY_PROP_TECHNOLOGY,
  975. POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
  976. POWER_SUPPLY_PROP_SCOPE,
  977. };
  978. static const struct power_supply_desc bq24190_battery_desc = {
  979. .name = "bq24190-battery",
  980. .type = POWER_SUPPLY_TYPE_BATTERY,
  981. .properties = bq24190_battery_properties,
  982. .num_properties = ARRAY_SIZE(bq24190_battery_properties),
  983. .get_property = bq24190_battery_get_property,
  984. .set_property = bq24190_battery_set_property,
  985. .property_is_writeable = bq24190_battery_property_is_writeable,
  986. };
  987. static irqreturn_t bq24190_irq_handler_thread(int irq, void *data)
  988. {
  989. struct bq24190_dev_info *bdi = data;
  990. const u8 battery_mask_ss = BQ24190_REG_SS_CHRG_STAT_MASK;
  991. const u8 battery_mask_f = BQ24190_REG_F_BAT_FAULT_MASK
  992. | BQ24190_REG_F_NTC_FAULT_MASK;
  993. bool alert_charger = false, alert_battery = false;
  994. u8 ss_reg = 0, f_reg = 0;
  995. int i, ret;
  996. pm_runtime_get_sync(bdi->dev);
  997. ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
  998. if (ret < 0) {
  999. dev_err(bdi->dev, "Can't read SS reg: %d\n", ret);
  1000. goto out;
  1001. }
  1002. i = 0;
  1003. do {
  1004. ret = bq24190_read(bdi, BQ24190_REG_F, &f_reg);
  1005. if (ret < 0) {
  1006. dev_err(bdi->dev, "Can't read F reg: %d\n", ret);
  1007. goto out;
  1008. }
  1009. } while (f_reg && ++i < 2);
  1010. /* ignore over/under voltage fault after disconnect */
  1011. if (f_reg == (1 << BQ24190_REG_F_CHRG_FAULT_SHIFT) &&
  1012. !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK))
  1013. f_reg = 0;
  1014. if (f_reg != bdi->f_reg) {
  1015. dev_warn(bdi->dev,
  1016. "Fault: boost %d, charge %d, battery %d, ntc %d\n",
  1017. !!(f_reg & BQ24190_REG_F_BOOST_FAULT_MASK),
  1018. !!(f_reg & BQ24190_REG_F_CHRG_FAULT_MASK),
  1019. !!(f_reg & BQ24190_REG_F_BAT_FAULT_MASK),
  1020. !!(f_reg & BQ24190_REG_F_NTC_FAULT_MASK));
  1021. mutex_lock(&bdi->f_reg_lock);
  1022. if ((bdi->f_reg & battery_mask_f) != (f_reg & battery_mask_f))
  1023. alert_battery = true;
  1024. if ((bdi->f_reg & ~battery_mask_f) != (f_reg & ~battery_mask_f))
  1025. alert_charger = true;
  1026. bdi->f_reg = f_reg;
  1027. mutex_unlock(&bdi->f_reg_lock);
  1028. }
  1029. if (ss_reg != bdi->ss_reg) {
  1030. /*
  1031. * The device is in host mode so when PG_STAT goes from 1->0
  1032. * (i.e., power removed) HIZ needs to be disabled.
  1033. */
  1034. if ((bdi->ss_reg & BQ24190_REG_SS_PG_STAT_MASK) &&
  1035. !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) {
  1036. ret = bq24190_write_mask(bdi, BQ24190_REG_ISC,
  1037. BQ24190_REG_ISC_EN_HIZ_MASK,
  1038. BQ24190_REG_ISC_EN_HIZ_SHIFT,
  1039. 0);
  1040. if (ret < 0)
  1041. dev_err(bdi->dev, "Can't access ISC reg: %d\n",
  1042. ret);
  1043. }
  1044. if ((bdi->ss_reg & battery_mask_ss) != (ss_reg & battery_mask_ss))
  1045. alert_battery = true;
  1046. if ((bdi->ss_reg & ~battery_mask_ss) != (ss_reg & ~battery_mask_ss))
  1047. alert_charger = true;
  1048. bdi->ss_reg = ss_reg;
  1049. }
  1050. if (alert_charger)
  1051. power_supply_changed(bdi->charger);
  1052. if (alert_battery)
  1053. power_supply_changed(bdi->battery);
  1054. out:
  1055. pm_runtime_put_sync(bdi->dev);
  1056. dev_dbg(bdi->dev, "ss_reg: 0x%02x, f_reg: 0x%02x\n", ss_reg, f_reg);
  1057. return IRQ_HANDLED;
  1058. }
  1059. static int bq24190_hw_init(struct bq24190_dev_info *bdi)
  1060. {
  1061. u8 v;
  1062. int ret;
  1063. pm_runtime_get_sync(bdi->dev);
  1064. /* First check that the device really is what its supposed to be */
  1065. ret = bq24190_read_mask(bdi, BQ24190_REG_VPRS,
  1066. BQ24190_REG_VPRS_PN_MASK,
  1067. BQ24190_REG_VPRS_PN_SHIFT,
  1068. &v);
  1069. if (ret < 0)
  1070. goto out;
  1071. if (v != bdi->model) {
  1072. ret = -ENODEV;
  1073. goto out;
  1074. }
  1075. ret = bq24190_register_reset(bdi);
  1076. if (ret < 0)
  1077. goto out;
  1078. ret = bq24190_set_mode_host(bdi);
  1079. if (ret < 0)
  1080. goto out;
  1081. ret = bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
  1082. out:
  1083. pm_runtime_put_sync(bdi->dev);
  1084. return ret;
  1085. }
  1086. #ifdef CONFIG_OF
  1087. static int bq24190_setup_dt(struct bq24190_dev_info *bdi)
  1088. {
  1089. bdi->irq = irq_of_parse_and_map(bdi->dev->of_node, 0);
  1090. if (bdi->irq <= 0)
  1091. return -1;
  1092. return 0;
  1093. }
  1094. #else
  1095. static int bq24190_setup_dt(struct bq24190_dev_info *bdi)
  1096. {
  1097. return -1;
  1098. }
  1099. #endif
  1100. static int bq24190_setup_pdata(struct bq24190_dev_info *bdi,
  1101. struct bq24190_platform_data *pdata)
  1102. {
  1103. int ret;
  1104. if (!gpio_is_valid(pdata->gpio_int))
  1105. return -1;
  1106. ret = gpio_request(pdata->gpio_int, dev_name(bdi->dev));
  1107. if (ret < 0)
  1108. return -1;
  1109. ret = gpio_direction_input(pdata->gpio_int);
  1110. if (ret < 0)
  1111. goto out;
  1112. bdi->irq = gpio_to_irq(pdata->gpio_int);
  1113. if (!bdi->irq)
  1114. goto out;
  1115. bdi->gpio_int = pdata->gpio_int;
  1116. return 0;
  1117. out:
  1118. gpio_free(pdata->gpio_int);
  1119. return -1;
  1120. }
  1121. static int bq24190_probe(struct i2c_client *client,
  1122. const struct i2c_device_id *id)
  1123. {
  1124. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1125. struct device *dev = &client->dev;
  1126. struct bq24190_platform_data *pdata = client->dev.platform_data;
  1127. struct power_supply_config charger_cfg = {}, battery_cfg = {};
  1128. struct bq24190_dev_info *bdi;
  1129. int ret;
  1130. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1131. dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
  1132. return -ENODEV;
  1133. }
  1134. bdi = devm_kzalloc(dev, sizeof(*bdi), GFP_KERNEL);
  1135. if (!bdi) {
  1136. dev_err(dev, "Can't alloc bdi struct\n");
  1137. return -ENOMEM;
  1138. }
  1139. bdi->client = client;
  1140. bdi->dev = dev;
  1141. bdi->model = id->driver_data;
  1142. strncpy(bdi->model_name, id->name, I2C_NAME_SIZE);
  1143. mutex_init(&bdi->f_reg_lock);
  1144. bdi->f_reg = 0;
  1145. bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
  1146. i2c_set_clientdata(client, bdi);
  1147. if (dev->of_node)
  1148. ret = bq24190_setup_dt(bdi);
  1149. else
  1150. ret = bq24190_setup_pdata(bdi, pdata);
  1151. if (ret) {
  1152. dev_err(dev, "Can't get irq info\n");
  1153. return -EINVAL;
  1154. }
  1155. pm_runtime_enable(dev);
  1156. pm_runtime_resume(dev);
  1157. ret = bq24190_hw_init(bdi);
  1158. if (ret < 0) {
  1159. dev_err(dev, "Hardware init failed\n");
  1160. goto out1;
  1161. }
  1162. charger_cfg.drv_data = bdi;
  1163. charger_cfg.supplied_to = bq24190_charger_supplied_to;
  1164. charger_cfg.num_supplicants = ARRAY_SIZE(bq24190_charger_supplied_to),
  1165. bdi->charger = power_supply_register(dev, &bq24190_charger_desc,
  1166. &charger_cfg);
  1167. if (IS_ERR(bdi->charger)) {
  1168. dev_err(dev, "Can't register charger\n");
  1169. ret = PTR_ERR(bdi->charger);
  1170. goto out1;
  1171. }
  1172. battery_cfg.drv_data = bdi;
  1173. bdi->battery = power_supply_register(dev, &bq24190_battery_desc,
  1174. &battery_cfg);
  1175. if (IS_ERR(bdi->battery)) {
  1176. dev_err(dev, "Can't register battery\n");
  1177. ret = PTR_ERR(bdi->battery);
  1178. goto out2;
  1179. }
  1180. ret = bq24190_sysfs_create_group(bdi);
  1181. if (ret) {
  1182. dev_err(dev, "Can't create sysfs entries\n");
  1183. goto out3;
  1184. }
  1185. ret = devm_request_threaded_irq(dev, bdi->irq, NULL,
  1186. bq24190_irq_handler_thread,
  1187. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1188. "bq24190-charger", bdi);
  1189. if (ret < 0) {
  1190. dev_err(dev, "Can't set up irq handler\n");
  1191. goto out4;
  1192. }
  1193. return 0;
  1194. out4:
  1195. bq24190_sysfs_remove_group(bdi);
  1196. out3:
  1197. power_supply_unregister(bdi->battery);
  1198. out2:
  1199. power_supply_unregister(bdi->charger);
  1200. out1:
  1201. pm_runtime_disable(dev);
  1202. if (bdi->gpio_int)
  1203. gpio_free(bdi->gpio_int);
  1204. return ret;
  1205. }
  1206. static int bq24190_remove(struct i2c_client *client)
  1207. {
  1208. struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
  1209. pm_runtime_get_sync(bdi->dev);
  1210. bq24190_register_reset(bdi);
  1211. pm_runtime_put_sync(bdi->dev);
  1212. bq24190_sysfs_remove_group(bdi);
  1213. power_supply_unregister(bdi->battery);
  1214. power_supply_unregister(bdi->charger);
  1215. pm_runtime_disable(bdi->dev);
  1216. if (bdi->gpio_int)
  1217. gpio_free(bdi->gpio_int);
  1218. return 0;
  1219. }
  1220. #ifdef CONFIG_PM_SLEEP
  1221. static int bq24190_pm_suspend(struct device *dev)
  1222. {
  1223. struct i2c_client *client = to_i2c_client(dev);
  1224. struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
  1225. pm_runtime_get_sync(bdi->dev);
  1226. bq24190_register_reset(bdi);
  1227. pm_runtime_put_sync(bdi->dev);
  1228. return 0;
  1229. }
  1230. static int bq24190_pm_resume(struct device *dev)
  1231. {
  1232. struct i2c_client *client = to_i2c_client(dev);
  1233. struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
  1234. bdi->f_reg = 0;
  1235. bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
  1236. pm_runtime_get_sync(bdi->dev);
  1237. bq24190_register_reset(bdi);
  1238. bq24190_set_mode_host(bdi);
  1239. bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
  1240. pm_runtime_put_sync(bdi->dev);
  1241. /* Things may have changed while suspended so alert upper layer */
  1242. power_supply_changed(bdi->charger);
  1243. power_supply_changed(bdi->battery);
  1244. return 0;
  1245. }
  1246. #endif
  1247. static SIMPLE_DEV_PM_OPS(bq24190_pm_ops, bq24190_pm_suspend, bq24190_pm_resume);
  1248. /*
  1249. * Only support the bq24190 right now. The bq24192, bq24192i, and bq24193
  1250. * are similar but not identical so the driver needs to be extended to
  1251. * support them.
  1252. */
  1253. static const struct i2c_device_id bq24190_i2c_ids[] = {
  1254. { "bq24190", BQ24190_REG_VPRS_PN_24190 },
  1255. { },
  1256. };
  1257. MODULE_DEVICE_TABLE(i2c, bq24190_i2c_ids);
  1258. #ifdef CONFIG_OF
  1259. static const struct of_device_id bq24190_of_match[] = {
  1260. { .compatible = "ti,bq24190", },
  1261. { },
  1262. };
  1263. MODULE_DEVICE_TABLE(of, bq24190_of_match);
  1264. #else
  1265. static const struct of_device_id bq24190_of_match[] = {
  1266. { },
  1267. };
  1268. #endif
  1269. static struct i2c_driver bq24190_driver = {
  1270. .probe = bq24190_probe,
  1271. .remove = bq24190_remove,
  1272. .id_table = bq24190_i2c_ids,
  1273. .driver = {
  1274. .name = "bq24190-charger",
  1275. .pm = &bq24190_pm_ops,
  1276. .of_match_table = of_match_ptr(bq24190_of_match),
  1277. },
  1278. };
  1279. module_i2c_driver(bq24190_driver);
  1280. MODULE_LICENSE("GPL");
  1281. MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
  1282. MODULE_DESCRIPTION("TI BQ24190 Charger Driver");