bq2415x_charger.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. /*
  2. * bq2415x charger driver
  3. *
  4. * Copyright (C) 2011-2013 Pali Rohár <pali.rohar@gmail.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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Datasheets:
  17. * http://www.ti.com/product/bq24150
  18. * http://www.ti.com/product/bq24150a
  19. * http://www.ti.com/product/bq24152
  20. * http://www.ti.com/product/bq24153
  21. * http://www.ti.com/product/bq24153a
  22. * http://www.ti.com/product/bq24155
  23. * http://www.ti.com/product/bq24157s
  24. * http://www.ti.com/product/bq24158
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/param.h>
  29. #include <linux/err.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/sysfs.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/power_supply.h>
  34. #include <linux/idr.h>
  35. #include <linux/i2c.h>
  36. #include <linux/slab.h>
  37. #include <linux/acpi.h>
  38. #include <linux/power/bq2415x_charger.h>
  39. /* timeout for resetting chip timer */
  40. #define BQ2415X_TIMER_TIMEOUT 10
  41. #define BQ2415X_REG_STATUS 0x00
  42. #define BQ2415X_REG_CONTROL 0x01
  43. #define BQ2415X_REG_VOLTAGE 0x02
  44. #define BQ2415X_REG_VENDER 0x03
  45. #define BQ2415X_REG_CURRENT 0x04
  46. /* reset state for all registers */
  47. #define BQ2415X_RESET_STATUS BIT(6)
  48. #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
  49. #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
  50. #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
  51. /* status register */
  52. #define BQ2415X_BIT_TMR_RST 7
  53. #define BQ2415X_BIT_OTG 7
  54. #define BQ2415X_BIT_EN_STAT 6
  55. #define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
  56. #define BQ2415X_SHIFT_STAT 4
  57. #define BQ2415X_BIT_BOOST 3
  58. #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
  59. #define BQ2415X_SHIFT_FAULT 0
  60. /* control register */
  61. #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
  62. #define BQ2415X_SHIFT_LIMIT 6
  63. #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
  64. #define BQ2415X_SHIFT_VLOWV 4
  65. #define BQ2415X_BIT_TE 3
  66. #define BQ2415X_BIT_CE 2
  67. #define BQ2415X_BIT_HZ_MODE 1
  68. #define BQ2415X_BIT_OPA_MODE 0
  69. /* voltage register */
  70. #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
  71. #define BQ2415X_SHIFT_VO 2
  72. #define BQ2415X_BIT_OTG_PL 1
  73. #define BQ2415X_BIT_OTG_EN 0
  74. /* vender register */
  75. #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
  76. #define BQ2415X_SHIFT_VENDER 5
  77. #define BQ2415X_MASK_PN (BIT(3)|BIT(4))
  78. #define BQ2415X_SHIFT_PN 3
  79. #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
  80. #define BQ2415X_SHIFT_REVISION 0
  81. /* current register */
  82. #define BQ2415X_MASK_RESET BIT(7)
  83. #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
  84. #define BQ2415X_SHIFT_VI_CHRG 4
  85. /* N/A BIT(3) */
  86. #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
  87. #define BQ2415X_SHIFT_VI_TERM 0
  88. enum bq2415x_command {
  89. BQ2415X_TIMER_RESET,
  90. BQ2415X_OTG_STATUS,
  91. BQ2415X_STAT_PIN_STATUS,
  92. BQ2415X_STAT_PIN_ENABLE,
  93. BQ2415X_STAT_PIN_DISABLE,
  94. BQ2415X_CHARGE_STATUS,
  95. BQ2415X_BOOST_STATUS,
  96. BQ2415X_FAULT_STATUS,
  97. BQ2415X_CHARGE_TERMINATION_STATUS,
  98. BQ2415X_CHARGE_TERMINATION_ENABLE,
  99. BQ2415X_CHARGE_TERMINATION_DISABLE,
  100. BQ2415X_CHARGER_STATUS,
  101. BQ2415X_CHARGER_ENABLE,
  102. BQ2415X_CHARGER_DISABLE,
  103. BQ2415X_HIGH_IMPEDANCE_STATUS,
  104. BQ2415X_HIGH_IMPEDANCE_ENABLE,
  105. BQ2415X_HIGH_IMPEDANCE_DISABLE,
  106. BQ2415X_BOOST_MODE_STATUS,
  107. BQ2415X_BOOST_MODE_ENABLE,
  108. BQ2415X_BOOST_MODE_DISABLE,
  109. BQ2415X_OTG_LEVEL,
  110. BQ2415X_OTG_ACTIVATE_HIGH,
  111. BQ2415X_OTG_ACTIVATE_LOW,
  112. BQ2415X_OTG_PIN_STATUS,
  113. BQ2415X_OTG_PIN_ENABLE,
  114. BQ2415X_OTG_PIN_DISABLE,
  115. BQ2415X_VENDER_CODE,
  116. BQ2415X_PART_NUMBER,
  117. BQ2415X_REVISION,
  118. };
  119. enum bq2415x_chip {
  120. BQUNKNOWN,
  121. BQ24150,
  122. BQ24150A,
  123. BQ24151,
  124. BQ24151A,
  125. BQ24152,
  126. BQ24153,
  127. BQ24153A,
  128. BQ24155,
  129. BQ24156,
  130. BQ24156A,
  131. BQ24157S,
  132. BQ24158,
  133. };
  134. static char *bq2415x_chip_name[] = {
  135. "unknown",
  136. "bq24150",
  137. "bq24150a",
  138. "bq24151",
  139. "bq24151a",
  140. "bq24152",
  141. "bq24153",
  142. "bq24153a",
  143. "bq24155",
  144. "bq24156",
  145. "bq24156a",
  146. "bq24157s",
  147. "bq24158",
  148. };
  149. struct bq2415x_device {
  150. struct device *dev;
  151. struct bq2415x_platform_data init_data;
  152. struct power_supply *charger;
  153. struct power_supply_desc charger_desc;
  154. struct delayed_work work;
  155. struct device_node *notify_node;
  156. struct notifier_block nb;
  157. enum bq2415x_mode reported_mode;/* mode reported by hook function */
  158. enum bq2415x_mode mode; /* currently configured mode */
  159. enum bq2415x_chip chip;
  160. const char *timer_error;
  161. char *model;
  162. char *name;
  163. int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
  164. int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
  165. int id;
  166. };
  167. /* each registered chip must have unique id */
  168. static DEFINE_IDR(bq2415x_id);
  169. static DEFINE_MUTEX(bq2415x_id_mutex);
  170. static DEFINE_MUTEX(bq2415x_timer_mutex);
  171. static DEFINE_MUTEX(bq2415x_i2c_mutex);
  172. /**** i2c read functions ****/
  173. /* read value from register */
  174. static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
  175. {
  176. struct i2c_client *client = to_i2c_client(bq->dev);
  177. struct i2c_msg msg[2];
  178. u8 val;
  179. int ret;
  180. if (!client->adapter)
  181. return -ENODEV;
  182. msg[0].addr = client->addr;
  183. msg[0].flags = 0;
  184. msg[0].buf = &reg;
  185. msg[0].len = sizeof(reg);
  186. msg[1].addr = client->addr;
  187. msg[1].flags = I2C_M_RD;
  188. msg[1].buf = &val;
  189. msg[1].len = sizeof(val);
  190. mutex_lock(&bq2415x_i2c_mutex);
  191. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  192. mutex_unlock(&bq2415x_i2c_mutex);
  193. if (ret < 0)
  194. return ret;
  195. return val;
  196. }
  197. /* read value from register, apply mask and right shift it */
  198. static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
  199. u8 mask, u8 shift)
  200. {
  201. int ret;
  202. if (shift > 8)
  203. return -EINVAL;
  204. ret = bq2415x_i2c_read(bq, reg);
  205. if (ret < 0)
  206. return ret;
  207. return (ret & mask) >> shift;
  208. }
  209. /* read value from register and return one specified bit */
  210. static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
  211. {
  212. if (bit > 8)
  213. return -EINVAL;
  214. return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
  215. }
  216. /**** i2c write functions ****/
  217. /* write value to register */
  218. static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
  219. {
  220. struct i2c_client *client = to_i2c_client(bq->dev);
  221. struct i2c_msg msg[1];
  222. u8 data[2];
  223. int ret;
  224. data[0] = reg;
  225. data[1] = val;
  226. msg[0].addr = client->addr;
  227. msg[0].flags = 0;
  228. msg[0].buf = data;
  229. msg[0].len = ARRAY_SIZE(data);
  230. mutex_lock(&bq2415x_i2c_mutex);
  231. ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  232. mutex_unlock(&bq2415x_i2c_mutex);
  233. /* i2c_transfer returns number of messages transferred */
  234. if (ret < 0)
  235. return ret;
  236. else if (ret != 1)
  237. return -EIO;
  238. return 0;
  239. }
  240. /* read value from register, change it with mask left shifted and write back */
  241. static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
  242. u8 mask, u8 shift)
  243. {
  244. int ret;
  245. if (shift > 8)
  246. return -EINVAL;
  247. ret = bq2415x_i2c_read(bq, reg);
  248. if (ret < 0)
  249. return ret;
  250. ret &= ~mask;
  251. ret |= val << shift;
  252. return bq2415x_i2c_write(bq, reg, ret);
  253. }
  254. /* change only one bit in register */
  255. static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
  256. bool val, u8 bit)
  257. {
  258. if (bit > 8)
  259. return -EINVAL;
  260. return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
  261. }
  262. /**** global functions ****/
  263. /* exec command function */
  264. static int bq2415x_exec_command(struct bq2415x_device *bq,
  265. enum bq2415x_command command)
  266. {
  267. int ret;
  268. switch (command) {
  269. case BQ2415X_TIMER_RESET:
  270. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
  271. 1, BQ2415X_BIT_TMR_RST);
  272. case BQ2415X_OTG_STATUS:
  273. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
  274. BQ2415X_BIT_OTG);
  275. case BQ2415X_STAT_PIN_STATUS:
  276. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
  277. BQ2415X_BIT_EN_STAT);
  278. case BQ2415X_STAT_PIN_ENABLE:
  279. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
  280. BQ2415X_BIT_EN_STAT);
  281. case BQ2415X_STAT_PIN_DISABLE:
  282. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
  283. BQ2415X_BIT_EN_STAT);
  284. case BQ2415X_CHARGE_STATUS:
  285. return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
  286. BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
  287. case BQ2415X_BOOST_STATUS:
  288. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
  289. BQ2415X_BIT_BOOST);
  290. case BQ2415X_FAULT_STATUS:
  291. return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
  292. BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
  293. case BQ2415X_CHARGE_TERMINATION_STATUS:
  294. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  295. BQ2415X_BIT_TE);
  296. case BQ2415X_CHARGE_TERMINATION_ENABLE:
  297. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  298. 1, BQ2415X_BIT_TE);
  299. case BQ2415X_CHARGE_TERMINATION_DISABLE:
  300. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  301. 0, BQ2415X_BIT_TE);
  302. case BQ2415X_CHARGER_STATUS:
  303. ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  304. BQ2415X_BIT_CE);
  305. if (ret < 0)
  306. return ret;
  307. return ret > 0 ? 0 : 1;
  308. case BQ2415X_CHARGER_ENABLE:
  309. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  310. 0, BQ2415X_BIT_CE);
  311. case BQ2415X_CHARGER_DISABLE:
  312. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  313. 1, BQ2415X_BIT_CE);
  314. case BQ2415X_HIGH_IMPEDANCE_STATUS:
  315. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  316. BQ2415X_BIT_HZ_MODE);
  317. case BQ2415X_HIGH_IMPEDANCE_ENABLE:
  318. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  319. 1, BQ2415X_BIT_HZ_MODE);
  320. case BQ2415X_HIGH_IMPEDANCE_DISABLE:
  321. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  322. 0, BQ2415X_BIT_HZ_MODE);
  323. case BQ2415X_BOOST_MODE_STATUS:
  324. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  325. BQ2415X_BIT_OPA_MODE);
  326. case BQ2415X_BOOST_MODE_ENABLE:
  327. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  328. 1, BQ2415X_BIT_OPA_MODE);
  329. case BQ2415X_BOOST_MODE_DISABLE:
  330. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  331. 0, BQ2415X_BIT_OPA_MODE);
  332. case BQ2415X_OTG_LEVEL:
  333. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
  334. BQ2415X_BIT_OTG_PL);
  335. case BQ2415X_OTG_ACTIVATE_HIGH:
  336. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  337. 1, BQ2415X_BIT_OTG_PL);
  338. case BQ2415X_OTG_ACTIVATE_LOW:
  339. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  340. 0, BQ2415X_BIT_OTG_PL);
  341. case BQ2415X_OTG_PIN_STATUS:
  342. return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
  343. BQ2415X_BIT_OTG_EN);
  344. case BQ2415X_OTG_PIN_ENABLE:
  345. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  346. 1, BQ2415X_BIT_OTG_EN);
  347. case BQ2415X_OTG_PIN_DISABLE:
  348. return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  349. 0, BQ2415X_BIT_OTG_EN);
  350. case BQ2415X_VENDER_CODE:
  351. return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
  352. BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
  353. case BQ2415X_PART_NUMBER:
  354. return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
  355. BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
  356. case BQ2415X_REVISION:
  357. return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
  358. BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
  359. }
  360. return -EINVAL;
  361. }
  362. /* detect chip type */
  363. static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
  364. {
  365. struct i2c_client *client = to_i2c_client(bq->dev);
  366. int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
  367. if (ret < 0)
  368. return ret;
  369. switch (client->addr) {
  370. case 0x6b:
  371. switch (ret) {
  372. case 0:
  373. if (bq->chip == BQ24151A)
  374. return bq->chip;
  375. return BQ24151;
  376. case 1:
  377. if (bq->chip == BQ24150A ||
  378. bq->chip == BQ24152 ||
  379. bq->chip == BQ24155)
  380. return bq->chip;
  381. return BQ24150;
  382. case 2:
  383. if (bq->chip == BQ24153A)
  384. return bq->chip;
  385. return BQ24153;
  386. default:
  387. return BQUNKNOWN;
  388. }
  389. break;
  390. case 0x6a:
  391. switch (ret) {
  392. case 0:
  393. if (bq->chip == BQ24156A)
  394. return bq->chip;
  395. return BQ24156;
  396. case 2:
  397. if (bq->chip == BQ24157S)
  398. return bq->chip;
  399. return BQ24158;
  400. default:
  401. return BQUNKNOWN;
  402. }
  403. break;
  404. }
  405. return BQUNKNOWN;
  406. }
  407. /* detect chip revision */
  408. static int bq2415x_detect_revision(struct bq2415x_device *bq)
  409. {
  410. int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
  411. int chip = bq2415x_detect_chip(bq);
  412. if (ret < 0 || chip < 0)
  413. return -1;
  414. switch (chip) {
  415. case BQ24150:
  416. case BQ24150A:
  417. case BQ24151:
  418. case BQ24151A:
  419. case BQ24152:
  420. if (ret >= 0 && ret <= 3)
  421. return ret;
  422. return -1;
  423. case BQ24153:
  424. case BQ24153A:
  425. case BQ24156:
  426. case BQ24156A:
  427. case BQ24157S:
  428. case BQ24158:
  429. if (ret == 3)
  430. return 0;
  431. else if (ret == 1)
  432. return 1;
  433. return -1;
  434. case BQ24155:
  435. if (ret == 3)
  436. return 3;
  437. return -1;
  438. case BQUNKNOWN:
  439. return -1;
  440. }
  441. return -1;
  442. }
  443. /* return chip vender code */
  444. static int bq2415x_get_vender_code(struct bq2415x_device *bq)
  445. {
  446. int ret;
  447. ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
  448. if (ret < 0)
  449. return 0;
  450. /* convert to binary */
  451. return (ret & 0x1) +
  452. ((ret >> 1) & 0x1) * 10 +
  453. ((ret >> 2) & 0x1) * 100;
  454. }
  455. /* reset all chip registers to default state */
  456. static void bq2415x_reset_chip(struct bq2415x_device *bq)
  457. {
  458. bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
  459. bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
  460. bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
  461. bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
  462. bq->timer_error = NULL;
  463. }
  464. /**** properties functions ****/
  465. /* set current limit in mA */
  466. static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
  467. {
  468. int val;
  469. if (mA <= 100)
  470. val = 0;
  471. else if (mA <= 500)
  472. val = 1;
  473. else if (mA <= 800)
  474. val = 2;
  475. else
  476. val = 3;
  477. return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
  478. BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
  479. }
  480. /* get current limit in mA */
  481. static int bq2415x_get_current_limit(struct bq2415x_device *bq)
  482. {
  483. int ret;
  484. ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
  485. BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
  486. if (ret < 0)
  487. return ret;
  488. else if (ret == 0)
  489. return 100;
  490. else if (ret == 1)
  491. return 500;
  492. else if (ret == 2)
  493. return 800;
  494. else if (ret == 3)
  495. return 1800;
  496. return -EINVAL;
  497. }
  498. /* set weak battery voltage in mV */
  499. static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
  500. {
  501. int val;
  502. /* round to 100mV */
  503. if (mV <= 3400 + 50)
  504. val = 0;
  505. else if (mV <= 3500 + 50)
  506. val = 1;
  507. else if (mV <= 3600 + 50)
  508. val = 2;
  509. else
  510. val = 3;
  511. return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
  512. BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
  513. }
  514. /* get weak battery voltage in mV */
  515. static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
  516. {
  517. int ret;
  518. ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
  519. BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
  520. if (ret < 0)
  521. return ret;
  522. return 100 * (34 + ret);
  523. }
  524. /* set battery regulation voltage in mV */
  525. static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
  526. int mV)
  527. {
  528. int val = (mV/10 - 350) / 2;
  529. /*
  530. * According to datasheet, maximum battery regulation voltage is
  531. * 4440mV which is b101111 = 47.
  532. */
  533. if (val < 0)
  534. val = 0;
  535. else if (val > 47)
  536. return -EINVAL;
  537. return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
  538. BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
  539. }
  540. /* get battery regulation voltage in mV */
  541. static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
  542. {
  543. int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
  544. BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
  545. if (ret < 0)
  546. return ret;
  547. return 10 * (350 + 2*ret);
  548. }
  549. /* set charge current in mA (platform data must provide resistor sense) */
  550. static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
  551. {
  552. int val;
  553. if (bq->init_data.resistor_sense <= 0)
  554. return -EINVAL;
  555. val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
  556. if (val < 0)
  557. val = 0;
  558. else if (val > 7)
  559. val = 7;
  560. return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
  561. BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
  562. BQ2415X_SHIFT_VI_CHRG);
  563. }
  564. /* get charge current in mA (platform data must provide resistor sense) */
  565. static int bq2415x_get_charge_current(struct bq2415x_device *bq)
  566. {
  567. int ret;
  568. if (bq->init_data.resistor_sense <= 0)
  569. return -EINVAL;
  570. ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
  571. BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
  572. if (ret < 0)
  573. return ret;
  574. return (37400 + 6800*ret) / bq->init_data.resistor_sense;
  575. }
  576. /* set termination current in mA (platform data must provide resistor sense) */
  577. static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
  578. {
  579. int val;
  580. if (bq->init_data.resistor_sense <= 0)
  581. return -EINVAL;
  582. val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
  583. if (val < 0)
  584. val = 0;
  585. else if (val > 7)
  586. val = 7;
  587. return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
  588. BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
  589. BQ2415X_SHIFT_VI_TERM);
  590. }
  591. /* get termination current in mA (platform data must provide resistor sense) */
  592. static int bq2415x_get_termination_current(struct bq2415x_device *bq)
  593. {
  594. int ret;
  595. if (bq->init_data.resistor_sense <= 0)
  596. return -EINVAL;
  597. ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
  598. BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
  599. if (ret < 0)
  600. return ret;
  601. return (3400 + 3400*ret) / bq->init_data.resistor_sense;
  602. }
  603. /* set default value of property */
  604. #define bq2415x_set_default_value(bq, prop) \
  605. do { \
  606. int ret = 0; \
  607. if (bq->init_data.prop != -1) \
  608. ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
  609. if (ret < 0) \
  610. return ret; \
  611. } while (0)
  612. /* set default values of all properties */
  613. static int bq2415x_set_defaults(struct bq2415x_device *bq)
  614. {
  615. bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
  616. bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
  617. bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
  618. bq2415x_set_default_value(bq, current_limit);
  619. bq2415x_set_default_value(bq, weak_battery_voltage);
  620. bq2415x_set_default_value(bq, battery_regulation_voltage);
  621. if (bq->init_data.resistor_sense > 0) {
  622. bq2415x_set_default_value(bq, charge_current);
  623. bq2415x_set_default_value(bq, termination_current);
  624. bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
  625. }
  626. bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
  627. return 0;
  628. }
  629. /**** charger mode functions ****/
  630. /* set charger mode */
  631. static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
  632. {
  633. int ret = 0;
  634. int charger = 0;
  635. int boost = 0;
  636. if (mode == BQ2415X_MODE_BOOST)
  637. boost = 1;
  638. else if (mode != BQ2415X_MODE_OFF)
  639. charger = 1;
  640. if (!charger)
  641. ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
  642. if (!boost)
  643. ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
  644. if (ret < 0)
  645. return ret;
  646. switch (mode) {
  647. case BQ2415X_MODE_OFF:
  648. dev_dbg(bq->dev, "changing mode to: Offline\n");
  649. ret = bq2415x_set_current_limit(bq, 100);
  650. break;
  651. case BQ2415X_MODE_NONE:
  652. dev_dbg(bq->dev, "changing mode to: N/A\n");
  653. ret = bq2415x_set_current_limit(bq, 100);
  654. break;
  655. case BQ2415X_MODE_HOST_CHARGER:
  656. dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
  657. ret = bq2415x_set_current_limit(bq, 500);
  658. break;
  659. case BQ2415X_MODE_DEDICATED_CHARGER:
  660. dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
  661. ret = bq2415x_set_current_limit(bq, 1800);
  662. break;
  663. case BQ2415X_MODE_BOOST: /* Boost mode */
  664. dev_dbg(bq->dev, "changing mode to: Boost\n");
  665. ret = bq2415x_set_current_limit(bq, 100);
  666. break;
  667. }
  668. if (ret < 0)
  669. return ret;
  670. if (charger)
  671. ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
  672. else if (boost)
  673. ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
  674. if (ret < 0)
  675. return ret;
  676. bq2415x_set_default_value(bq, weak_battery_voltage);
  677. bq2415x_set_default_value(bq, battery_regulation_voltage);
  678. bq->mode = mode;
  679. sysfs_notify(&bq->charger->dev.kobj, NULL, "mode");
  680. return 0;
  681. }
  682. static bool bq2415x_update_reported_mode(struct bq2415x_device *bq, int mA)
  683. {
  684. enum bq2415x_mode mode;
  685. if (mA == 0)
  686. mode = BQ2415X_MODE_OFF;
  687. else if (mA < 500)
  688. mode = BQ2415X_MODE_NONE;
  689. else if (mA < 1800)
  690. mode = BQ2415X_MODE_HOST_CHARGER;
  691. else
  692. mode = BQ2415X_MODE_DEDICATED_CHARGER;
  693. if (bq->reported_mode == mode)
  694. return false;
  695. bq->reported_mode = mode;
  696. return true;
  697. }
  698. static int bq2415x_notifier_call(struct notifier_block *nb,
  699. unsigned long val, void *v)
  700. {
  701. struct bq2415x_device *bq =
  702. container_of(nb, struct bq2415x_device, nb);
  703. struct power_supply *psy = v;
  704. union power_supply_propval prop;
  705. int ret;
  706. if (val != PSY_EVENT_PROP_CHANGED)
  707. return NOTIFY_OK;
  708. /* Ignore event if it was not send by notify_node/notify_device */
  709. if (bq->notify_node) {
  710. if (!psy->dev.parent ||
  711. psy->dev.parent->of_node != bq->notify_node)
  712. return NOTIFY_OK;
  713. } else if (bq->init_data.notify_device) {
  714. if (strcmp(psy->desc->name, bq->init_data.notify_device) != 0)
  715. return NOTIFY_OK;
  716. }
  717. dev_dbg(bq->dev, "notifier call was called\n");
  718. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
  719. &prop);
  720. if (ret != 0)
  721. return NOTIFY_OK;
  722. if (!bq2415x_update_reported_mode(bq, prop.intval))
  723. return NOTIFY_OK;
  724. /* if automode is not enabled do not tell about reported_mode */
  725. if (bq->automode < 1)
  726. return NOTIFY_OK;
  727. schedule_delayed_work(&bq->work, 0);
  728. return NOTIFY_OK;
  729. }
  730. /**** timer functions ****/
  731. /* enable/disable auto resetting chip timer */
  732. static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
  733. {
  734. mutex_lock(&bq2415x_timer_mutex);
  735. if (bq->autotimer == state) {
  736. mutex_unlock(&bq2415x_timer_mutex);
  737. return;
  738. }
  739. bq->autotimer = state;
  740. if (state) {
  741. schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
  742. bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
  743. bq->timer_error = NULL;
  744. } else {
  745. cancel_delayed_work_sync(&bq->work);
  746. }
  747. mutex_unlock(&bq2415x_timer_mutex);
  748. }
  749. /* called by bq2415x_timer_work on timer error */
  750. static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
  751. {
  752. bq->timer_error = msg;
  753. sysfs_notify(&bq->charger->dev.kobj, NULL, "timer");
  754. dev_err(bq->dev, "%s\n", msg);
  755. if (bq->automode > 0)
  756. bq->automode = 0;
  757. bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
  758. bq2415x_set_autotimer(bq, 0);
  759. }
  760. /* delayed work function for auto resetting chip timer */
  761. static void bq2415x_timer_work(struct work_struct *work)
  762. {
  763. struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
  764. work.work);
  765. int ret;
  766. int error;
  767. int boost;
  768. if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
  769. sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode");
  770. bq2415x_set_mode(bq, bq->reported_mode);
  771. }
  772. if (!bq->autotimer)
  773. return;
  774. ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
  775. if (ret < 0) {
  776. bq2415x_timer_error(bq, "Resetting timer failed");
  777. return;
  778. }
  779. boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
  780. if (boost < 0) {
  781. bq2415x_timer_error(bq, "Unknown error");
  782. return;
  783. }
  784. error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
  785. if (error < 0) {
  786. bq2415x_timer_error(bq, "Unknown error");
  787. return;
  788. }
  789. if (boost) {
  790. switch (error) {
  791. /* Non fatal errors, chip is OK */
  792. case 0: /* No error */
  793. break;
  794. case 6: /* Timer expired */
  795. dev_err(bq->dev, "Timer expired\n");
  796. break;
  797. case 3: /* Battery voltage too low */
  798. dev_err(bq->dev, "Battery voltage to low\n");
  799. break;
  800. /* Fatal errors, disable and reset chip */
  801. case 1: /* Overvoltage protection (chip fried) */
  802. bq2415x_timer_error(bq,
  803. "Overvoltage protection (chip fried)");
  804. return;
  805. case 2: /* Overload */
  806. bq2415x_timer_error(bq, "Overload");
  807. return;
  808. case 4: /* Battery overvoltage protection */
  809. bq2415x_timer_error(bq,
  810. "Battery overvoltage protection");
  811. return;
  812. case 5: /* Thermal shutdown (too hot) */
  813. bq2415x_timer_error(bq,
  814. "Thermal shutdown (too hot)");
  815. return;
  816. case 7: /* N/A */
  817. bq2415x_timer_error(bq, "Unknown error");
  818. return;
  819. }
  820. } else {
  821. switch (error) {
  822. /* Non fatal errors, chip is OK */
  823. case 0: /* No error */
  824. break;
  825. case 2: /* Sleep mode */
  826. dev_err(bq->dev, "Sleep mode\n");
  827. break;
  828. case 3: /* Poor input source */
  829. dev_err(bq->dev, "Poor input source\n");
  830. break;
  831. case 6: /* Timer expired */
  832. dev_err(bq->dev, "Timer expired\n");
  833. break;
  834. case 7: /* No battery */
  835. dev_err(bq->dev, "No battery\n");
  836. break;
  837. /* Fatal errors, disable and reset chip */
  838. case 1: /* Overvoltage protection (chip fried) */
  839. bq2415x_timer_error(bq,
  840. "Overvoltage protection (chip fried)");
  841. return;
  842. case 4: /* Battery overvoltage protection */
  843. bq2415x_timer_error(bq,
  844. "Battery overvoltage protection");
  845. return;
  846. case 5: /* Thermal shutdown (too hot) */
  847. bq2415x_timer_error(bq,
  848. "Thermal shutdown (too hot)");
  849. return;
  850. }
  851. }
  852. schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
  853. }
  854. /**** power supply interface code ****/
  855. static enum power_supply_property bq2415x_power_supply_props[] = {
  856. /* TODO: maybe add more power supply properties */
  857. POWER_SUPPLY_PROP_STATUS,
  858. POWER_SUPPLY_PROP_MODEL_NAME,
  859. };
  860. static int bq2415x_power_supply_get_property(struct power_supply *psy,
  861. enum power_supply_property psp,
  862. union power_supply_propval *val)
  863. {
  864. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  865. int ret;
  866. switch (psp) {
  867. case POWER_SUPPLY_PROP_STATUS:
  868. ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
  869. if (ret < 0)
  870. return ret;
  871. else if (ret == 0) /* Ready */
  872. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  873. else if (ret == 1) /* Charge in progress */
  874. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  875. else if (ret == 2) /* Charge done */
  876. val->intval = POWER_SUPPLY_STATUS_FULL;
  877. else
  878. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  879. break;
  880. case POWER_SUPPLY_PROP_MODEL_NAME:
  881. val->strval = bq->model;
  882. break;
  883. default:
  884. return -EINVAL;
  885. }
  886. return 0;
  887. }
  888. static int bq2415x_power_supply_init(struct bq2415x_device *bq)
  889. {
  890. int ret;
  891. int chip;
  892. char revstr[8];
  893. struct power_supply_config psy_cfg = { .drv_data = bq, };
  894. bq->charger_desc.name = bq->name;
  895. bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
  896. bq->charger_desc.properties = bq2415x_power_supply_props;
  897. bq->charger_desc.num_properties =
  898. ARRAY_SIZE(bq2415x_power_supply_props);
  899. bq->charger_desc.get_property = bq2415x_power_supply_get_property;
  900. ret = bq2415x_detect_chip(bq);
  901. if (ret < 0)
  902. chip = BQUNKNOWN;
  903. else
  904. chip = ret;
  905. ret = bq2415x_detect_revision(bq);
  906. if (ret < 0)
  907. strcpy(revstr, "unknown");
  908. else
  909. sprintf(revstr, "1.%d", ret);
  910. bq->model = kasprintf(GFP_KERNEL,
  911. "chip %s, revision %s, vender code %.3d",
  912. bq2415x_chip_name[chip], revstr,
  913. bq2415x_get_vender_code(bq));
  914. if (!bq->model) {
  915. dev_err(bq->dev, "failed to allocate model name\n");
  916. return -ENOMEM;
  917. }
  918. bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
  919. &psy_cfg);
  920. if (IS_ERR(bq->charger)) {
  921. kfree(bq->model);
  922. return PTR_ERR(bq->charger);
  923. }
  924. return 0;
  925. }
  926. static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
  927. {
  928. bq->autotimer = 0;
  929. if (bq->automode > 0)
  930. bq->automode = 0;
  931. cancel_delayed_work_sync(&bq->work);
  932. power_supply_unregister(bq->charger);
  933. kfree(bq->model);
  934. }
  935. /**** additional sysfs entries for power supply interface ****/
  936. /* show *_status entries */
  937. static ssize_t bq2415x_sysfs_show_status(struct device *dev,
  938. struct device_attribute *attr,
  939. char *buf)
  940. {
  941. struct power_supply *psy = dev_get_drvdata(dev);
  942. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  943. enum bq2415x_command command;
  944. int ret;
  945. if (strcmp(attr->attr.name, "otg_status") == 0)
  946. command = BQ2415X_OTG_STATUS;
  947. else if (strcmp(attr->attr.name, "charge_status") == 0)
  948. command = BQ2415X_CHARGE_STATUS;
  949. else if (strcmp(attr->attr.name, "boost_status") == 0)
  950. command = BQ2415X_BOOST_STATUS;
  951. else if (strcmp(attr->attr.name, "fault_status") == 0)
  952. command = BQ2415X_FAULT_STATUS;
  953. else
  954. return -EINVAL;
  955. ret = bq2415x_exec_command(bq, command);
  956. if (ret < 0)
  957. return ret;
  958. return sprintf(buf, "%d\n", ret);
  959. }
  960. /*
  961. * set timer entry:
  962. * auto - enable auto mode
  963. * off - disable auto mode
  964. * (other values) - reset chip timer
  965. */
  966. static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
  967. struct device_attribute *attr,
  968. const char *buf,
  969. size_t count)
  970. {
  971. struct power_supply *psy = dev_get_drvdata(dev);
  972. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  973. int ret = 0;
  974. if (strncmp(buf, "auto", 4) == 0)
  975. bq2415x_set_autotimer(bq, 1);
  976. else if (strncmp(buf, "off", 3) == 0)
  977. bq2415x_set_autotimer(bq, 0);
  978. else
  979. ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
  980. if (ret < 0)
  981. return ret;
  982. return count;
  983. }
  984. /* show timer entry (auto or off) */
  985. static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
  986. struct device_attribute *attr,
  987. char *buf)
  988. {
  989. struct power_supply *psy = dev_get_drvdata(dev);
  990. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  991. if (bq->timer_error)
  992. return sprintf(buf, "%s\n", bq->timer_error);
  993. if (bq->autotimer)
  994. return sprintf(buf, "auto\n");
  995. return sprintf(buf, "off\n");
  996. }
  997. /*
  998. * set mode entry:
  999. * auto - if automode is supported, enable it and set mode to reported
  1000. * none - disable charger and boost mode
  1001. * host - charging mode for host/hub chargers (current limit 500mA)
  1002. * dedicated - charging mode for dedicated chargers (unlimited current limit)
  1003. * boost - disable charger and enable boost mode
  1004. */
  1005. static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
  1006. struct device_attribute *attr,
  1007. const char *buf,
  1008. size_t count)
  1009. {
  1010. struct power_supply *psy = dev_get_drvdata(dev);
  1011. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1012. enum bq2415x_mode mode;
  1013. int ret = 0;
  1014. if (strncmp(buf, "auto", 4) == 0) {
  1015. if (bq->automode < 0)
  1016. return -EINVAL;
  1017. bq->automode = 1;
  1018. mode = bq->reported_mode;
  1019. } else if (strncmp(buf, "off", 3) == 0) {
  1020. if (bq->automode > 0)
  1021. bq->automode = 0;
  1022. mode = BQ2415X_MODE_OFF;
  1023. } else if (strncmp(buf, "none", 4) == 0) {
  1024. if (bq->automode > 0)
  1025. bq->automode = 0;
  1026. mode = BQ2415X_MODE_NONE;
  1027. } else if (strncmp(buf, "host", 4) == 0) {
  1028. if (bq->automode > 0)
  1029. bq->automode = 0;
  1030. mode = BQ2415X_MODE_HOST_CHARGER;
  1031. } else if (strncmp(buf, "dedicated", 9) == 0) {
  1032. if (bq->automode > 0)
  1033. bq->automode = 0;
  1034. mode = BQ2415X_MODE_DEDICATED_CHARGER;
  1035. } else if (strncmp(buf, "boost", 5) == 0) {
  1036. if (bq->automode > 0)
  1037. bq->automode = 0;
  1038. mode = BQ2415X_MODE_BOOST;
  1039. } else if (strncmp(buf, "reset", 5) == 0) {
  1040. bq2415x_reset_chip(bq);
  1041. bq2415x_set_defaults(bq);
  1042. if (bq->automode <= 0)
  1043. return count;
  1044. bq->automode = 1;
  1045. mode = bq->reported_mode;
  1046. } else {
  1047. return -EINVAL;
  1048. }
  1049. ret = bq2415x_set_mode(bq, mode);
  1050. if (ret < 0)
  1051. return ret;
  1052. return count;
  1053. }
  1054. /* show mode entry (auto, none, host, dedicated or boost) */
  1055. static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
  1056. struct device_attribute *attr,
  1057. char *buf)
  1058. {
  1059. struct power_supply *psy = dev_get_drvdata(dev);
  1060. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1061. ssize_t ret = 0;
  1062. if (bq->automode > 0)
  1063. ret += sprintf(buf+ret, "auto (");
  1064. switch (bq->mode) {
  1065. case BQ2415X_MODE_OFF:
  1066. ret += sprintf(buf+ret, "off");
  1067. break;
  1068. case BQ2415X_MODE_NONE:
  1069. ret += sprintf(buf+ret, "none");
  1070. break;
  1071. case BQ2415X_MODE_HOST_CHARGER:
  1072. ret += sprintf(buf+ret, "host");
  1073. break;
  1074. case BQ2415X_MODE_DEDICATED_CHARGER:
  1075. ret += sprintf(buf+ret, "dedicated");
  1076. break;
  1077. case BQ2415X_MODE_BOOST:
  1078. ret += sprintf(buf+ret, "boost");
  1079. break;
  1080. }
  1081. if (bq->automode > 0)
  1082. ret += sprintf(buf+ret, ")");
  1083. ret += sprintf(buf+ret, "\n");
  1084. return ret;
  1085. }
  1086. /* show reported_mode entry (none, host, dedicated or boost) */
  1087. static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
  1088. struct device_attribute *attr,
  1089. char *buf)
  1090. {
  1091. struct power_supply *psy = dev_get_drvdata(dev);
  1092. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1093. if (bq->automode < 0)
  1094. return -EINVAL;
  1095. switch (bq->reported_mode) {
  1096. case BQ2415X_MODE_OFF:
  1097. return sprintf(buf, "off\n");
  1098. case BQ2415X_MODE_NONE:
  1099. return sprintf(buf, "none\n");
  1100. case BQ2415X_MODE_HOST_CHARGER:
  1101. return sprintf(buf, "host\n");
  1102. case BQ2415X_MODE_DEDICATED_CHARGER:
  1103. return sprintf(buf, "dedicated\n");
  1104. case BQ2415X_MODE_BOOST:
  1105. return sprintf(buf, "boost\n");
  1106. }
  1107. return -EINVAL;
  1108. }
  1109. /* directly set raw value to chip register, format: 'register value' */
  1110. static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
  1111. struct device_attribute *attr,
  1112. const char *buf,
  1113. size_t count)
  1114. {
  1115. struct power_supply *psy = dev_get_drvdata(dev);
  1116. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1117. ssize_t ret = 0;
  1118. unsigned int reg;
  1119. unsigned int val;
  1120. if (sscanf(buf, "%x %x", &reg, &val) != 2)
  1121. return -EINVAL;
  1122. if (reg > 4 || val > 255)
  1123. return -EINVAL;
  1124. ret = bq2415x_i2c_write(bq, reg, val);
  1125. if (ret < 0)
  1126. return ret;
  1127. return count;
  1128. }
  1129. /* print value of chip register, format: 'register=value' */
  1130. static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
  1131. u8 reg,
  1132. char *buf)
  1133. {
  1134. int ret = bq2415x_i2c_read(bq, reg);
  1135. if (ret < 0)
  1136. return sprintf(buf, "%#.2x=error %d\n", reg, ret);
  1137. return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
  1138. }
  1139. /* show all raw values of chip register, format per line: 'register=value' */
  1140. static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
  1141. struct device_attribute *attr,
  1142. char *buf)
  1143. {
  1144. struct power_supply *psy = dev_get_drvdata(dev);
  1145. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1146. ssize_t ret = 0;
  1147. ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
  1148. ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
  1149. ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
  1150. ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
  1151. ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
  1152. return ret;
  1153. }
  1154. /* set current and voltage limit entries (in mA or mV) */
  1155. static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
  1156. struct device_attribute *attr,
  1157. const char *buf,
  1158. size_t count)
  1159. {
  1160. struct power_supply *psy = dev_get_drvdata(dev);
  1161. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1162. long val;
  1163. int ret;
  1164. if (kstrtol(buf, 10, &val) < 0)
  1165. return -EINVAL;
  1166. if (strcmp(attr->attr.name, "current_limit") == 0)
  1167. ret = bq2415x_set_current_limit(bq, val);
  1168. else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
  1169. ret = bq2415x_set_weak_battery_voltage(bq, val);
  1170. else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
  1171. ret = bq2415x_set_battery_regulation_voltage(bq, val);
  1172. else if (strcmp(attr->attr.name, "charge_current") == 0)
  1173. ret = bq2415x_set_charge_current(bq, val);
  1174. else if (strcmp(attr->attr.name, "termination_current") == 0)
  1175. ret = bq2415x_set_termination_current(bq, val);
  1176. else
  1177. return -EINVAL;
  1178. if (ret < 0)
  1179. return ret;
  1180. return count;
  1181. }
  1182. /* show current and voltage limit entries (in mA or mV) */
  1183. static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
  1184. struct device_attribute *attr,
  1185. char *buf)
  1186. {
  1187. struct power_supply *psy = dev_get_drvdata(dev);
  1188. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1189. int ret;
  1190. if (strcmp(attr->attr.name, "current_limit") == 0)
  1191. ret = bq2415x_get_current_limit(bq);
  1192. else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
  1193. ret = bq2415x_get_weak_battery_voltage(bq);
  1194. else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
  1195. ret = bq2415x_get_battery_regulation_voltage(bq);
  1196. else if (strcmp(attr->attr.name, "charge_current") == 0)
  1197. ret = bq2415x_get_charge_current(bq);
  1198. else if (strcmp(attr->attr.name, "termination_current") == 0)
  1199. ret = bq2415x_get_termination_current(bq);
  1200. else
  1201. return -EINVAL;
  1202. if (ret < 0)
  1203. return ret;
  1204. return sprintf(buf, "%d\n", ret);
  1205. }
  1206. /* set *_enable entries */
  1207. static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
  1208. struct device_attribute *attr,
  1209. const char *buf,
  1210. size_t count)
  1211. {
  1212. struct power_supply *psy = dev_get_drvdata(dev);
  1213. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1214. enum bq2415x_command command;
  1215. long val;
  1216. int ret;
  1217. if (kstrtol(buf, 10, &val) < 0)
  1218. return -EINVAL;
  1219. if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
  1220. command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
  1221. BQ2415X_CHARGE_TERMINATION_DISABLE;
  1222. else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
  1223. command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
  1224. BQ2415X_HIGH_IMPEDANCE_DISABLE;
  1225. else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
  1226. command = val ? BQ2415X_OTG_PIN_ENABLE :
  1227. BQ2415X_OTG_PIN_DISABLE;
  1228. else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
  1229. command = val ? BQ2415X_STAT_PIN_ENABLE :
  1230. BQ2415X_STAT_PIN_DISABLE;
  1231. else
  1232. return -EINVAL;
  1233. ret = bq2415x_exec_command(bq, command);
  1234. if (ret < 0)
  1235. return ret;
  1236. return count;
  1237. }
  1238. /* show *_enable entries */
  1239. static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
  1240. struct device_attribute *attr,
  1241. char *buf)
  1242. {
  1243. struct power_supply *psy = dev_get_drvdata(dev);
  1244. struct bq2415x_device *bq = power_supply_get_drvdata(psy);
  1245. enum bq2415x_command command;
  1246. int ret;
  1247. if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
  1248. command = BQ2415X_CHARGE_TERMINATION_STATUS;
  1249. else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
  1250. command = BQ2415X_HIGH_IMPEDANCE_STATUS;
  1251. else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
  1252. command = BQ2415X_OTG_PIN_STATUS;
  1253. else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
  1254. command = BQ2415X_STAT_PIN_STATUS;
  1255. else
  1256. return -EINVAL;
  1257. ret = bq2415x_exec_command(bq, command);
  1258. if (ret < 0)
  1259. return ret;
  1260. return sprintf(buf, "%d\n", ret);
  1261. }
  1262. static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
  1263. bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  1264. static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
  1265. bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  1266. static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
  1267. bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  1268. static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
  1269. bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  1270. static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
  1271. bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  1272. static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
  1273. bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  1274. static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
  1275. bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  1276. static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
  1277. bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  1278. static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
  1279. bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  1280. static DEVICE_ATTR(reported_mode, S_IRUGO,
  1281. bq2415x_sysfs_show_reported_mode, NULL);
  1282. static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
  1283. bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
  1284. static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
  1285. bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
  1286. static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
  1287. bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
  1288. static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  1289. static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  1290. static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  1291. static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  1292. static struct attribute *bq2415x_sysfs_attributes[] = {
  1293. /*
  1294. * TODO: some (appropriate) of these attrs should be switched to
  1295. * use power supply class props.
  1296. */
  1297. &dev_attr_current_limit.attr,
  1298. &dev_attr_weak_battery_voltage.attr,
  1299. &dev_attr_battery_regulation_voltage.attr,
  1300. &dev_attr_charge_current.attr,
  1301. &dev_attr_termination_current.attr,
  1302. &dev_attr_charge_termination_enable.attr,
  1303. &dev_attr_high_impedance_enable.attr,
  1304. &dev_attr_otg_pin_enable.attr,
  1305. &dev_attr_stat_pin_enable.attr,
  1306. &dev_attr_reported_mode.attr,
  1307. &dev_attr_mode.attr,
  1308. &dev_attr_timer.attr,
  1309. &dev_attr_registers.attr,
  1310. &dev_attr_otg_status.attr,
  1311. &dev_attr_charge_status.attr,
  1312. &dev_attr_boost_status.attr,
  1313. &dev_attr_fault_status.attr,
  1314. NULL,
  1315. };
  1316. static const struct attribute_group bq2415x_sysfs_attr_group = {
  1317. .attrs = bq2415x_sysfs_attributes,
  1318. };
  1319. static int bq2415x_sysfs_init(struct bq2415x_device *bq)
  1320. {
  1321. return sysfs_create_group(&bq->charger->dev.kobj,
  1322. &bq2415x_sysfs_attr_group);
  1323. }
  1324. static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
  1325. {
  1326. sysfs_remove_group(&bq->charger->dev.kobj, &bq2415x_sysfs_attr_group);
  1327. }
  1328. /* main bq2415x probe function */
  1329. static int bq2415x_probe(struct i2c_client *client,
  1330. const struct i2c_device_id *id)
  1331. {
  1332. int ret;
  1333. int num;
  1334. char *name = NULL;
  1335. struct bq2415x_device *bq;
  1336. struct device_node *np = client->dev.of_node;
  1337. struct bq2415x_platform_data *pdata = client->dev.platform_data;
  1338. const struct acpi_device_id *acpi_id = NULL;
  1339. struct power_supply *notify_psy = NULL;
  1340. union power_supply_propval prop;
  1341. if (!np && !pdata && !ACPI_HANDLE(&client->dev)) {
  1342. dev_err(&client->dev, "Neither devicetree, nor platform data, nor ACPI support\n");
  1343. return -ENODEV;
  1344. }
  1345. /* Get new ID for the new device */
  1346. mutex_lock(&bq2415x_id_mutex);
  1347. num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
  1348. mutex_unlock(&bq2415x_id_mutex);
  1349. if (num < 0)
  1350. return num;
  1351. if (id) {
  1352. name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  1353. } else if (ACPI_HANDLE(&client->dev)) {
  1354. acpi_id =
  1355. acpi_match_device(client->dev.driver->acpi_match_table,
  1356. &client->dev);
  1357. if (!acpi_id) {
  1358. dev_err(&client->dev, "failed to match device name\n");
  1359. ret = -ENODEV;
  1360. goto error_1;
  1361. }
  1362. name = kasprintf(GFP_KERNEL, "%s-%d", acpi_id->id, num);
  1363. }
  1364. if (!name) {
  1365. dev_err(&client->dev, "failed to allocate device name\n");
  1366. ret = -ENOMEM;
  1367. goto error_1;
  1368. }
  1369. bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
  1370. if (!bq) {
  1371. ret = -ENOMEM;
  1372. goto error_2;
  1373. }
  1374. i2c_set_clientdata(client, bq);
  1375. bq->id = num;
  1376. bq->dev = &client->dev;
  1377. if (id)
  1378. bq->chip = id->driver_data;
  1379. else if (ACPI_HANDLE(bq->dev))
  1380. bq->chip = acpi_id->driver_data;
  1381. bq->name = name;
  1382. bq->mode = BQ2415X_MODE_OFF;
  1383. bq->reported_mode = BQ2415X_MODE_OFF;
  1384. bq->autotimer = 0;
  1385. bq->automode = 0;
  1386. if (np || ACPI_HANDLE(bq->dev)) {
  1387. ret = device_property_read_u32(bq->dev,
  1388. "ti,current-limit",
  1389. &bq->init_data.current_limit);
  1390. if (ret)
  1391. goto error_2;
  1392. ret = device_property_read_u32(bq->dev,
  1393. "ti,weak-battery-voltage",
  1394. &bq->init_data.weak_battery_voltage);
  1395. if (ret)
  1396. goto error_2;
  1397. ret = device_property_read_u32(bq->dev,
  1398. "ti,battery-regulation-voltage",
  1399. &bq->init_data.battery_regulation_voltage);
  1400. if (ret)
  1401. goto error_2;
  1402. ret = device_property_read_u32(bq->dev,
  1403. "ti,charge-current",
  1404. &bq->init_data.charge_current);
  1405. if (ret)
  1406. goto error_2;
  1407. ret = device_property_read_u32(bq->dev,
  1408. "ti,termination-current",
  1409. &bq->init_data.termination_current);
  1410. if (ret)
  1411. goto error_2;
  1412. ret = device_property_read_u32(bq->dev,
  1413. "ti,resistor-sense",
  1414. &bq->init_data.resistor_sense);
  1415. if (ret)
  1416. goto error_2;
  1417. if (np)
  1418. bq->notify_node = of_parse_phandle(np,
  1419. "ti,usb-charger-detection", 0);
  1420. } else {
  1421. memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
  1422. }
  1423. bq2415x_reset_chip(bq);
  1424. ret = bq2415x_power_supply_init(bq);
  1425. if (ret) {
  1426. dev_err(bq->dev, "failed to register power supply: %d\n", ret);
  1427. goto error_2;
  1428. }
  1429. ret = bq2415x_sysfs_init(bq);
  1430. if (ret) {
  1431. dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
  1432. goto error_3;
  1433. }
  1434. ret = bq2415x_set_defaults(bq);
  1435. if (ret) {
  1436. dev_err(bq->dev, "failed to set default values: %d\n", ret);
  1437. goto error_4;
  1438. }
  1439. if (bq->notify_node || bq->init_data.notify_device) {
  1440. bq->nb.notifier_call = bq2415x_notifier_call;
  1441. ret = power_supply_reg_notifier(&bq->nb);
  1442. if (ret) {
  1443. dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
  1444. goto error_4;
  1445. }
  1446. bq->automode = 1;
  1447. dev_info(bq->dev, "automode supported, waiting for events\n");
  1448. } else {
  1449. bq->automode = -1;
  1450. dev_info(bq->dev, "automode not supported\n");
  1451. }
  1452. /* Query for initial reported_mode and set it */
  1453. if (bq->nb.notifier_call) {
  1454. if (np) {
  1455. notify_psy = power_supply_get_by_phandle(np,
  1456. "ti,usb-charger-detection");
  1457. if (IS_ERR(notify_psy))
  1458. notify_psy = NULL;
  1459. } else if (bq->init_data.notify_device) {
  1460. notify_psy = power_supply_get_by_name(
  1461. bq->init_data.notify_device);
  1462. }
  1463. }
  1464. if (notify_psy) {
  1465. ret = power_supply_get_property(notify_psy,
  1466. POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
  1467. power_supply_put(notify_psy);
  1468. if (ret == 0) {
  1469. bq2415x_update_reported_mode(bq, prop.intval);
  1470. bq2415x_set_mode(bq, bq->reported_mode);
  1471. }
  1472. }
  1473. INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
  1474. bq2415x_set_autotimer(bq, 1);
  1475. dev_info(bq->dev, "driver registered\n");
  1476. return 0;
  1477. error_4:
  1478. bq2415x_sysfs_exit(bq);
  1479. error_3:
  1480. bq2415x_power_supply_exit(bq);
  1481. error_2:
  1482. if (bq)
  1483. of_node_put(bq->notify_node);
  1484. kfree(name);
  1485. error_1:
  1486. mutex_lock(&bq2415x_id_mutex);
  1487. idr_remove(&bq2415x_id, num);
  1488. mutex_unlock(&bq2415x_id_mutex);
  1489. return ret;
  1490. }
  1491. /* main bq2415x remove function */
  1492. static int bq2415x_remove(struct i2c_client *client)
  1493. {
  1494. struct bq2415x_device *bq = i2c_get_clientdata(client);
  1495. if (bq->nb.notifier_call)
  1496. power_supply_unreg_notifier(&bq->nb);
  1497. of_node_put(bq->notify_node);
  1498. bq2415x_sysfs_exit(bq);
  1499. bq2415x_power_supply_exit(bq);
  1500. bq2415x_reset_chip(bq);
  1501. mutex_lock(&bq2415x_id_mutex);
  1502. idr_remove(&bq2415x_id, bq->id);
  1503. mutex_unlock(&bq2415x_id_mutex);
  1504. dev_info(bq->dev, "driver unregistered\n");
  1505. kfree(bq->name);
  1506. return 0;
  1507. }
  1508. static const struct i2c_device_id bq2415x_i2c_id_table[] = {
  1509. { "bq2415x", BQUNKNOWN },
  1510. { "bq24150", BQ24150 },
  1511. { "bq24150a", BQ24150A },
  1512. { "bq24151", BQ24151 },
  1513. { "bq24151a", BQ24151A },
  1514. { "bq24152", BQ24152 },
  1515. { "bq24153", BQ24153 },
  1516. { "bq24153a", BQ24153A },
  1517. { "bq24155", BQ24155 },
  1518. { "bq24156", BQ24156 },
  1519. { "bq24156a", BQ24156A },
  1520. { "bq24157s", BQ24157S },
  1521. { "bq24158", BQ24158 },
  1522. {},
  1523. };
  1524. MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
  1525. #ifdef CONFIG_ACPI
  1526. static const struct acpi_device_id bq2415x_i2c_acpi_match[] = {
  1527. { "BQ2415X", BQUNKNOWN },
  1528. { "BQ241500", BQ24150 },
  1529. { "BQA24150", BQ24150A },
  1530. { "BQ241510", BQ24151 },
  1531. { "BQA24151", BQ24151A },
  1532. { "BQ241520", BQ24152 },
  1533. { "BQ241530", BQ24153 },
  1534. { "BQA24153", BQ24153A },
  1535. { "BQ241550", BQ24155 },
  1536. { "BQ241560", BQ24156 },
  1537. { "BQA24156", BQ24156A },
  1538. { "BQS24157", BQ24157S },
  1539. { "BQ241580", BQ24158 },
  1540. {},
  1541. };
  1542. MODULE_DEVICE_TABLE(acpi, bq2415x_i2c_acpi_match);
  1543. #endif
  1544. #ifdef CONFIG_OF
  1545. static const struct of_device_id bq2415x_of_match_table[] = {
  1546. { .compatible = "ti,bq24150" },
  1547. { .compatible = "ti,bq24150a" },
  1548. { .compatible = "ti,bq24151" },
  1549. { .compatible = "ti,bq24151a" },
  1550. { .compatible = "ti,bq24152" },
  1551. { .compatible = "ti,bq24153" },
  1552. { .compatible = "ti,bq24153a" },
  1553. { .compatible = "ti,bq24155" },
  1554. { .compatible = "ti,bq24156" },
  1555. { .compatible = "ti,bq24156a" },
  1556. { .compatible = "ti,bq24157s" },
  1557. { .compatible = "ti,bq24158" },
  1558. {},
  1559. };
  1560. MODULE_DEVICE_TABLE(of, bq2415x_of_match_table);
  1561. #endif
  1562. static struct i2c_driver bq2415x_driver = {
  1563. .driver = {
  1564. .name = "bq2415x-charger",
  1565. .of_match_table = of_match_ptr(bq2415x_of_match_table),
  1566. .acpi_match_table = ACPI_PTR(bq2415x_i2c_acpi_match),
  1567. },
  1568. .probe = bq2415x_probe,
  1569. .remove = bq2415x_remove,
  1570. .id_table = bq2415x_i2c_id_table,
  1571. };
  1572. module_i2c_driver(bq2415x_driver);
  1573. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  1574. MODULE_DESCRIPTION("bq2415x charger driver");
  1575. MODULE_LICENSE("GPL");