rt9455_charger.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. /*
  2. * Driver for Richtek RT9455WSC battery charger.
  3. *
  4. * Copyright (C) 2015 Intel Corporation
  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. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/delay.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/i2c.h>
  24. #include <linux/acpi.h>
  25. #include <linux/usb/phy.h>
  26. #include <linux/regmap.h>
  27. #define RT9455_MANUFACTURER "Richtek"
  28. #define RT9455_MODEL_NAME "RT9455"
  29. #define RT9455_DRIVER_NAME "rt9455-charger"
  30. #define RT9455_IRQ_NAME "interrupt"
  31. #define RT9455_PWR_RDY_DELAY 1 /* 1 second */
  32. #define RT9455_MAX_CHARGING_TIME 21600 /* 6 hrs */
  33. #define RT9455_BATT_PRESENCE_DELAY 60 /* 60 seconds */
  34. #define RT9455_CHARGE_MODE 0x00
  35. #define RT9455_BOOST_MODE 0x01
  36. #define RT9455_FAULT 0x03
  37. #define RT9455_IAICR_100MA 0x00
  38. #define RT9455_IAICR_500MA 0x01
  39. #define RT9455_IAICR_NO_LIMIT 0x03
  40. #define RT9455_CHARGE_DISABLE 0x00
  41. #define RT9455_CHARGE_ENABLE 0x01
  42. #define RT9455_PWR_FAULT 0x00
  43. #define RT9455_PWR_GOOD 0x01
  44. #define RT9455_REG_CTRL1 0x00 /* CTRL1 reg address */
  45. #define RT9455_REG_CTRL2 0x01 /* CTRL2 reg address */
  46. #define RT9455_REG_CTRL3 0x02 /* CTRL3 reg address */
  47. #define RT9455_REG_DEV_ID 0x03 /* DEV_ID reg address */
  48. #define RT9455_REG_CTRL4 0x04 /* CTRL4 reg address */
  49. #define RT9455_REG_CTRL5 0x05 /* CTRL5 reg address */
  50. #define RT9455_REG_CTRL6 0x06 /* CTRL6 reg address */
  51. #define RT9455_REG_CTRL7 0x07 /* CTRL7 reg address */
  52. #define RT9455_REG_IRQ1 0x08 /* IRQ1 reg address */
  53. #define RT9455_REG_IRQ2 0x09 /* IRQ2 reg address */
  54. #define RT9455_REG_IRQ3 0x0A /* IRQ3 reg address */
  55. #define RT9455_REG_MASK1 0x0B /* MASK1 reg address */
  56. #define RT9455_REG_MASK2 0x0C /* MASK2 reg address */
  57. #define RT9455_REG_MASK3 0x0D /* MASK3 reg address */
  58. enum rt9455_fields {
  59. F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */
  60. F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ,
  61. F_OPA_MODE, /* CTRL2 reg fields */
  62. F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */
  63. F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */
  64. F_RST, /* CTRL4 reg fields */
  65. F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/
  66. F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */
  67. F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */
  68. F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */
  69. F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI,
  70. F_CHMIVRI, /* IRQ2 reg fields */
  71. F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */
  72. F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */
  73. F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM,
  74. F_CHMIVRIM, /* MASK2 reg fields */
  75. F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */
  76. F_MAX_FIELDS
  77. };
  78. static const struct reg_field rt9455_reg_fields[] = {
  79. [F_STAT] = REG_FIELD(RT9455_REG_CTRL1, 4, 5),
  80. [F_BOOST] = REG_FIELD(RT9455_REG_CTRL1, 3, 3),
  81. [F_PWR_RDY] = REG_FIELD(RT9455_REG_CTRL1, 2, 2),
  82. [F_OTG_PIN_POLARITY] = REG_FIELD(RT9455_REG_CTRL1, 1, 1),
  83. [F_IAICR] = REG_FIELD(RT9455_REG_CTRL2, 6, 7),
  84. [F_TE_SHDN_EN] = REG_FIELD(RT9455_REG_CTRL2, 5, 5),
  85. [F_HIGHER_OCP] = REG_FIELD(RT9455_REG_CTRL2, 4, 4),
  86. [F_TE] = REG_FIELD(RT9455_REG_CTRL2, 3, 3),
  87. [F_IAICR_INT] = REG_FIELD(RT9455_REG_CTRL2, 2, 2),
  88. [F_HIZ] = REG_FIELD(RT9455_REG_CTRL2, 1, 1),
  89. [F_OPA_MODE] = REG_FIELD(RT9455_REG_CTRL2, 0, 0),
  90. [F_VOREG] = REG_FIELD(RT9455_REG_CTRL3, 2, 7),
  91. [F_OTG_PL] = REG_FIELD(RT9455_REG_CTRL3, 1, 1),
  92. [F_OTG_EN] = REG_FIELD(RT9455_REG_CTRL3, 0, 0),
  93. [F_VENDOR_ID] = REG_FIELD(RT9455_REG_DEV_ID, 4, 7),
  94. [F_CHIP_REV] = REG_FIELD(RT9455_REG_DEV_ID, 0, 3),
  95. [F_RST] = REG_FIELD(RT9455_REG_CTRL4, 7, 7),
  96. [F_TMR_EN] = REG_FIELD(RT9455_REG_CTRL5, 7, 7),
  97. [F_MIVR] = REG_FIELD(RT9455_REG_CTRL5, 4, 5),
  98. [F_IPREC] = REG_FIELD(RT9455_REG_CTRL5, 2, 3),
  99. [F_IEOC_PERCENTAGE] = REG_FIELD(RT9455_REG_CTRL5, 0, 1),
  100. [F_IAICR_SEL] = REG_FIELD(RT9455_REG_CTRL6, 7, 7),
  101. [F_ICHRG] = REG_FIELD(RT9455_REG_CTRL6, 4, 6),
  102. [F_VPREC] = REG_FIELD(RT9455_REG_CTRL6, 0, 2),
  103. [F_BATD_EN] = REG_FIELD(RT9455_REG_CTRL7, 6, 6),
  104. [F_CHG_EN] = REG_FIELD(RT9455_REG_CTRL7, 4, 4),
  105. [F_VMREG] = REG_FIELD(RT9455_REG_CTRL7, 0, 3),
  106. [F_TSDI] = REG_FIELD(RT9455_REG_IRQ1, 7, 7),
  107. [F_VINOVPI] = REG_FIELD(RT9455_REG_IRQ1, 6, 6),
  108. [F_BATAB] = REG_FIELD(RT9455_REG_IRQ1, 0, 0),
  109. [F_CHRVPI] = REG_FIELD(RT9455_REG_IRQ2, 7, 7),
  110. [F_CHBATOVI] = REG_FIELD(RT9455_REG_IRQ2, 5, 5),
  111. [F_CHTERMI] = REG_FIELD(RT9455_REG_IRQ2, 4, 4),
  112. [F_CHRCHGI] = REG_FIELD(RT9455_REG_IRQ2, 3, 3),
  113. [F_CH32MI] = REG_FIELD(RT9455_REG_IRQ2, 2, 2),
  114. [F_CHTREGI] = REG_FIELD(RT9455_REG_IRQ2, 1, 1),
  115. [F_CHMIVRI] = REG_FIELD(RT9455_REG_IRQ2, 0, 0),
  116. [F_BSTBUSOVI] = REG_FIELD(RT9455_REG_IRQ3, 7, 7),
  117. [F_BSTOLI] = REG_FIELD(RT9455_REG_IRQ3, 6, 6),
  118. [F_BSTLOWVI] = REG_FIELD(RT9455_REG_IRQ3, 5, 5),
  119. [F_BST32SI] = REG_FIELD(RT9455_REG_IRQ3, 3, 3),
  120. [F_TSDM] = REG_FIELD(RT9455_REG_MASK1, 7, 7),
  121. [F_VINOVPIM] = REG_FIELD(RT9455_REG_MASK1, 6, 6),
  122. [F_BATABM] = REG_FIELD(RT9455_REG_MASK1, 0, 0),
  123. [F_CHRVPIM] = REG_FIELD(RT9455_REG_MASK2, 7, 7),
  124. [F_CHBATOVIM] = REG_FIELD(RT9455_REG_MASK2, 5, 5),
  125. [F_CHTERMIM] = REG_FIELD(RT9455_REG_MASK2, 4, 4),
  126. [F_CHRCHGIM] = REG_FIELD(RT9455_REG_MASK2, 3, 3),
  127. [F_CH32MIM] = REG_FIELD(RT9455_REG_MASK2, 2, 2),
  128. [F_CHTREGIM] = REG_FIELD(RT9455_REG_MASK2, 1, 1),
  129. [F_CHMIVRIM] = REG_FIELD(RT9455_REG_MASK2, 0, 0),
  130. [F_BSTVINOVIM] = REG_FIELD(RT9455_REG_MASK3, 7, 7),
  131. [F_BSTOLIM] = REG_FIELD(RT9455_REG_MASK3, 6, 6),
  132. [F_BSTLOWVIM] = REG_FIELD(RT9455_REG_MASK3, 5, 5),
  133. [F_BST32SIM] = REG_FIELD(RT9455_REG_MASK3, 3, 3),
  134. };
  135. #define GET_MASK(fid) (BIT(rt9455_reg_fields[fid].msb + 1) - \
  136. BIT(rt9455_reg_fields[fid].lsb))
  137. /*
  138. * Each array initialised below shows the possible real-world values for a
  139. * group of bits belonging to RT9455 registers. The arrays are sorted in
  140. * ascending order. The index of each real-world value represents the value
  141. * that is encoded in the group of bits belonging to RT9455 registers.
  142. */
  143. /* REG06[6:4] (ICHRG) in uAh */
  144. static const int rt9455_ichrg_values[] = {
  145. 500000, 650000, 800000, 950000, 1100000, 1250000, 1400000, 1550000
  146. };
  147. /*
  148. * When the charger is in charge mode, REG02[7:2] represent battery regulation
  149. * voltage.
  150. */
  151. /* REG02[7:2] (VOREG) in uV */
  152. static const int rt9455_voreg_values[] = {
  153. 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
  154. 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
  155. 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
  156. 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
  157. 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
  158. 4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000,
  159. 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000,
  160. 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000
  161. };
  162. /*
  163. * When the charger is in boost mode, REG02[7:2] represent boost output
  164. * voltage.
  165. */
  166. /* REG02[7:2] (Boost output voltage) in uV */
  167. static const int rt9455_boost_voltage_values[] = {
  168. 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
  169. 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
  170. 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
  171. 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
  172. 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
  173. 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
  174. 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
  175. 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
  176. };
  177. /* REG07[3:0] (VMREG) in uV */
  178. static const int rt9455_vmreg_values[] = {
  179. 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
  180. 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
  181. };
  182. /* REG05[5:4] (IEOC_PERCENTAGE) */
  183. static const int rt9455_ieoc_percentage_values[] = {
  184. 10, 30, 20, 30
  185. };
  186. /* REG05[1:0] (MIVR) in uV */
  187. static const int rt9455_mivr_values[] = {
  188. 4000000, 4250000, 4500000, 5000000
  189. };
  190. /* REG05[1:0] (IAICR) in uA */
  191. static const int rt9455_iaicr_values[] = {
  192. 100000, 500000, 1000000, 2000000
  193. };
  194. struct rt9455_info {
  195. struct i2c_client *client;
  196. struct regmap *regmap;
  197. struct regmap_field *regmap_fields[F_MAX_FIELDS];
  198. struct power_supply *charger;
  199. #if IS_ENABLED(CONFIG_USB_PHY)
  200. struct usb_phy *usb_phy;
  201. struct notifier_block nb;
  202. #endif
  203. struct delayed_work pwr_rdy_work;
  204. struct delayed_work max_charging_time_work;
  205. struct delayed_work batt_presence_work;
  206. u32 voreg;
  207. u32 boost_voltage;
  208. };
  209. /*
  210. * Iterate through each element of the 'tbl' array until an element whose value
  211. * is greater than v is found. Return the index of the respective element,
  212. * or the index of the last element in the array, if no such element is found.
  213. */
  214. static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
  215. {
  216. int i;
  217. /*
  218. * No need to iterate until the last index in the table because
  219. * if no element greater than v is found in the table,
  220. * or if only the last element is greater than v,
  221. * function returns the index of the last element.
  222. */
  223. for (i = 0; i < tbl_size - 1; i++)
  224. if (v <= tbl[i])
  225. return i;
  226. return (tbl_size - 1);
  227. }
  228. static int rt9455_get_field_val(struct rt9455_info *info,
  229. enum rt9455_fields field,
  230. const int tbl[], int tbl_size, int *val)
  231. {
  232. unsigned int v;
  233. int ret;
  234. ret = regmap_field_read(info->regmap_fields[field], &v);
  235. if (ret)
  236. return ret;
  237. v = (v >= tbl_size) ? (tbl_size - 1) : v;
  238. *val = tbl[v];
  239. return 0;
  240. }
  241. static int rt9455_set_field_val(struct rt9455_info *info,
  242. enum rt9455_fields field,
  243. const int tbl[], int tbl_size, int val)
  244. {
  245. unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
  246. return regmap_field_write(info->regmap_fields[field], idx);
  247. }
  248. static int rt9455_register_reset(struct rt9455_info *info)
  249. {
  250. struct device *dev = &info->client->dev;
  251. unsigned int v;
  252. int ret, limit = 100;
  253. ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
  254. if (ret) {
  255. dev_err(dev, "Failed to set RST bit\n");
  256. return ret;
  257. }
  258. /*
  259. * To make sure that reset operation has finished, loop until RST bit
  260. * is set to 0.
  261. */
  262. do {
  263. ret = regmap_field_read(info->regmap_fields[F_RST], &v);
  264. if (ret) {
  265. dev_err(dev, "Failed to read RST bit\n");
  266. return ret;
  267. }
  268. if (!v)
  269. break;
  270. usleep_range(10, 100);
  271. } while (--limit);
  272. if (!limit)
  273. return -EIO;
  274. return 0;
  275. }
  276. /* Charger power supply property routines */
  277. static enum power_supply_property rt9455_charger_properties[] = {
  278. POWER_SUPPLY_PROP_STATUS,
  279. POWER_SUPPLY_PROP_HEALTH,
  280. POWER_SUPPLY_PROP_PRESENT,
  281. POWER_SUPPLY_PROP_ONLINE,
  282. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  283. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  284. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  285. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  286. POWER_SUPPLY_PROP_SCOPE,
  287. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  288. POWER_SUPPLY_PROP_MODEL_NAME,
  289. POWER_SUPPLY_PROP_MANUFACTURER,
  290. };
  291. static char *rt9455_charger_supplied_to[] = {
  292. "main-battery",
  293. };
  294. static int rt9455_charger_get_status(struct rt9455_info *info,
  295. union power_supply_propval *val)
  296. {
  297. unsigned int v, pwr_rdy;
  298. int ret;
  299. ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
  300. &pwr_rdy);
  301. if (ret) {
  302. dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
  303. return ret;
  304. }
  305. /*
  306. * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
  307. * STAT bits value must be checked.
  308. */
  309. if (!pwr_rdy) {
  310. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  311. return 0;
  312. }
  313. ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
  314. if (ret) {
  315. dev_err(&info->client->dev, "Failed to read STAT bits\n");
  316. return ret;
  317. }
  318. switch (v) {
  319. case 0:
  320. /*
  321. * If PWR_RDY bit is set, but STAT bits value is 0, the charger
  322. * may be in one of the following cases:
  323. * 1. CHG_EN bit is 0.
  324. * 2. CHG_EN bit is 1 but the battery is not connected.
  325. * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
  326. * returned.
  327. */
  328. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  329. return 0;
  330. case 1:
  331. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  332. return 0;
  333. case 2:
  334. val->intval = POWER_SUPPLY_STATUS_FULL;
  335. return 0;
  336. default:
  337. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  338. return 0;
  339. }
  340. }
  341. static int rt9455_charger_get_health(struct rt9455_info *info,
  342. union power_supply_propval *val)
  343. {
  344. struct device *dev = &info->client->dev;
  345. unsigned int v;
  346. int ret;
  347. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  348. ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
  349. if (ret) {
  350. dev_err(dev, "Failed to read IRQ1 register\n");
  351. return ret;
  352. }
  353. if (v & GET_MASK(F_TSDI)) {
  354. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  355. return 0;
  356. }
  357. if (v & GET_MASK(F_VINOVPI)) {
  358. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  359. return 0;
  360. }
  361. if (v & GET_MASK(F_BATAB)) {
  362. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  363. return 0;
  364. }
  365. ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
  366. if (ret) {
  367. dev_err(dev, "Failed to read IRQ2 register\n");
  368. return ret;
  369. }
  370. if (v & GET_MASK(F_CHBATOVI)) {
  371. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  372. return 0;
  373. }
  374. if (v & GET_MASK(F_CH32MI)) {
  375. val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  376. return 0;
  377. }
  378. ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
  379. if (ret) {
  380. dev_err(dev, "Failed to read IRQ3 register\n");
  381. return ret;
  382. }
  383. if (v & GET_MASK(F_BSTBUSOVI)) {
  384. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  385. return 0;
  386. }
  387. if (v & GET_MASK(F_BSTOLI)) {
  388. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  389. return 0;
  390. }
  391. if (v & GET_MASK(F_BSTLOWVI)) {
  392. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  393. return 0;
  394. }
  395. if (v & GET_MASK(F_BST32SI)) {
  396. val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  397. return 0;
  398. }
  399. ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
  400. if (ret) {
  401. dev_err(dev, "Failed to read STAT bits\n");
  402. return ret;
  403. }
  404. if (v == RT9455_FAULT) {
  405. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  406. return 0;
  407. }
  408. return 0;
  409. }
  410. static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
  411. union power_supply_propval *val)
  412. {
  413. unsigned int v;
  414. int ret;
  415. ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
  416. if (ret) {
  417. dev_err(&info->client->dev, "Failed to read BATAB bit\n");
  418. return ret;
  419. }
  420. /*
  421. * Since BATAB is 1 when battery is NOT present and 0 otherwise,
  422. * !BATAB is returned.
  423. */
  424. val->intval = !v;
  425. return 0;
  426. }
  427. static int rt9455_charger_get_online(struct rt9455_info *info,
  428. union power_supply_propval *val)
  429. {
  430. unsigned int v;
  431. int ret;
  432. ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
  433. if (ret) {
  434. dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
  435. return ret;
  436. }
  437. val->intval = (int)v;
  438. return 0;
  439. }
  440. static int rt9455_charger_get_current(struct rt9455_info *info,
  441. union power_supply_propval *val)
  442. {
  443. int curr;
  444. int ret;
  445. ret = rt9455_get_field_val(info, F_ICHRG,
  446. rt9455_ichrg_values,
  447. ARRAY_SIZE(rt9455_ichrg_values),
  448. &curr);
  449. if (ret) {
  450. dev_err(&info->client->dev, "Failed to read ICHRG value\n");
  451. return ret;
  452. }
  453. val->intval = curr;
  454. return 0;
  455. }
  456. static int rt9455_charger_get_current_max(struct rt9455_info *info,
  457. union power_supply_propval *val)
  458. {
  459. int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
  460. val->intval = rt9455_ichrg_values[idx];
  461. return 0;
  462. }
  463. static int rt9455_charger_get_voltage(struct rt9455_info *info,
  464. union power_supply_propval *val)
  465. {
  466. int voltage;
  467. int ret;
  468. ret = rt9455_get_field_val(info, F_VOREG,
  469. rt9455_voreg_values,
  470. ARRAY_SIZE(rt9455_voreg_values),
  471. &voltage);
  472. if (ret) {
  473. dev_err(&info->client->dev, "Failed to read VOREG value\n");
  474. return ret;
  475. }
  476. val->intval = voltage;
  477. return 0;
  478. }
  479. static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
  480. union power_supply_propval *val)
  481. {
  482. int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
  483. val->intval = rt9455_vmreg_values[idx];
  484. return 0;
  485. }
  486. static int rt9455_charger_get_term_current(struct rt9455_info *info,
  487. union power_supply_propval *val)
  488. {
  489. struct device *dev = &info->client->dev;
  490. int ichrg, ieoc_percentage, ret;
  491. ret = rt9455_get_field_val(info, F_ICHRG,
  492. rt9455_ichrg_values,
  493. ARRAY_SIZE(rt9455_ichrg_values),
  494. &ichrg);
  495. if (ret) {
  496. dev_err(dev, "Failed to read ICHRG value\n");
  497. return ret;
  498. }
  499. ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
  500. rt9455_ieoc_percentage_values,
  501. ARRAY_SIZE(rt9455_ieoc_percentage_values),
  502. &ieoc_percentage);
  503. if (ret) {
  504. dev_err(dev, "Failed to read IEOC value\n");
  505. return ret;
  506. }
  507. val->intval = ichrg * ieoc_percentage / 100;
  508. return 0;
  509. }
  510. static int rt9455_charger_get_property(struct power_supply *psy,
  511. enum power_supply_property psp,
  512. union power_supply_propval *val)
  513. {
  514. struct rt9455_info *info = power_supply_get_drvdata(psy);
  515. switch (psp) {
  516. case POWER_SUPPLY_PROP_STATUS:
  517. return rt9455_charger_get_status(info, val);
  518. case POWER_SUPPLY_PROP_HEALTH:
  519. return rt9455_charger_get_health(info, val);
  520. case POWER_SUPPLY_PROP_PRESENT:
  521. return rt9455_charger_get_battery_presence(info, val);
  522. case POWER_SUPPLY_PROP_ONLINE:
  523. return rt9455_charger_get_online(info, val);
  524. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  525. return rt9455_charger_get_current(info, val);
  526. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  527. return rt9455_charger_get_current_max(info, val);
  528. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  529. return rt9455_charger_get_voltage(info, val);
  530. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  531. return rt9455_charger_get_voltage_max(info, val);
  532. case POWER_SUPPLY_PROP_SCOPE:
  533. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  534. return 0;
  535. case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  536. return rt9455_charger_get_term_current(info, val);
  537. case POWER_SUPPLY_PROP_MODEL_NAME:
  538. val->strval = RT9455_MODEL_NAME;
  539. return 0;
  540. case POWER_SUPPLY_PROP_MANUFACTURER:
  541. val->strval = RT9455_MANUFACTURER;
  542. return 0;
  543. default:
  544. return -ENODATA;
  545. }
  546. }
  547. static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
  548. u32 ieoc_percentage,
  549. u32 mivr, u32 iaicr)
  550. {
  551. struct device *dev = &info->client->dev;
  552. int idx, ret;
  553. ret = rt9455_register_reset(info);
  554. if (ret) {
  555. dev_err(dev, "Power On Reset failed\n");
  556. return ret;
  557. }
  558. /* Set TE bit in order to enable end of charge detection */
  559. ret = regmap_field_write(info->regmap_fields[F_TE], 1);
  560. if (ret) {
  561. dev_err(dev, "Failed to set TE bit\n");
  562. return ret;
  563. }
  564. /* Set TE_SHDN_EN bit in order to enable end of charge detection */
  565. ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
  566. if (ret) {
  567. dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
  568. return ret;
  569. }
  570. /*
  571. * Set BATD_EN bit in order to enable battery detection
  572. * when charging is done
  573. */
  574. ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
  575. if (ret) {
  576. dev_err(dev, "Failed to set BATD_EN bit\n");
  577. return ret;
  578. }
  579. /*
  580. * Disable Safety Timer. In charge mode, this timer terminates charging
  581. * if no read or write via I2C is done within 32 minutes. This timer
  582. * avoids overcharging the baterry when the OS is not loaded and the
  583. * charger is connected to a power source.
  584. * In boost mode, this timer triggers BST32SI interrupt if no read or
  585. * write via I2C is done within 32 seconds.
  586. * When the OS is loaded and the charger driver is inserted, it is used
  587. * delayed_work, named max_charging_time_work, to avoid overcharging
  588. * the battery.
  589. */
  590. ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
  591. if (ret) {
  592. dev_err(dev, "Failed to disable Safety Timer\n");
  593. return ret;
  594. }
  595. /* Set ICHRG to value retrieved from device-specific data */
  596. ret = rt9455_set_field_val(info, F_ICHRG,
  597. rt9455_ichrg_values,
  598. ARRAY_SIZE(rt9455_ichrg_values), ichrg);
  599. if (ret) {
  600. dev_err(dev, "Failed to set ICHRG value\n");
  601. return ret;
  602. }
  603. /* Set IEOC Percentage to value retrieved from device-specific data */
  604. ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
  605. rt9455_ieoc_percentage_values,
  606. ARRAY_SIZE(rt9455_ieoc_percentage_values),
  607. ieoc_percentage);
  608. if (ret) {
  609. dev_err(dev, "Failed to set IEOC Percentage value\n");
  610. return ret;
  611. }
  612. /* Set VOREG to value retrieved from device-specific data */
  613. ret = rt9455_set_field_val(info, F_VOREG,
  614. rt9455_voreg_values,
  615. ARRAY_SIZE(rt9455_voreg_values),
  616. info->voreg);
  617. if (ret) {
  618. dev_err(dev, "Failed to set VOREG value\n");
  619. return ret;
  620. }
  621. /* Set VMREG value to maximum (4.45V). */
  622. idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
  623. ret = rt9455_set_field_val(info, F_VMREG,
  624. rt9455_vmreg_values,
  625. ARRAY_SIZE(rt9455_vmreg_values),
  626. rt9455_vmreg_values[idx]);
  627. if (ret) {
  628. dev_err(dev, "Failed to set VMREG value\n");
  629. return ret;
  630. }
  631. /*
  632. * Set MIVR to value retrieved from device-specific data.
  633. * If no value is specified, default value for MIVR is 4.5V.
  634. */
  635. if (mivr == -1)
  636. mivr = 4500000;
  637. ret = rt9455_set_field_val(info, F_MIVR,
  638. rt9455_mivr_values,
  639. ARRAY_SIZE(rt9455_mivr_values), mivr);
  640. if (ret) {
  641. dev_err(dev, "Failed to set MIVR value\n");
  642. return ret;
  643. }
  644. /*
  645. * Set IAICR to value retrieved from device-specific data.
  646. * If no value is specified, default value for IAICR is 500 mA.
  647. */
  648. if (iaicr == -1)
  649. iaicr = 500000;
  650. ret = rt9455_set_field_val(info, F_IAICR,
  651. rt9455_iaicr_values,
  652. ARRAY_SIZE(rt9455_iaicr_values), iaicr);
  653. if (ret) {
  654. dev_err(dev, "Failed to set IAICR value\n");
  655. return ret;
  656. }
  657. /*
  658. * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
  659. * and not by OTG pin.
  660. */
  661. ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
  662. if (ret) {
  663. dev_err(dev, "Failed to set IAICR_INT bit\n");
  664. return ret;
  665. }
  666. /*
  667. * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
  668. * CHMIVRI is triggered, but there is no action to be taken by the
  669. * driver when CHMIVRI is triggered.
  670. */
  671. ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
  672. if (ret) {
  673. dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
  674. return ret;
  675. }
  676. return 0;
  677. }
  678. #if IS_ENABLED(CONFIG_USB_PHY)
  679. /*
  680. * Before setting the charger into boost mode, boost output voltage is
  681. * set. This is needed because boost output voltage may differ from battery
  682. * regulation voltage. F_VOREG bits represent either battery regulation voltage
  683. * or boost output voltage, depending on the mode the charger is. Both battery
  684. * regulation voltage and boost output voltage are read from DT/ACPI during
  685. * probe.
  686. */
  687. static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
  688. {
  689. struct device *dev = &info->client->dev;
  690. int ret;
  691. ret = rt9455_set_field_val(info, F_VOREG,
  692. rt9455_boost_voltage_values,
  693. ARRAY_SIZE(rt9455_boost_voltage_values),
  694. info->boost_voltage);
  695. if (ret) {
  696. dev_err(dev, "Failed to set boost output voltage value\n");
  697. return ret;
  698. }
  699. return 0;
  700. }
  701. #endif
  702. /*
  703. * Before setting the charger into charge mode, battery regulation voltage is
  704. * set. This is needed because boost output voltage may differ from battery
  705. * regulation voltage. F_VOREG bits represent either battery regulation voltage
  706. * or boost output voltage, depending on the mode the charger is. Both battery
  707. * regulation voltage and boost output voltage are read from DT/ACPI during
  708. * probe.
  709. */
  710. static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
  711. {
  712. struct device *dev = &info->client->dev;
  713. int ret;
  714. ret = rt9455_set_field_val(info, F_VOREG,
  715. rt9455_voreg_values,
  716. ARRAY_SIZE(rt9455_voreg_values),
  717. info->voreg);
  718. if (ret) {
  719. dev_err(dev, "Failed to set VOREG value\n");
  720. return ret;
  721. }
  722. return 0;
  723. }
  724. static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
  725. bool *_is_battery_absent,
  726. bool *_alert_userspace)
  727. {
  728. unsigned int irq1, mask1, mask2;
  729. struct device *dev = &info->client->dev;
  730. bool is_battery_absent = false;
  731. bool alert_userspace = false;
  732. int ret;
  733. ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
  734. if (ret) {
  735. dev_err(dev, "Failed to read IRQ1 register\n");
  736. return ret;
  737. }
  738. ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
  739. if (ret) {
  740. dev_err(dev, "Failed to read MASK1 register\n");
  741. return ret;
  742. }
  743. if (irq1 & GET_MASK(F_TSDI)) {
  744. dev_err(dev, "Thermal shutdown fault occurred\n");
  745. alert_userspace = true;
  746. }
  747. if (irq1 & GET_MASK(F_VINOVPI)) {
  748. dev_err(dev, "Overvoltage input occurred\n");
  749. alert_userspace = true;
  750. }
  751. if (irq1 & GET_MASK(F_BATAB)) {
  752. dev_err(dev, "Battery absence occurred\n");
  753. is_battery_absent = true;
  754. alert_userspace = true;
  755. if ((mask1 & GET_MASK(F_BATABM)) == 0) {
  756. ret = regmap_field_write(info->regmap_fields[F_BATABM],
  757. 0x01);
  758. if (ret) {
  759. dev_err(dev, "Failed to mask BATAB interrupt\n");
  760. return ret;
  761. }
  762. }
  763. ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
  764. if (ret) {
  765. dev_err(dev, "Failed to read MASK2 register\n");
  766. return ret;
  767. }
  768. if (mask2 & GET_MASK(F_CHTERMIM)) {
  769. ret = regmap_field_write(
  770. info->regmap_fields[F_CHTERMIM], 0x00);
  771. if (ret) {
  772. dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
  773. return ret;
  774. }
  775. }
  776. if (mask2 & GET_MASK(F_CHRCHGIM)) {
  777. ret = regmap_field_write(
  778. info->regmap_fields[F_CHRCHGIM], 0x00);
  779. if (ret) {
  780. dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
  781. return ret;
  782. }
  783. }
  784. /*
  785. * When the battery is absent, max_charging_time_work is
  786. * cancelled, since no charging is done.
  787. */
  788. cancel_delayed_work_sync(&info->max_charging_time_work);
  789. /*
  790. * Since no interrupt is triggered when the battery is
  791. * reconnected, max_charging_time_work is not rescheduled.
  792. * Therefore, batt_presence_work is scheduled to check whether
  793. * the battery is still absent or not.
  794. */
  795. queue_delayed_work(system_power_efficient_wq,
  796. &info->batt_presence_work,
  797. RT9455_BATT_PRESENCE_DELAY * HZ);
  798. }
  799. *_is_battery_absent = is_battery_absent;
  800. if (alert_userspace)
  801. *_alert_userspace = alert_userspace;
  802. return 0;
  803. }
  804. static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
  805. bool is_battery_absent,
  806. bool *_alert_userspace)
  807. {
  808. unsigned int irq2, mask2;
  809. struct device *dev = &info->client->dev;
  810. bool alert_userspace = false;
  811. int ret;
  812. ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
  813. if (ret) {
  814. dev_err(dev, "Failed to read IRQ2 register\n");
  815. return ret;
  816. }
  817. ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
  818. if (ret) {
  819. dev_err(dev, "Failed to read MASK2 register\n");
  820. return ret;
  821. }
  822. if (irq2 & GET_MASK(F_CHRVPI)) {
  823. dev_dbg(dev, "Charger fault occurred\n");
  824. /*
  825. * CHRVPI bit is set in 2 cases:
  826. * 1. when the power source is connected to the charger.
  827. * 2. when the power source is disconnected from the charger.
  828. * To identify the case, PWR_RDY bit is checked. Because
  829. * PWR_RDY bit is set / cleared after CHRVPI interrupt is
  830. * triggered, it is used delayed_work to later read PWR_RDY bit.
  831. * Also, do not set to true alert_userspace, because there is no
  832. * need to notify userspace when CHRVPI interrupt has occurred.
  833. * Userspace will be notified after PWR_RDY bit is read.
  834. */
  835. queue_delayed_work(system_power_efficient_wq,
  836. &info->pwr_rdy_work,
  837. RT9455_PWR_RDY_DELAY * HZ);
  838. }
  839. if (irq2 & GET_MASK(F_CHBATOVI)) {
  840. dev_err(dev, "Battery OVP occurred\n");
  841. alert_userspace = true;
  842. }
  843. if (irq2 & GET_MASK(F_CHTERMI)) {
  844. dev_dbg(dev, "Charge terminated\n");
  845. if (!is_battery_absent) {
  846. if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
  847. ret = regmap_field_write(
  848. info->regmap_fields[F_CHTERMIM], 0x01);
  849. if (ret) {
  850. dev_err(dev, "Failed to mask CHTERMI interrupt\n");
  851. return ret;
  852. }
  853. /*
  854. * Update MASK2 value, since CHTERMIM bit is
  855. * set.
  856. */
  857. mask2 = mask2 | GET_MASK(F_CHTERMIM);
  858. }
  859. cancel_delayed_work_sync(&info->max_charging_time_work);
  860. alert_userspace = true;
  861. }
  862. }
  863. if (irq2 & GET_MASK(F_CHRCHGI)) {
  864. dev_dbg(dev, "Recharge request\n");
  865. ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
  866. RT9455_CHARGE_ENABLE);
  867. if (ret) {
  868. dev_err(dev, "Failed to enable charging\n");
  869. return ret;
  870. }
  871. if (mask2 & GET_MASK(F_CHTERMIM)) {
  872. ret = regmap_field_write(
  873. info->regmap_fields[F_CHTERMIM], 0x00);
  874. if (ret) {
  875. dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
  876. return ret;
  877. }
  878. /* Update MASK2 value, since CHTERMIM bit is cleared. */
  879. mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
  880. }
  881. if (!is_battery_absent) {
  882. /*
  883. * No need to check whether the charger is connected to
  884. * power source when CHRCHGI is received, since CHRCHGI
  885. * is not triggered if the charger is not connected to
  886. * the power source.
  887. */
  888. queue_delayed_work(system_power_efficient_wq,
  889. &info->max_charging_time_work,
  890. RT9455_MAX_CHARGING_TIME * HZ);
  891. alert_userspace = true;
  892. }
  893. }
  894. if (irq2 & GET_MASK(F_CH32MI)) {
  895. dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
  896. alert_userspace = true;
  897. }
  898. if (irq2 & GET_MASK(F_CHTREGI)) {
  899. dev_warn(dev,
  900. "Charger warning. Thermal regulation loop active\n");
  901. alert_userspace = true;
  902. }
  903. if (irq2 & GET_MASK(F_CHMIVRI)) {
  904. dev_dbg(dev,
  905. "Charger warning. Input voltage MIVR loop active\n");
  906. }
  907. if (alert_userspace)
  908. *_alert_userspace = alert_userspace;
  909. return 0;
  910. }
  911. static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
  912. bool *_alert_userspace)
  913. {
  914. unsigned int irq3, mask3;
  915. struct device *dev = &info->client->dev;
  916. bool alert_userspace = false;
  917. int ret;
  918. ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
  919. if (ret) {
  920. dev_err(dev, "Failed to read IRQ3 register\n");
  921. return ret;
  922. }
  923. ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
  924. if (ret) {
  925. dev_err(dev, "Failed to read MASK3 register\n");
  926. return ret;
  927. }
  928. if (irq3 & GET_MASK(F_BSTBUSOVI)) {
  929. dev_err(dev, "Boost fault. Overvoltage input occurred\n");
  930. alert_userspace = true;
  931. }
  932. if (irq3 & GET_MASK(F_BSTOLI)) {
  933. dev_err(dev, "Boost fault. Overload\n");
  934. alert_userspace = true;
  935. }
  936. if (irq3 & GET_MASK(F_BSTLOWVI)) {
  937. dev_err(dev, "Boost fault. Battery voltage too low\n");
  938. alert_userspace = true;
  939. }
  940. if (irq3 & GET_MASK(F_BST32SI)) {
  941. dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
  942. alert_userspace = true;
  943. }
  944. if (alert_userspace) {
  945. dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
  946. ret = rt9455_set_voreg_before_charge_mode(info);
  947. if (ret) {
  948. dev_err(dev, "Failed to set VOREG before entering charge mode\n");
  949. return ret;
  950. }
  951. ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
  952. RT9455_CHARGE_MODE);
  953. if (ret) {
  954. dev_err(dev, "Failed to set charger in charge mode\n");
  955. return ret;
  956. }
  957. *_alert_userspace = alert_userspace;
  958. }
  959. return 0;
  960. }
  961. static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
  962. {
  963. struct rt9455_info *info = data;
  964. struct device *dev;
  965. bool alert_userspace = false;
  966. bool is_battery_absent = false;
  967. unsigned int status;
  968. int ret;
  969. if (!info)
  970. return IRQ_NONE;
  971. dev = &info->client->dev;
  972. if (irq != info->client->irq) {
  973. dev_err(dev, "Interrupt is not for RT9455 charger\n");
  974. return IRQ_NONE;
  975. }
  976. ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
  977. if (ret) {
  978. dev_err(dev, "Failed to read STAT bits\n");
  979. return IRQ_HANDLED;
  980. }
  981. dev_dbg(dev, "Charger status is %d\n", status);
  982. /*
  983. * Each function that processes an IRQ register receives as output
  984. * parameter alert_userspace pointer. alert_userspace is set to true
  985. * in such a function only if an interrupt has occurred in the
  986. * respective interrupt register. This way, it is avoided the following
  987. * case: interrupt occurs only in IRQ1 register,
  988. * rt9455_irq_handler_check_irq1_register() function sets to true
  989. * alert_userspace, but rt9455_irq_handler_check_irq2_register()
  990. * and rt9455_irq_handler_check_irq3_register() functions set to false
  991. * alert_userspace and power_supply_changed() is never called.
  992. */
  993. ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
  994. &alert_userspace);
  995. if (ret) {
  996. dev_err(dev, "Failed to handle IRQ1 register\n");
  997. return IRQ_HANDLED;
  998. }
  999. ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
  1000. &alert_userspace);
  1001. if (ret) {
  1002. dev_err(dev, "Failed to handle IRQ2 register\n");
  1003. return IRQ_HANDLED;
  1004. }
  1005. ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
  1006. if (ret) {
  1007. dev_err(dev, "Failed to handle IRQ3 register\n");
  1008. return IRQ_HANDLED;
  1009. }
  1010. if (alert_userspace) {
  1011. /*
  1012. * Sometimes, an interrupt occurs while rt9455_probe() function
  1013. * is executing and power_supply_register() is not yet called.
  1014. * Do not call power_supply_changed() in this case.
  1015. */
  1016. if (info->charger)
  1017. power_supply_changed(info->charger);
  1018. }
  1019. return IRQ_HANDLED;
  1020. }
  1021. static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
  1022. u32 *ieoc_percentage,
  1023. u32 *mivr, u32 *iaicr)
  1024. {
  1025. struct device *dev = &info->client->dev;
  1026. int ret;
  1027. if (!dev->of_node && !ACPI_HANDLE(dev)) {
  1028. dev_err(dev, "No support for either device tree or ACPI\n");
  1029. return -EINVAL;
  1030. }
  1031. /*
  1032. * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
  1033. * parameters.
  1034. */
  1035. ret = device_property_read_u32(dev, "richtek,output-charge-current",
  1036. ichrg);
  1037. if (ret) {
  1038. dev_err(dev, "Error: missing \"output-charge-current\" property\n");
  1039. return ret;
  1040. }
  1041. ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
  1042. ieoc_percentage);
  1043. if (ret) {
  1044. dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
  1045. return ret;
  1046. }
  1047. ret = device_property_read_u32(dev,
  1048. "richtek,battery-regulation-voltage",
  1049. &info->voreg);
  1050. if (ret) {
  1051. dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
  1052. return ret;
  1053. }
  1054. ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
  1055. &info->boost_voltage);
  1056. if (ret) {
  1057. dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
  1058. return ret;
  1059. }
  1060. /*
  1061. * MIVR and IAICR are optional parameters. Do not return error if one of
  1062. * them is not present in ACPI table or device tree specification.
  1063. */
  1064. device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
  1065. mivr);
  1066. device_property_read_u32(dev, "richtek,avg-input-current-regulation",
  1067. iaicr);
  1068. return 0;
  1069. }
  1070. #if IS_ENABLED(CONFIG_USB_PHY)
  1071. static int rt9455_usb_event_none(struct rt9455_info *info,
  1072. u8 opa_mode, u8 iaicr)
  1073. {
  1074. struct device *dev = &info->client->dev;
  1075. int ret;
  1076. if (opa_mode == RT9455_BOOST_MODE) {
  1077. ret = rt9455_set_voreg_before_charge_mode(info);
  1078. if (ret) {
  1079. dev_err(dev, "Failed to set VOREG before entering charge mode\n");
  1080. return ret;
  1081. }
  1082. /*
  1083. * If the charger is in boost mode, and it has received
  1084. * USB_EVENT_NONE, this means the consumer device powered by the
  1085. * charger is not connected anymore.
  1086. * In this case, the charger goes into charge mode.
  1087. */
  1088. dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
  1089. ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
  1090. RT9455_CHARGE_MODE);
  1091. if (ret) {
  1092. dev_err(dev, "Failed to set charger in charge mode\n");
  1093. return NOTIFY_DONE;
  1094. }
  1095. }
  1096. dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
  1097. if (iaicr != RT9455_IAICR_100MA) {
  1098. ret = regmap_field_write(info->regmap_fields[F_IAICR],
  1099. RT9455_IAICR_100MA);
  1100. if (ret) {
  1101. dev_err(dev, "Failed to set IAICR value\n");
  1102. return NOTIFY_DONE;
  1103. }
  1104. }
  1105. return NOTIFY_OK;
  1106. }
  1107. static int rt9455_usb_event_vbus(struct rt9455_info *info,
  1108. u8 opa_mode, u8 iaicr)
  1109. {
  1110. struct device *dev = &info->client->dev;
  1111. int ret;
  1112. if (opa_mode == RT9455_BOOST_MODE) {
  1113. ret = rt9455_set_voreg_before_charge_mode(info);
  1114. if (ret) {
  1115. dev_err(dev, "Failed to set VOREG before entering charge mode\n");
  1116. return ret;
  1117. }
  1118. /*
  1119. * If the charger is in boost mode, and it has received
  1120. * USB_EVENT_VBUS, this means the consumer device powered by the
  1121. * charger is not connected anymore.
  1122. * In this case, the charger goes into charge mode.
  1123. */
  1124. dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
  1125. ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
  1126. RT9455_CHARGE_MODE);
  1127. if (ret) {
  1128. dev_err(dev, "Failed to set charger in charge mode\n");
  1129. return NOTIFY_DONE;
  1130. }
  1131. }
  1132. dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
  1133. if (iaicr != RT9455_IAICR_500MA) {
  1134. ret = regmap_field_write(info->regmap_fields[F_IAICR],
  1135. RT9455_IAICR_500MA);
  1136. if (ret) {
  1137. dev_err(dev, "Failed to set IAICR value\n");
  1138. return NOTIFY_DONE;
  1139. }
  1140. }
  1141. return NOTIFY_OK;
  1142. }
  1143. static int rt9455_usb_event_id(struct rt9455_info *info,
  1144. u8 opa_mode, u8 iaicr)
  1145. {
  1146. struct device *dev = &info->client->dev;
  1147. int ret;
  1148. if (opa_mode == RT9455_CHARGE_MODE) {
  1149. ret = rt9455_set_boost_voltage_before_boost_mode(info);
  1150. if (ret) {
  1151. dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
  1152. return ret;
  1153. }
  1154. /*
  1155. * If the charger is in charge mode, and it has received
  1156. * USB_EVENT_ID, this means a consumer device is connected and
  1157. * it should be powered by the charger.
  1158. * In this case, the charger goes into boost mode.
  1159. */
  1160. dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
  1161. ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
  1162. RT9455_BOOST_MODE);
  1163. if (ret) {
  1164. dev_err(dev, "Failed to set charger in boost mode\n");
  1165. return NOTIFY_DONE;
  1166. }
  1167. }
  1168. dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
  1169. if (iaicr != RT9455_IAICR_100MA) {
  1170. ret = regmap_field_write(info->regmap_fields[F_IAICR],
  1171. RT9455_IAICR_100MA);
  1172. if (ret) {
  1173. dev_err(dev, "Failed to set IAICR value\n");
  1174. return NOTIFY_DONE;
  1175. }
  1176. }
  1177. return NOTIFY_OK;
  1178. }
  1179. static int rt9455_usb_event_charger(struct rt9455_info *info,
  1180. u8 opa_mode, u8 iaicr)
  1181. {
  1182. struct device *dev = &info->client->dev;
  1183. int ret;
  1184. if (opa_mode == RT9455_BOOST_MODE) {
  1185. ret = rt9455_set_voreg_before_charge_mode(info);
  1186. if (ret) {
  1187. dev_err(dev, "Failed to set VOREG before entering charge mode\n");
  1188. return ret;
  1189. }
  1190. /*
  1191. * If the charger is in boost mode, and it has received
  1192. * USB_EVENT_CHARGER, this means the consumer device powered by
  1193. * the charger is not connected anymore.
  1194. * In this case, the charger goes into charge mode.
  1195. */
  1196. dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
  1197. ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
  1198. RT9455_CHARGE_MODE);
  1199. if (ret) {
  1200. dev_err(dev, "Failed to set charger in charge mode\n");
  1201. return NOTIFY_DONE;
  1202. }
  1203. }
  1204. dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
  1205. if (iaicr != RT9455_IAICR_NO_LIMIT) {
  1206. ret = regmap_field_write(info->regmap_fields[F_IAICR],
  1207. RT9455_IAICR_NO_LIMIT);
  1208. if (ret) {
  1209. dev_err(dev, "Failed to set IAICR value\n");
  1210. return NOTIFY_DONE;
  1211. }
  1212. }
  1213. return NOTIFY_OK;
  1214. }
  1215. static int rt9455_usb_event(struct notifier_block *nb,
  1216. unsigned long event, void *power)
  1217. {
  1218. struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
  1219. struct device *dev = &info->client->dev;
  1220. unsigned int opa_mode, iaicr;
  1221. int ret;
  1222. /*
  1223. * Determine whether the charger is in charge mode
  1224. * or in boost mode.
  1225. */
  1226. ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
  1227. &opa_mode);
  1228. if (ret) {
  1229. dev_err(dev, "Failed to read OPA_MODE value\n");
  1230. return NOTIFY_DONE;
  1231. }
  1232. ret = regmap_field_read(info->regmap_fields[F_IAICR],
  1233. &iaicr);
  1234. if (ret) {
  1235. dev_err(dev, "Failed to read IAICR value\n");
  1236. return NOTIFY_DONE;
  1237. }
  1238. dev_dbg(dev, "Received USB event %lu\n", event);
  1239. switch (event) {
  1240. case USB_EVENT_NONE:
  1241. return rt9455_usb_event_none(info, opa_mode, iaicr);
  1242. case USB_EVENT_VBUS:
  1243. return rt9455_usb_event_vbus(info, opa_mode, iaicr);
  1244. case USB_EVENT_ID:
  1245. return rt9455_usb_event_id(info, opa_mode, iaicr);
  1246. case USB_EVENT_CHARGER:
  1247. return rt9455_usb_event_charger(info, opa_mode, iaicr);
  1248. default:
  1249. dev_err(dev, "Unknown USB event\n");
  1250. }
  1251. return NOTIFY_DONE;
  1252. }
  1253. #endif
  1254. static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
  1255. {
  1256. struct rt9455_info *info = container_of(work, struct rt9455_info,
  1257. pwr_rdy_work.work);
  1258. struct device *dev = &info->client->dev;
  1259. unsigned int pwr_rdy;
  1260. int ret;
  1261. ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
  1262. if (ret) {
  1263. dev_err(dev, "Failed to read PWR_RDY bit\n");
  1264. return;
  1265. }
  1266. switch (pwr_rdy) {
  1267. case RT9455_PWR_FAULT:
  1268. dev_dbg(dev, "Charger disconnected from power source\n");
  1269. cancel_delayed_work_sync(&info->max_charging_time_work);
  1270. break;
  1271. case RT9455_PWR_GOOD:
  1272. dev_dbg(dev, "Charger connected to power source\n");
  1273. ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
  1274. RT9455_CHARGE_ENABLE);
  1275. if (ret) {
  1276. dev_err(dev, "Failed to enable charging\n");
  1277. return;
  1278. }
  1279. queue_delayed_work(system_power_efficient_wq,
  1280. &info->max_charging_time_work,
  1281. RT9455_MAX_CHARGING_TIME * HZ);
  1282. break;
  1283. }
  1284. /*
  1285. * Notify userspace that the charger has been either connected to or
  1286. * disconnected from the power source.
  1287. */
  1288. power_supply_changed(info->charger);
  1289. }
  1290. static void rt9455_max_charging_time_work_callback(struct work_struct *work)
  1291. {
  1292. struct rt9455_info *info = container_of(work, struct rt9455_info,
  1293. max_charging_time_work.work);
  1294. struct device *dev = &info->client->dev;
  1295. int ret;
  1296. dev_err(dev, "Battery has been charging for at least 6 hours and is not yet fully charged. Battery is dead, therefore charging is disabled.\n");
  1297. ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
  1298. RT9455_CHARGE_DISABLE);
  1299. if (ret)
  1300. dev_err(dev, "Failed to disable charging\n");
  1301. }
  1302. static void rt9455_batt_presence_work_callback(struct work_struct *work)
  1303. {
  1304. struct rt9455_info *info = container_of(work, struct rt9455_info,
  1305. batt_presence_work.work);
  1306. struct device *dev = &info->client->dev;
  1307. unsigned int irq1, mask1;
  1308. int ret;
  1309. ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
  1310. if (ret) {
  1311. dev_err(dev, "Failed to read IRQ1 register\n");
  1312. return;
  1313. }
  1314. /*
  1315. * If the battery is still absent, batt_presence_work is rescheduled.
  1316. * Otherwise, max_charging_time is scheduled.
  1317. */
  1318. if (irq1 & GET_MASK(F_BATAB)) {
  1319. queue_delayed_work(system_power_efficient_wq,
  1320. &info->batt_presence_work,
  1321. RT9455_BATT_PRESENCE_DELAY * HZ);
  1322. } else {
  1323. queue_delayed_work(system_power_efficient_wq,
  1324. &info->max_charging_time_work,
  1325. RT9455_MAX_CHARGING_TIME * HZ);
  1326. ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
  1327. if (ret) {
  1328. dev_err(dev, "Failed to read MASK1 register\n");
  1329. return;
  1330. }
  1331. if (mask1 & GET_MASK(F_BATABM)) {
  1332. ret = regmap_field_write(info->regmap_fields[F_BATABM],
  1333. 0x00);
  1334. if (ret)
  1335. dev_err(dev, "Failed to unmask BATAB interrupt\n");
  1336. }
  1337. /*
  1338. * Notify userspace that the battery is now connected to the
  1339. * charger.
  1340. */
  1341. power_supply_changed(info->charger);
  1342. }
  1343. }
  1344. static const struct power_supply_desc rt9455_charger_desc = {
  1345. .name = RT9455_DRIVER_NAME,
  1346. .type = POWER_SUPPLY_TYPE_USB,
  1347. .properties = rt9455_charger_properties,
  1348. .num_properties = ARRAY_SIZE(rt9455_charger_properties),
  1349. .get_property = rt9455_charger_get_property,
  1350. };
  1351. static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
  1352. {
  1353. switch (reg) {
  1354. case RT9455_REG_DEV_ID:
  1355. case RT9455_REG_IRQ1:
  1356. case RT9455_REG_IRQ2:
  1357. case RT9455_REG_IRQ3:
  1358. return false;
  1359. default:
  1360. return true;
  1361. }
  1362. }
  1363. static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
  1364. {
  1365. switch (reg) {
  1366. case RT9455_REG_DEV_ID:
  1367. case RT9455_REG_CTRL5:
  1368. case RT9455_REG_CTRL6:
  1369. return false;
  1370. default:
  1371. return true;
  1372. }
  1373. }
  1374. static const struct regmap_config rt9455_regmap_config = {
  1375. .reg_bits = 8,
  1376. .val_bits = 8,
  1377. .writeable_reg = rt9455_is_writeable_reg,
  1378. .volatile_reg = rt9455_is_volatile_reg,
  1379. .max_register = RT9455_REG_MASK3,
  1380. .cache_type = REGCACHE_RBTREE,
  1381. };
  1382. static int rt9455_probe(struct i2c_client *client,
  1383. const struct i2c_device_id *id)
  1384. {
  1385. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1386. struct device *dev = &client->dev;
  1387. struct rt9455_info *info;
  1388. struct power_supply_config rt9455_charger_config = {};
  1389. /*
  1390. * Mandatory device-specific data values. Also, VOREG and boost output
  1391. * voltage are mandatory values, but they are stored in rt9455_info
  1392. * structure.
  1393. */
  1394. u32 ichrg, ieoc_percentage;
  1395. /* Optional device-specific data values. */
  1396. u32 mivr = -1, iaicr = -1;
  1397. int i, ret;
  1398. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1399. dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
  1400. return -ENODEV;
  1401. }
  1402. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  1403. if (!info)
  1404. return -ENOMEM;
  1405. info->client = client;
  1406. i2c_set_clientdata(client, info);
  1407. info->regmap = devm_regmap_init_i2c(client,
  1408. &rt9455_regmap_config);
  1409. if (IS_ERR(info->regmap)) {
  1410. dev_err(dev, "Failed to initialize register map\n");
  1411. return -EINVAL;
  1412. }
  1413. for (i = 0; i < F_MAX_FIELDS; i++) {
  1414. info->regmap_fields[i] =
  1415. devm_regmap_field_alloc(dev, info->regmap,
  1416. rt9455_reg_fields[i]);
  1417. if (IS_ERR(info->regmap_fields[i])) {
  1418. dev_err(dev,
  1419. "Failed to allocate regmap field = %d\n", i);
  1420. return PTR_ERR(info->regmap_fields[i]);
  1421. }
  1422. }
  1423. ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage,
  1424. &mivr, &iaicr);
  1425. if (ret) {
  1426. dev_err(dev, "Failed to discover charger\n");
  1427. return ret;
  1428. }
  1429. #if IS_ENABLED(CONFIG_USB_PHY)
  1430. info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  1431. if (IS_ERR(info->usb_phy)) {
  1432. dev_err(dev, "Failed to get USB transceiver\n");
  1433. } else {
  1434. info->nb.notifier_call = rt9455_usb_event;
  1435. ret = usb_register_notifier(info->usb_phy, &info->nb);
  1436. if (ret) {
  1437. dev_err(dev, "Failed to register USB notifier\n");
  1438. /*
  1439. * If usb_register_notifier() fails, set notifier_call
  1440. * to NULL, to avoid calling usb_unregister_notifier().
  1441. */
  1442. info->nb.notifier_call = NULL;
  1443. }
  1444. }
  1445. #endif
  1446. INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback);
  1447. INIT_DEFERRABLE_WORK(&info->max_charging_time_work,
  1448. rt9455_max_charging_time_work_callback);
  1449. INIT_DEFERRABLE_WORK(&info->batt_presence_work,
  1450. rt9455_batt_presence_work_callback);
  1451. rt9455_charger_config.of_node = dev->of_node;
  1452. rt9455_charger_config.drv_data = info;
  1453. rt9455_charger_config.supplied_to = rt9455_charger_supplied_to;
  1454. rt9455_charger_config.num_supplicants =
  1455. ARRAY_SIZE(rt9455_charger_supplied_to);
  1456. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  1457. rt9455_irq_handler_thread,
  1458. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  1459. RT9455_DRIVER_NAME, info);
  1460. if (ret) {
  1461. dev_err(dev, "Failed to register IRQ handler\n");
  1462. goto put_usb_notifier;
  1463. }
  1464. ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr);
  1465. if (ret) {
  1466. dev_err(dev, "Failed to set charger to its default values\n");
  1467. goto put_usb_notifier;
  1468. }
  1469. info->charger = devm_power_supply_register(dev, &rt9455_charger_desc,
  1470. &rt9455_charger_config);
  1471. if (IS_ERR(info->charger)) {
  1472. dev_err(dev, "Failed to register charger\n");
  1473. ret = PTR_ERR(info->charger);
  1474. goto put_usb_notifier;
  1475. }
  1476. return 0;
  1477. put_usb_notifier:
  1478. #if IS_ENABLED(CONFIG_USB_PHY)
  1479. if (info->nb.notifier_call) {
  1480. usb_unregister_notifier(info->usb_phy, &info->nb);
  1481. info->nb.notifier_call = NULL;
  1482. }
  1483. #endif
  1484. return ret;
  1485. }
  1486. static int rt9455_remove(struct i2c_client *client)
  1487. {
  1488. int ret;
  1489. struct rt9455_info *info = i2c_get_clientdata(client);
  1490. ret = rt9455_register_reset(info);
  1491. if (ret)
  1492. dev_err(&info->client->dev, "Failed to set charger to its default values\n");
  1493. #if IS_ENABLED(CONFIG_USB_PHY)
  1494. if (info->nb.notifier_call)
  1495. usb_unregister_notifier(info->usb_phy, &info->nb);
  1496. #endif
  1497. cancel_delayed_work_sync(&info->pwr_rdy_work);
  1498. cancel_delayed_work_sync(&info->max_charging_time_work);
  1499. cancel_delayed_work_sync(&info->batt_presence_work);
  1500. return ret;
  1501. }
  1502. static const struct i2c_device_id rt9455_i2c_id_table[] = {
  1503. { RT9455_DRIVER_NAME, 0 },
  1504. { },
  1505. };
  1506. MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
  1507. static const struct of_device_id rt9455_of_match[] = {
  1508. { .compatible = "richtek,rt9455", },
  1509. { },
  1510. };
  1511. MODULE_DEVICE_TABLE(of, rt9455_of_match);
  1512. static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
  1513. { "RT945500", 0 },
  1514. { }
  1515. };
  1516. MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
  1517. static struct i2c_driver rt9455_driver = {
  1518. .probe = rt9455_probe,
  1519. .remove = rt9455_remove,
  1520. .id_table = rt9455_i2c_id_table,
  1521. .driver = {
  1522. .name = RT9455_DRIVER_NAME,
  1523. .of_match_table = of_match_ptr(rt9455_of_match),
  1524. .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match),
  1525. },
  1526. };
  1527. module_i2c_driver(rt9455_driver);
  1528. MODULE_LICENSE("GPL");
  1529. MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
  1530. MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");