ab8500_charger.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2012
  3. *
  4. * Charger driver for AB8500
  5. *
  6. * License Terms: GNU General Public License v2
  7. * Author:
  8. * Johan Palsson <johan.palsson@stericsson.com>
  9. * Karl Komierowski <karl.komierowski@stericsson.com>
  10. * Arun R Murthy <arun.murthy@stericsson.com>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/completion.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/err.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/kobject.h>
  25. #include <linux/of.h>
  26. #include <linux/mfd/core.h>
  27. #include <linux/mfd/abx500/ab8500.h>
  28. #include <linux/mfd/abx500.h>
  29. #include <linux/mfd/abx500/ab8500-bm.h>
  30. #include <linux/mfd/abx500/ab8500-gpadc.h>
  31. #include <linux/mfd/abx500/ux500_chargalg.h>
  32. #include <linux/usb/otg.h>
  33. #include <linux/mutex.h>
  34. /* Charger constants */
  35. #define NO_PW_CONN 0
  36. #define AC_PW_CONN 1
  37. #define USB_PW_CONN 2
  38. #define MAIN_WDOG_ENA 0x01
  39. #define MAIN_WDOG_KICK 0x02
  40. #define MAIN_WDOG_DIS 0x00
  41. #define CHARG_WD_KICK 0x01
  42. #define MAIN_CH_ENA 0x01
  43. #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
  44. #define USB_CH_ENA 0x01
  45. #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
  46. #define MAIN_CH_DET 0x01
  47. #define MAIN_CH_CV_ON 0x04
  48. #define USB_CH_CV_ON 0x08
  49. #define VBUS_DET_DBNC100 0x02
  50. #define VBUS_DET_DBNC1 0x01
  51. #define OTP_ENABLE_WD 0x01
  52. #define MAIN_CH_INPUT_CURR_SHIFT 4
  53. #define VBUS_IN_CURR_LIM_SHIFT 4
  54. #define LED_INDICATOR_PWM_ENA 0x01
  55. #define LED_INDICATOR_PWM_DIS 0x00
  56. #define LED_IND_CUR_5MA 0x04
  57. #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
  58. /* HW failure constants */
  59. #define MAIN_CH_TH_PROT 0x02
  60. #define VBUS_CH_NOK 0x08
  61. #define USB_CH_TH_PROT 0x02
  62. #define VBUS_OVV_TH 0x01
  63. #define MAIN_CH_NOK 0x01
  64. #define VBUS_DET 0x80
  65. #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
  66. #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
  67. #define USB_CH_VBUSDROP 0x40
  68. #define USB_CH_VBUSDETDBNC 0x01
  69. /* UsbLineStatus register bit masks */
  70. #define AB8500_USB_LINK_STATUS 0x78
  71. #define AB8500_STD_HOST_SUSP 0x18
  72. /* Watchdog timeout constant */
  73. #define WD_TIMER 0x30 /* 4min */
  74. #define WD_KICK_INTERVAL (60 * HZ)
  75. /* Lowest charger voltage is 3.39V -> 0x4E */
  76. #define LOW_VOLT_REG 0x4E
  77. /* Step up/down delay in us */
  78. #define STEP_UDELAY 1000
  79. #define CHARGER_STATUS_POLL 10 /* in ms */
  80. /* UsbLineStatus register - usb types */
  81. enum ab8500_charger_link_status {
  82. USB_STAT_NOT_CONFIGURED,
  83. USB_STAT_STD_HOST_NC,
  84. USB_STAT_STD_HOST_C_NS,
  85. USB_STAT_STD_HOST_C_S,
  86. USB_STAT_HOST_CHG_NM,
  87. USB_STAT_HOST_CHG_HS,
  88. USB_STAT_HOST_CHG_HS_CHIRP,
  89. USB_STAT_DEDICATED_CHG,
  90. USB_STAT_ACA_RID_A,
  91. USB_STAT_ACA_RID_B,
  92. USB_STAT_ACA_RID_C_NM,
  93. USB_STAT_ACA_RID_C_HS,
  94. USB_STAT_ACA_RID_C_HS_CHIRP,
  95. USB_STAT_HM_IDGND,
  96. USB_STAT_RESERVED,
  97. USB_STAT_NOT_VALID_LINK,
  98. };
  99. enum ab8500_usb_state {
  100. AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
  101. AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
  102. AB8500_BM_USB_STATE_CONFIGURED,
  103. AB8500_BM_USB_STATE_SUSPEND,
  104. AB8500_BM_USB_STATE_RESUME,
  105. AB8500_BM_USB_STATE_MAX,
  106. };
  107. /* VBUS input current limits supported in AB8500 in mA */
  108. #define USB_CH_IP_CUR_LVL_0P05 50
  109. #define USB_CH_IP_CUR_LVL_0P09 98
  110. #define USB_CH_IP_CUR_LVL_0P19 193
  111. #define USB_CH_IP_CUR_LVL_0P29 290
  112. #define USB_CH_IP_CUR_LVL_0P38 380
  113. #define USB_CH_IP_CUR_LVL_0P45 450
  114. #define USB_CH_IP_CUR_LVL_0P5 500
  115. #define USB_CH_IP_CUR_LVL_0P6 600
  116. #define USB_CH_IP_CUR_LVL_0P7 700
  117. #define USB_CH_IP_CUR_LVL_0P8 800
  118. #define USB_CH_IP_CUR_LVL_0P9 900
  119. #define USB_CH_IP_CUR_LVL_1P0 1000
  120. #define USB_CH_IP_CUR_LVL_1P1 1100
  121. #define USB_CH_IP_CUR_LVL_1P3 1300
  122. #define USB_CH_IP_CUR_LVL_1P4 1400
  123. #define USB_CH_IP_CUR_LVL_1P5 1500
  124. #define VBAT_TRESH_IP_CUR_RED 3800
  125. #define to_ab8500_charger_usb_device_info(x) container_of((x), \
  126. struct ab8500_charger, usb_chg)
  127. #define to_ab8500_charger_ac_device_info(x) container_of((x), \
  128. struct ab8500_charger, ac_chg)
  129. /**
  130. * struct ab8500_charger_interrupts - ab8500 interupts
  131. * @name: name of the interrupt
  132. * @isr function pointer to the isr
  133. */
  134. struct ab8500_charger_interrupts {
  135. char *name;
  136. irqreturn_t (*isr)(int irq, void *data);
  137. };
  138. struct ab8500_charger_info {
  139. int charger_connected;
  140. int charger_online;
  141. int charger_voltage;
  142. int cv_active;
  143. bool wd_expired;
  144. };
  145. struct ab8500_charger_event_flags {
  146. bool mainextchnotok;
  147. bool main_thermal_prot;
  148. bool usb_thermal_prot;
  149. bool vbus_ovv;
  150. bool usbchargernotok;
  151. bool chgwdexp;
  152. bool vbus_collapse;
  153. };
  154. struct ab8500_charger_usb_state {
  155. bool usb_changed;
  156. int usb_current;
  157. enum ab8500_usb_state state;
  158. spinlock_t usb_lock;
  159. };
  160. /**
  161. * struct ab8500_charger - ab8500 Charger device information
  162. * @dev: Pointer to the structure device
  163. * @max_usb_in_curr: Max USB charger input current
  164. * @vbus_detected: VBUS detected
  165. * @vbus_detected_start:
  166. * VBUS detected during startup
  167. * @ac_conn: This will be true when the AC charger has been plugged
  168. * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
  169. * charger is enabled
  170. * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
  171. * charger is enabled
  172. * @vbat Battery voltage
  173. * @old_vbat Previously measured battery voltage
  174. * @autopower Indicate if we should have automatic pwron after pwrloss
  175. * @autopower_cfg platform specific power config support for "pwron after pwrloss"
  176. * @parent: Pointer to the struct ab8500
  177. * @gpadc: Pointer to the struct gpadc
  178. * @bm: Platform specific battery management information
  179. * @flags: Structure for information about events triggered
  180. * @usb_state: Structure for usb stack information
  181. * @ac_chg: AC charger power supply
  182. * @usb_chg: USB charger power supply
  183. * @ac: Structure that holds the AC charger properties
  184. * @usb: Structure that holds the USB charger properties
  185. * @regu: Pointer to the struct regulator
  186. * @charger_wq: Work queue for the IRQs and checking HW state
  187. * @check_vbat_work Work for checking vbat threshold to adjust vbus current
  188. * @check_hw_failure_work: Work for checking HW state
  189. * @check_usbchgnotok_work: Work for checking USB charger not ok status
  190. * @kick_wd_work: Work for kicking the charger watchdog in case
  191. * of ABB rev 1.* due to the watchog logic bug
  192. * @ac_charger_attached_work: Work for checking if AC charger is still
  193. * connected
  194. * @usb_charger_attached_work: Work for checking if USB charger is still
  195. * connected
  196. * @ac_work: Work for checking AC charger connection
  197. * @detect_usb_type_work: Work for detecting the USB type connected
  198. * @usb_link_status_work: Work for checking the new USB link status
  199. * @usb_state_changed_work: Work for checking USB state
  200. * @check_main_thermal_prot_work:
  201. * Work for checking Main thermal status
  202. * @check_usb_thermal_prot_work:
  203. * Work for checking USB thermal status
  204. * @charger_attached_mutex: For controlling the wakelock
  205. */
  206. struct ab8500_charger {
  207. struct device *dev;
  208. int max_usb_in_curr;
  209. bool vbus_detected;
  210. bool vbus_detected_start;
  211. bool ac_conn;
  212. bool vddadc_en_ac;
  213. bool vddadc_en_usb;
  214. int vbat;
  215. int old_vbat;
  216. bool autopower;
  217. bool autopower_cfg;
  218. struct ab8500 *parent;
  219. struct ab8500_gpadc *gpadc;
  220. struct abx500_bm_data *bm;
  221. struct ab8500_charger_event_flags flags;
  222. struct ab8500_charger_usb_state usb_state;
  223. struct ux500_charger ac_chg;
  224. struct ux500_charger usb_chg;
  225. struct ab8500_charger_info ac;
  226. struct ab8500_charger_info usb;
  227. struct regulator *regu;
  228. struct workqueue_struct *charger_wq;
  229. struct delayed_work check_vbat_work;
  230. struct delayed_work check_hw_failure_work;
  231. struct delayed_work check_usbchgnotok_work;
  232. struct delayed_work kick_wd_work;
  233. struct delayed_work ac_charger_attached_work;
  234. struct delayed_work usb_charger_attached_work;
  235. struct work_struct ac_work;
  236. struct work_struct detect_usb_type_work;
  237. struct work_struct usb_link_status_work;
  238. struct work_struct usb_state_changed_work;
  239. struct work_struct check_main_thermal_prot_work;
  240. struct work_struct check_usb_thermal_prot_work;
  241. struct usb_phy *usb_phy;
  242. struct notifier_block nb;
  243. struct mutex charger_attached_mutex;
  244. };
  245. /* AC properties */
  246. static enum power_supply_property ab8500_charger_ac_props[] = {
  247. POWER_SUPPLY_PROP_HEALTH,
  248. POWER_SUPPLY_PROP_PRESENT,
  249. POWER_SUPPLY_PROP_ONLINE,
  250. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  251. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  252. POWER_SUPPLY_PROP_CURRENT_NOW,
  253. };
  254. /* USB properties */
  255. static enum power_supply_property ab8500_charger_usb_props[] = {
  256. POWER_SUPPLY_PROP_HEALTH,
  257. POWER_SUPPLY_PROP_CURRENT_AVG,
  258. POWER_SUPPLY_PROP_PRESENT,
  259. POWER_SUPPLY_PROP_ONLINE,
  260. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  261. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  262. POWER_SUPPLY_PROP_CURRENT_NOW,
  263. };
  264. /*
  265. * Function for enabling and disabling sw fallback mode
  266. * should always be disabled when no charger is connected.
  267. */
  268. static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
  269. bool fallback)
  270. {
  271. u8 reg;
  272. int ret;
  273. dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
  274. /* read the register containing fallback bit */
  275. ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, &reg);
  276. if (ret) {
  277. dev_err(di->dev, "%d write failed\n", __LINE__);
  278. return;
  279. }
  280. /* enable the OPT emulation registers */
  281. ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
  282. if (ret) {
  283. dev_err(di->dev, "%d write failed\n", __LINE__);
  284. return;
  285. }
  286. if (fallback)
  287. reg |= 0x8;
  288. else
  289. reg &= ~0x8;
  290. /* write back the changed fallback bit value to register */
  291. ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
  292. if (ret) {
  293. dev_err(di->dev, "%d write failed\n", __LINE__);
  294. return;
  295. }
  296. /* disable the set OTP registers again */
  297. ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
  298. if (ret) {
  299. dev_err(di->dev, "%d write failed\n", __LINE__);
  300. return;
  301. }
  302. }
  303. /**
  304. * ab8500_power_supply_changed - a wrapper with local extentions for
  305. * power_supply_changed
  306. * @di: pointer to the ab8500_charger structure
  307. * @psy: pointer to power_supply_that have changed.
  308. *
  309. */
  310. static void ab8500_power_supply_changed(struct ab8500_charger *di,
  311. struct power_supply *psy)
  312. {
  313. if (di->autopower_cfg) {
  314. if (!di->usb.charger_connected &&
  315. !di->ac.charger_connected &&
  316. di->autopower) {
  317. di->autopower = false;
  318. ab8500_enable_disable_sw_fallback(di, false);
  319. } else if (!di->autopower &&
  320. (di->ac.charger_connected ||
  321. di->usb.charger_connected)) {
  322. di->autopower = true;
  323. ab8500_enable_disable_sw_fallback(di, true);
  324. }
  325. }
  326. power_supply_changed(psy);
  327. }
  328. static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
  329. bool connected)
  330. {
  331. if (connected != di->usb.charger_connected) {
  332. dev_dbg(di->dev, "USB connected:%i\n", connected);
  333. di->usb.charger_connected = connected;
  334. sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
  335. if (connected) {
  336. mutex_lock(&di->charger_attached_mutex);
  337. mutex_unlock(&di->charger_attached_mutex);
  338. queue_delayed_work(di->charger_wq,
  339. &di->usb_charger_attached_work,
  340. HZ);
  341. } else {
  342. cancel_delayed_work_sync(&di->usb_charger_attached_work);
  343. mutex_lock(&di->charger_attached_mutex);
  344. mutex_unlock(&di->charger_attached_mutex);
  345. }
  346. }
  347. }
  348. /**
  349. * ab8500_charger_get_ac_voltage() - get ac charger voltage
  350. * @di: pointer to the ab8500_charger structure
  351. *
  352. * Returns ac charger voltage (on success)
  353. */
  354. static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
  355. {
  356. int vch;
  357. /* Only measure voltage if the charger is connected */
  358. if (di->ac.charger_connected) {
  359. vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
  360. if (vch < 0)
  361. dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
  362. } else {
  363. vch = 0;
  364. }
  365. return vch;
  366. }
  367. /**
  368. * ab8500_charger_ac_cv() - check if the main charger is in CV mode
  369. * @di: pointer to the ab8500_charger structure
  370. *
  371. * Returns ac charger CV mode (on success) else error code
  372. */
  373. static int ab8500_charger_ac_cv(struct ab8500_charger *di)
  374. {
  375. u8 val;
  376. int ret = 0;
  377. /* Only check CV mode if the charger is online */
  378. if (di->ac.charger_online) {
  379. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  380. AB8500_CH_STATUS1_REG, &val);
  381. if (ret < 0) {
  382. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  383. return 0;
  384. }
  385. if (val & MAIN_CH_CV_ON)
  386. ret = 1;
  387. else
  388. ret = 0;
  389. }
  390. return ret;
  391. }
  392. /**
  393. * ab8500_charger_get_vbus_voltage() - get vbus voltage
  394. * @di: pointer to the ab8500_charger structure
  395. *
  396. * This function returns the vbus voltage.
  397. * Returns vbus voltage (on success)
  398. */
  399. static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
  400. {
  401. int vch;
  402. /* Only measure voltage if the charger is connected */
  403. if (di->usb.charger_connected) {
  404. vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
  405. if (vch < 0)
  406. dev_err(di->dev, "%s gpadc conv failed\n", __func__);
  407. } else {
  408. vch = 0;
  409. }
  410. return vch;
  411. }
  412. /**
  413. * ab8500_charger_get_usb_current() - get usb charger current
  414. * @di: pointer to the ab8500_charger structure
  415. *
  416. * This function returns the usb charger current.
  417. * Returns usb current (on success) and error code on failure
  418. */
  419. static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
  420. {
  421. int ich;
  422. /* Only measure current if the charger is online */
  423. if (di->usb.charger_online) {
  424. ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
  425. if (ich < 0)
  426. dev_err(di->dev, "%s gpadc conv failed\n", __func__);
  427. } else {
  428. ich = 0;
  429. }
  430. return ich;
  431. }
  432. /**
  433. * ab8500_charger_get_ac_current() - get ac charger current
  434. * @di: pointer to the ab8500_charger structure
  435. *
  436. * This function returns the ac charger current.
  437. * Returns ac current (on success) and error code on failure.
  438. */
  439. static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
  440. {
  441. int ich;
  442. /* Only measure current if the charger is online */
  443. if (di->ac.charger_online) {
  444. ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
  445. if (ich < 0)
  446. dev_err(di->dev, "%s gpadc conv failed\n", __func__);
  447. } else {
  448. ich = 0;
  449. }
  450. return ich;
  451. }
  452. /**
  453. * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
  454. * @di: pointer to the ab8500_charger structure
  455. *
  456. * Returns ac charger CV mode (on success) else error code
  457. */
  458. static int ab8500_charger_usb_cv(struct ab8500_charger *di)
  459. {
  460. int ret;
  461. u8 val;
  462. /* Only check CV mode if the charger is online */
  463. if (di->usb.charger_online) {
  464. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  465. AB8500_CH_USBCH_STAT1_REG, &val);
  466. if (ret < 0) {
  467. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  468. return 0;
  469. }
  470. if (val & USB_CH_CV_ON)
  471. ret = 1;
  472. else
  473. ret = 0;
  474. } else {
  475. ret = 0;
  476. }
  477. return ret;
  478. }
  479. /**
  480. * ab8500_charger_detect_chargers() - Detect the connected chargers
  481. * @di: pointer to the ab8500_charger structure
  482. *
  483. * Returns the type of charger connected.
  484. * For USB it will not mean we can actually charge from it
  485. * but that there is a USB cable connected that we have to
  486. * identify. This is used during startup when we don't get
  487. * interrupts of the charger detection
  488. *
  489. * Returns an integer value, that means,
  490. * NO_PW_CONN no power supply is connected
  491. * AC_PW_CONN if the AC power supply is connected
  492. * USB_PW_CONN if the USB power supply is connected
  493. * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
  494. */
  495. static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
  496. {
  497. int result = NO_PW_CONN;
  498. int ret;
  499. u8 val;
  500. /* Check for AC charger */
  501. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  502. AB8500_CH_STATUS1_REG, &val);
  503. if (ret < 0) {
  504. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  505. return ret;
  506. }
  507. if (val & MAIN_CH_DET)
  508. result = AC_PW_CONN;
  509. /* Check for USB charger */
  510. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  511. AB8500_CH_USBCH_STAT1_REG, &val);
  512. if (ret < 0) {
  513. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  514. return ret;
  515. }
  516. if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
  517. result |= USB_PW_CONN;
  518. return result;
  519. }
  520. /**
  521. * ab8500_charger_max_usb_curr() - get the max curr for the USB type
  522. * @di: pointer to the ab8500_charger structure
  523. * @link_status: the identified USB type
  524. *
  525. * Get the maximum current that is allowed to be drawn from the host
  526. * based on the USB type.
  527. * Returns error code in case of failure else 0 on success
  528. */
  529. static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
  530. enum ab8500_charger_link_status link_status)
  531. {
  532. int ret = 0;
  533. switch (link_status) {
  534. case USB_STAT_STD_HOST_NC:
  535. case USB_STAT_STD_HOST_C_NS:
  536. case USB_STAT_STD_HOST_C_S:
  537. dev_dbg(di->dev, "USB Type - Standard host is "
  538. "detected through USB driver\n");
  539. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
  540. break;
  541. case USB_STAT_HOST_CHG_HS_CHIRP:
  542. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
  543. break;
  544. case USB_STAT_HOST_CHG_HS:
  545. case USB_STAT_ACA_RID_C_HS:
  546. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
  547. break;
  548. case USB_STAT_ACA_RID_A:
  549. /*
  550. * Dedicated charger level minus maximum current accessory
  551. * can consume (300mA). Closest level is 1100mA
  552. */
  553. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
  554. break;
  555. case USB_STAT_ACA_RID_B:
  556. /*
  557. * Dedicated charger level minus 120mA (20mA for ACA and
  558. * 100mA for potential accessory). Closest level is 1300mA
  559. */
  560. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
  561. break;
  562. case USB_STAT_DEDICATED_CHG:
  563. case USB_STAT_HOST_CHG_NM:
  564. case USB_STAT_ACA_RID_C_HS_CHIRP:
  565. case USB_STAT_ACA_RID_C_NM:
  566. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
  567. break;
  568. case USB_STAT_RESERVED:
  569. /*
  570. * This state is used to indicate that VBUS has dropped below
  571. * the detection level 4 times in a row. This is due to the
  572. * charger output current is set to high making the charger
  573. * voltage collapse. This have to be propagated through to
  574. * chargalg. This is done using the property
  575. * POWER_SUPPLY_PROP_CURRENT_AVG = 1
  576. */
  577. di->flags.vbus_collapse = true;
  578. dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
  579. "VBUS has collapsed\n");
  580. ret = -1;
  581. break;
  582. case USB_STAT_HM_IDGND:
  583. case USB_STAT_NOT_CONFIGURED:
  584. case USB_STAT_NOT_VALID_LINK:
  585. dev_err(di->dev, "USB Type - Charging not allowed\n");
  586. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
  587. ret = -ENXIO;
  588. break;
  589. default:
  590. dev_err(di->dev, "USB Type - Unknown\n");
  591. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
  592. ret = -ENXIO;
  593. break;
  594. };
  595. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
  596. link_status, di->max_usb_in_curr);
  597. return ret;
  598. }
  599. /**
  600. * ab8500_charger_read_usb_type() - read the type of usb connected
  601. * @di: pointer to the ab8500_charger structure
  602. *
  603. * Detect the type of the plugged USB
  604. * Returns error code in case of failure else 0 on success
  605. */
  606. static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
  607. {
  608. int ret;
  609. u8 val;
  610. ret = abx500_get_register_interruptible(di->dev,
  611. AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
  612. if (ret < 0) {
  613. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  614. return ret;
  615. }
  616. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  617. AB8500_USB_LINE_STAT_REG, &val);
  618. if (ret < 0) {
  619. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  620. return ret;
  621. }
  622. /* get the USB type */
  623. val = (val & AB8500_USB_LINK_STATUS) >> 3;
  624. ret = ab8500_charger_max_usb_curr(di,
  625. (enum ab8500_charger_link_status) val);
  626. return ret;
  627. }
  628. /**
  629. * ab8500_charger_detect_usb_type() - get the type of usb connected
  630. * @di: pointer to the ab8500_charger structure
  631. *
  632. * Detect the type of the plugged USB
  633. * Returns error code in case of failure else 0 on success
  634. */
  635. static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
  636. {
  637. int i, ret;
  638. u8 val;
  639. /*
  640. * On getting the VBUS rising edge detect interrupt there
  641. * is a 250ms delay after which the register UsbLineStatus
  642. * is filled with valid data.
  643. */
  644. for (i = 0; i < 10; i++) {
  645. msleep(250);
  646. ret = abx500_get_register_interruptible(di->dev,
  647. AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
  648. &val);
  649. if (ret < 0) {
  650. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  651. return ret;
  652. }
  653. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  654. AB8500_USB_LINE_STAT_REG, &val);
  655. if (ret < 0) {
  656. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  657. return ret;
  658. }
  659. /*
  660. * Until the IT source register is read the UsbLineStatus
  661. * register is not updated, hence doing the same
  662. * Revisit this:
  663. */
  664. /* get the USB type */
  665. val = (val & AB8500_USB_LINK_STATUS) >> 3;
  666. if (val)
  667. break;
  668. }
  669. ret = ab8500_charger_max_usb_curr(di,
  670. (enum ab8500_charger_link_status) val);
  671. return ret;
  672. }
  673. /*
  674. * This array maps the raw hex value to charger voltage used by the AB8500
  675. * Values taken from the UM0836
  676. */
  677. static int ab8500_charger_voltage_map[] = {
  678. 3500 ,
  679. 3525 ,
  680. 3550 ,
  681. 3575 ,
  682. 3600 ,
  683. 3625 ,
  684. 3650 ,
  685. 3675 ,
  686. 3700 ,
  687. 3725 ,
  688. 3750 ,
  689. 3775 ,
  690. 3800 ,
  691. 3825 ,
  692. 3850 ,
  693. 3875 ,
  694. 3900 ,
  695. 3925 ,
  696. 3950 ,
  697. 3975 ,
  698. 4000 ,
  699. 4025 ,
  700. 4050 ,
  701. 4060 ,
  702. 4070 ,
  703. 4080 ,
  704. 4090 ,
  705. 4100 ,
  706. 4110 ,
  707. 4120 ,
  708. 4130 ,
  709. 4140 ,
  710. 4150 ,
  711. 4160 ,
  712. 4170 ,
  713. 4180 ,
  714. 4190 ,
  715. 4200 ,
  716. 4210 ,
  717. 4220 ,
  718. 4230 ,
  719. 4240 ,
  720. 4250 ,
  721. 4260 ,
  722. 4270 ,
  723. 4280 ,
  724. 4290 ,
  725. 4300 ,
  726. 4310 ,
  727. 4320 ,
  728. 4330 ,
  729. 4340 ,
  730. 4350 ,
  731. 4360 ,
  732. 4370 ,
  733. 4380 ,
  734. 4390 ,
  735. 4400 ,
  736. 4410 ,
  737. 4420 ,
  738. 4430 ,
  739. 4440 ,
  740. 4450 ,
  741. 4460 ,
  742. 4470 ,
  743. 4480 ,
  744. 4490 ,
  745. 4500 ,
  746. 4510 ,
  747. 4520 ,
  748. 4530 ,
  749. 4540 ,
  750. 4550 ,
  751. 4560 ,
  752. 4570 ,
  753. 4580 ,
  754. 4590 ,
  755. 4600 ,
  756. };
  757. /*
  758. * This array maps the raw hex value to charger current used by the AB8500
  759. * Values taken from the UM0836
  760. */
  761. static int ab8500_charger_current_map[] = {
  762. 100 ,
  763. 200 ,
  764. 300 ,
  765. 400 ,
  766. 500 ,
  767. 600 ,
  768. 700 ,
  769. 800 ,
  770. 900 ,
  771. 1000 ,
  772. 1100 ,
  773. 1200 ,
  774. 1300 ,
  775. 1400 ,
  776. 1500 ,
  777. };
  778. /*
  779. * This array maps the raw hex value to VBUS input current used by the AB8500
  780. * Values taken from the UM0836
  781. */
  782. static int ab8500_charger_vbus_in_curr_map[] = {
  783. USB_CH_IP_CUR_LVL_0P05,
  784. USB_CH_IP_CUR_LVL_0P09,
  785. USB_CH_IP_CUR_LVL_0P19,
  786. USB_CH_IP_CUR_LVL_0P29,
  787. USB_CH_IP_CUR_LVL_0P38,
  788. USB_CH_IP_CUR_LVL_0P45,
  789. USB_CH_IP_CUR_LVL_0P5,
  790. USB_CH_IP_CUR_LVL_0P6,
  791. USB_CH_IP_CUR_LVL_0P7,
  792. USB_CH_IP_CUR_LVL_0P8,
  793. USB_CH_IP_CUR_LVL_0P9,
  794. USB_CH_IP_CUR_LVL_1P0,
  795. USB_CH_IP_CUR_LVL_1P1,
  796. USB_CH_IP_CUR_LVL_1P3,
  797. USB_CH_IP_CUR_LVL_1P4,
  798. USB_CH_IP_CUR_LVL_1P5,
  799. };
  800. static int ab8500_voltage_to_regval(int voltage)
  801. {
  802. int i;
  803. /* Special case for voltage below 3.5V */
  804. if (voltage < ab8500_charger_voltage_map[0])
  805. return LOW_VOLT_REG;
  806. for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
  807. if (voltage < ab8500_charger_voltage_map[i])
  808. return i - 1;
  809. }
  810. /* If not last element, return error */
  811. i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
  812. if (voltage == ab8500_charger_voltage_map[i])
  813. return i;
  814. else
  815. return -1;
  816. }
  817. static int ab8500_current_to_regval(int curr)
  818. {
  819. int i;
  820. if (curr < ab8500_charger_current_map[0])
  821. return 0;
  822. for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
  823. if (curr < ab8500_charger_current_map[i])
  824. return i - 1;
  825. }
  826. /* If not last element, return error */
  827. i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
  828. if (curr == ab8500_charger_current_map[i])
  829. return i;
  830. else
  831. return -1;
  832. }
  833. static int ab8500_vbus_in_curr_to_regval(int curr)
  834. {
  835. int i;
  836. if (curr < ab8500_charger_vbus_in_curr_map[0])
  837. return 0;
  838. for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
  839. if (curr < ab8500_charger_vbus_in_curr_map[i])
  840. return i - 1;
  841. }
  842. /* If not last element, return error */
  843. i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
  844. if (curr == ab8500_charger_vbus_in_curr_map[i])
  845. return i;
  846. else
  847. return -1;
  848. }
  849. /**
  850. * ab8500_charger_get_usb_cur() - get usb current
  851. * @di: pointer to the ab8500_charger structre
  852. *
  853. * The usb stack provides the maximum current that can be drawn from
  854. * the standard usb host. This will be in mA.
  855. * This function converts current in mA to a value that can be written
  856. * to the register. Returns -1 if charging is not allowed
  857. */
  858. static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
  859. {
  860. switch (di->usb_state.usb_current) {
  861. case 100:
  862. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
  863. break;
  864. case 200:
  865. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
  866. break;
  867. case 300:
  868. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
  869. break;
  870. case 400:
  871. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
  872. break;
  873. case 500:
  874. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
  875. break;
  876. default:
  877. di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
  878. return -1;
  879. break;
  880. };
  881. return 0;
  882. }
  883. /**
  884. * ab8500_charger_set_current() - set charger current
  885. * @di: pointer to the ab8500_charger structure
  886. * @ich: charger current, in mA
  887. * @reg: select what charger register to set
  888. *
  889. * Set charger current.
  890. * There is no state machine in the AB to step up/down the charger
  891. * current to avoid dips and spikes on MAIN, VBUS and VBAT when
  892. * charging is started. Instead we need to implement
  893. * this charger current step-up/down here.
  894. * Returns error code in case of failure else 0(on success)
  895. */
  896. static int ab8500_charger_set_current(struct ab8500_charger *di,
  897. int ich, int reg)
  898. {
  899. int ret, i;
  900. int curr_index, prev_curr_index, shift_value;
  901. u8 reg_value;
  902. switch (reg) {
  903. case AB8500_MCH_IPT_CURLVL_REG:
  904. shift_value = MAIN_CH_INPUT_CURR_SHIFT;
  905. curr_index = ab8500_current_to_regval(ich);
  906. break;
  907. case AB8500_USBCH_IPT_CRNTLVL_REG:
  908. shift_value = VBUS_IN_CURR_LIM_SHIFT;
  909. curr_index = ab8500_vbus_in_curr_to_regval(ich);
  910. break;
  911. case AB8500_CH_OPT_CRNTLVL_REG:
  912. shift_value = 0;
  913. curr_index = ab8500_current_to_regval(ich);
  914. break;
  915. default:
  916. dev_err(di->dev, "%s current register not valid\n", __func__);
  917. return -ENXIO;
  918. }
  919. if (curr_index < 0) {
  920. dev_err(di->dev, "requested current limit out-of-range\n");
  921. return -ENXIO;
  922. }
  923. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  924. reg, &reg_value);
  925. if (ret < 0) {
  926. dev_err(di->dev, "%s read failed\n", __func__);
  927. return ret;
  928. }
  929. prev_curr_index = (reg_value >> shift_value);
  930. /* only update current if it's been changed */
  931. if (prev_curr_index == curr_index)
  932. return 0;
  933. dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
  934. __func__, ich, reg);
  935. if (prev_curr_index > curr_index) {
  936. for (i = prev_curr_index - 1; i >= curr_index; i--) {
  937. ret = abx500_set_register_interruptible(di->dev,
  938. AB8500_CHARGER, reg, (u8) i << shift_value);
  939. if (ret) {
  940. dev_err(di->dev, "%s write failed\n", __func__);
  941. return ret;
  942. }
  943. usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
  944. }
  945. } else {
  946. for (i = prev_curr_index + 1; i <= curr_index; i++) {
  947. ret = abx500_set_register_interruptible(di->dev,
  948. AB8500_CHARGER, reg, (u8) i << shift_value);
  949. if (ret) {
  950. dev_err(di->dev, "%s write failed\n", __func__);
  951. return ret;
  952. }
  953. usleep_range(STEP_UDELAY, STEP_UDELAY * 2);
  954. }
  955. }
  956. return ret;
  957. }
  958. /**
  959. * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
  960. * @di: pointer to the ab8500_charger structure
  961. * @ich_in: charger input current limit
  962. *
  963. * Sets the current that can be drawn from the USB host
  964. * Returns error code in case of failure else 0(on success)
  965. */
  966. static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
  967. int ich_in)
  968. {
  969. int min_value;
  970. /* We should always use to lowest current limit */
  971. min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
  972. switch (min_value) {
  973. case 100:
  974. if (di->vbat < VBAT_TRESH_IP_CUR_RED)
  975. min_value = USB_CH_IP_CUR_LVL_0P05;
  976. break;
  977. case 500:
  978. if (di->vbat < VBAT_TRESH_IP_CUR_RED)
  979. min_value = USB_CH_IP_CUR_LVL_0P45;
  980. break;
  981. default:
  982. break;
  983. }
  984. return ab8500_charger_set_current(di, min_value,
  985. AB8500_USBCH_IPT_CRNTLVL_REG);
  986. }
  987. /**
  988. * ab8500_charger_set_main_in_curr() - set main charger input current
  989. * @di: pointer to the ab8500_charger structure
  990. * @ich_in: input charger current, in mA
  991. *
  992. * Set main charger input current.
  993. * Returns error code in case of failure else 0(on success)
  994. */
  995. static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
  996. int ich_in)
  997. {
  998. return ab8500_charger_set_current(di, ich_in,
  999. AB8500_MCH_IPT_CURLVL_REG);
  1000. }
  1001. /**
  1002. * ab8500_charger_set_output_curr() - set charger output current
  1003. * @di: pointer to the ab8500_charger structure
  1004. * @ich_out: output charger current, in mA
  1005. *
  1006. * Set charger output current.
  1007. * Returns error code in case of failure else 0(on success)
  1008. */
  1009. static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
  1010. int ich_out)
  1011. {
  1012. return ab8500_charger_set_current(di, ich_out,
  1013. AB8500_CH_OPT_CRNTLVL_REG);
  1014. }
  1015. /**
  1016. * ab8500_charger_led_en() - turn on/off chargign led
  1017. * @di: pointer to the ab8500_charger structure
  1018. * @on: flag to turn on/off the chargign led
  1019. *
  1020. * Power ON/OFF charging LED indication
  1021. * Returns error code in case of failure else 0(on success)
  1022. */
  1023. static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
  1024. {
  1025. int ret;
  1026. if (on) {
  1027. /* Power ON charging LED indicator, set LED current to 5mA */
  1028. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1029. AB8500_LED_INDICATOR_PWM_CTRL,
  1030. (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
  1031. if (ret) {
  1032. dev_err(di->dev, "Power ON LED failed\n");
  1033. return ret;
  1034. }
  1035. /* LED indicator PWM duty cycle 252/256 */
  1036. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1037. AB8500_LED_INDICATOR_PWM_DUTY,
  1038. LED_INDICATOR_PWM_DUTY_252_256);
  1039. if (ret) {
  1040. dev_err(di->dev, "Set LED PWM duty cycle failed\n");
  1041. return ret;
  1042. }
  1043. } else {
  1044. /* Power off charging LED indicator */
  1045. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1046. AB8500_LED_INDICATOR_PWM_CTRL,
  1047. LED_INDICATOR_PWM_DIS);
  1048. if (ret) {
  1049. dev_err(di->dev, "Power-off LED failed\n");
  1050. return ret;
  1051. }
  1052. }
  1053. return ret;
  1054. }
  1055. /**
  1056. * ab8500_charger_ac_en() - enable or disable ac charging
  1057. * @di: pointer to the ab8500_charger structure
  1058. * @enable: enable/disable flag
  1059. * @vset: charging voltage
  1060. * @iset: charging current
  1061. *
  1062. * Enable/Disable AC/Mains charging and turns on/off the charging led
  1063. * respectively.
  1064. **/
  1065. static int ab8500_charger_ac_en(struct ux500_charger *charger,
  1066. int enable, int vset, int iset)
  1067. {
  1068. int ret;
  1069. int volt_index;
  1070. int curr_index;
  1071. int input_curr_index;
  1072. u8 overshoot = 0;
  1073. struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
  1074. if (enable) {
  1075. /* Check if AC is connected */
  1076. if (!di->ac.charger_connected) {
  1077. dev_err(di->dev, "AC charger not connected\n");
  1078. return -ENXIO;
  1079. }
  1080. /* Enable AC charging */
  1081. dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
  1082. /*
  1083. * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
  1084. * will be triggered everytime we enable the VDD ADC supply.
  1085. * This will turn off charging for a short while.
  1086. * It can be avoided by having the supply on when
  1087. * there is a charger enabled. Normally the VDD ADC supply
  1088. * is enabled everytime a GPADC conversion is triggered. We will
  1089. * force it to be enabled from this driver to have
  1090. * the GPADC module independant of the AB8500 chargers
  1091. */
  1092. if (!di->vddadc_en_ac) {
  1093. regulator_enable(di->regu);
  1094. di->vddadc_en_ac = true;
  1095. }
  1096. /* Check if the requested voltage or current is valid */
  1097. volt_index = ab8500_voltage_to_regval(vset);
  1098. curr_index = ab8500_current_to_regval(iset);
  1099. input_curr_index = ab8500_current_to_regval(
  1100. di->bm->chg_params->ac_curr_max);
  1101. if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
  1102. dev_err(di->dev,
  1103. "Charger voltage or current too high, "
  1104. "charging not started\n");
  1105. return -ENXIO;
  1106. }
  1107. /* ChVoltLevel: maximum battery charging voltage */
  1108. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1109. AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
  1110. if (ret) {
  1111. dev_err(di->dev, "%s write failed\n", __func__);
  1112. return ret;
  1113. }
  1114. /* MainChInputCurr: current that can be drawn from the charger*/
  1115. ret = ab8500_charger_set_main_in_curr(di,
  1116. di->bm->chg_params->ac_curr_max);
  1117. if (ret) {
  1118. dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
  1119. __func__);
  1120. return ret;
  1121. }
  1122. /* ChOutputCurentLevel: protected output current */
  1123. ret = ab8500_charger_set_output_curr(di, iset);
  1124. if (ret) {
  1125. dev_err(di->dev, "%s "
  1126. "Failed to set ChOutputCurentLevel\n",
  1127. __func__);
  1128. return ret;
  1129. }
  1130. /* Check if VBAT overshoot control should be enabled */
  1131. if (!di->bm->enable_overshoot)
  1132. overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
  1133. /* Enable Main Charger */
  1134. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1135. AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
  1136. if (ret) {
  1137. dev_err(di->dev, "%s write failed\n", __func__);
  1138. return ret;
  1139. }
  1140. /* Power on charging LED indication */
  1141. ret = ab8500_charger_led_en(di, true);
  1142. if (ret < 0)
  1143. dev_err(di->dev, "failed to enable LED\n");
  1144. di->ac.charger_online = 1;
  1145. } else {
  1146. /* Disable AC charging */
  1147. if (is_ab8500_1p1_or_earlier(di->parent)) {
  1148. /*
  1149. * For ABB revision 1.0 and 1.1 there is a bug in the
  1150. * watchdog logic. That means we have to continously
  1151. * kick the charger watchdog even when no charger is
  1152. * connected. This is only valid once the AC charger
  1153. * has been enabled. This is a bug that is not handled
  1154. * by the algorithm and the watchdog have to be kicked
  1155. * by the charger driver when the AC charger
  1156. * is disabled
  1157. */
  1158. if (di->ac_conn) {
  1159. queue_delayed_work(di->charger_wq,
  1160. &di->kick_wd_work,
  1161. round_jiffies(WD_KICK_INTERVAL));
  1162. }
  1163. /*
  1164. * We can't turn off charging completely
  1165. * due to a bug in AB8500 cut1.
  1166. * If we do, charging will not start again.
  1167. * That is why we set the lowest voltage
  1168. * and current possible
  1169. */
  1170. ret = abx500_set_register_interruptible(di->dev,
  1171. AB8500_CHARGER,
  1172. AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
  1173. if (ret) {
  1174. dev_err(di->dev,
  1175. "%s write failed\n", __func__);
  1176. return ret;
  1177. }
  1178. ret = ab8500_charger_set_output_curr(di, 0);
  1179. if (ret) {
  1180. dev_err(di->dev, "%s "
  1181. "Failed to set ChOutputCurentLevel\n",
  1182. __func__);
  1183. return ret;
  1184. }
  1185. } else {
  1186. ret = abx500_set_register_interruptible(di->dev,
  1187. AB8500_CHARGER,
  1188. AB8500_MCH_CTRL1, 0);
  1189. if (ret) {
  1190. dev_err(di->dev,
  1191. "%s write failed\n", __func__);
  1192. return ret;
  1193. }
  1194. }
  1195. ret = ab8500_charger_led_en(di, false);
  1196. if (ret < 0)
  1197. dev_err(di->dev, "failed to disable LED\n");
  1198. di->ac.charger_online = 0;
  1199. di->ac.wd_expired = false;
  1200. /* Disable regulator if enabled */
  1201. if (di->vddadc_en_ac) {
  1202. regulator_disable(di->regu);
  1203. di->vddadc_en_ac = false;
  1204. }
  1205. dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
  1206. }
  1207. ab8500_power_supply_changed(di, &di->ac_chg.psy);
  1208. return ret;
  1209. }
  1210. /**
  1211. * ab8500_charger_usb_en() - enable usb charging
  1212. * @di: pointer to the ab8500_charger structure
  1213. * @enable: enable/disable flag
  1214. * @vset: charging voltage
  1215. * @ich_out: charger output current
  1216. *
  1217. * Enable/Disable USB charging and turns on/off the charging led respectively.
  1218. * Returns error code in case of failure else 0(on success)
  1219. */
  1220. static int ab8500_charger_usb_en(struct ux500_charger *charger,
  1221. int enable, int vset, int ich_out)
  1222. {
  1223. int ret;
  1224. int volt_index;
  1225. int curr_index;
  1226. u8 overshoot = 0;
  1227. struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
  1228. if (enable) {
  1229. /* Check if USB is connected */
  1230. if (!di->usb.charger_connected) {
  1231. dev_err(di->dev, "USB charger not connected\n");
  1232. return -ENXIO;
  1233. }
  1234. /*
  1235. * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
  1236. * will be triggered everytime we enable the VDD ADC supply.
  1237. * This will turn off charging for a short while.
  1238. * It can be avoided by having the supply on when
  1239. * there is a charger enabled. Normally the VDD ADC supply
  1240. * is enabled everytime a GPADC conversion is triggered. We will
  1241. * force it to be enabled from this driver to have
  1242. * the GPADC module independant of the AB8500 chargers
  1243. */
  1244. if (!di->vddadc_en_usb) {
  1245. regulator_enable(di->regu);
  1246. di->vddadc_en_usb = true;
  1247. }
  1248. /* Enable USB charging */
  1249. dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
  1250. /* Check if the requested voltage or current is valid */
  1251. volt_index = ab8500_voltage_to_regval(vset);
  1252. curr_index = ab8500_current_to_regval(ich_out);
  1253. if (volt_index < 0 || curr_index < 0) {
  1254. dev_err(di->dev,
  1255. "Charger voltage or current too high, "
  1256. "charging not started\n");
  1257. return -ENXIO;
  1258. }
  1259. /* ChVoltLevel: max voltage upto which battery can be charged */
  1260. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1261. AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
  1262. if (ret) {
  1263. dev_err(di->dev, "%s write failed\n", __func__);
  1264. return ret;
  1265. }
  1266. /* USBChInputCurr: current that can be drawn from the usb */
  1267. ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
  1268. if (ret) {
  1269. dev_err(di->dev, "setting USBChInputCurr failed\n");
  1270. return ret;
  1271. }
  1272. /* ChOutputCurentLevel: protected output current */
  1273. ret = ab8500_charger_set_output_curr(di, ich_out);
  1274. if (ret) {
  1275. dev_err(di->dev, "%s "
  1276. "Failed to set ChOutputCurentLevel\n",
  1277. __func__);
  1278. return ret;
  1279. }
  1280. /* Check if VBAT overshoot control should be enabled */
  1281. if (!di->bm->enable_overshoot)
  1282. overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
  1283. /* Enable USB Charger */
  1284. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1285. AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
  1286. if (ret) {
  1287. dev_err(di->dev, "%s write failed\n", __func__);
  1288. return ret;
  1289. }
  1290. /* If success power on charging LED indication */
  1291. ret = ab8500_charger_led_en(di, true);
  1292. if (ret < 0)
  1293. dev_err(di->dev, "failed to enable LED\n");
  1294. queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
  1295. di->usb.charger_online = 1;
  1296. } else {
  1297. /* Disable USB charging */
  1298. ret = abx500_set_register_interruptible(di->dev,
  1299. AB8500_CHARGER,
  1300. AB8500_USBCH_CTRL1_REG, 0);
  1301. if (ret) {
  1302. dev_err(di->dev,
  1303. "%s write failed\n", __func__);
  1304. return ret;
  1305. }
  1306. ret = ab8500_charger_led_en(di, false);
  1307. if (ret < 0)
  1308. dev_err(di->dev, "failed to disable LED\n");
  1309. di->usb.charger_online = 0;
  1310. di->usb.wd_expired = false;
  1311. /* Disable regulator if enabled */
  1312. if (di->vddadc_en_usb) {
  1313. regulator_disable(di->regu);
  1314. di->vddadc_en_usb = false;
  1315. }
  1316. dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
  1317. /* Cancel any pending Vbat check work */
  1318. if (delayed_work_pending(&di->check_vbat_work))
  1319. cancel_delayed_work(&di->check_vbat_work);
  1320. }
  1321. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1322. return ret;
  1323. }
  1324. /**
  1325. * ab8500_charger_watchdog_kick() - kick charger watchdog
  1326. * @di: pointer to the ab8500_charger structure
  1327. *
  1328. * Kick charger watchdog
  1329. * Returns error code in case of failure else 0(on success)
  1330. */
  1331. static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
  1332. {
  1333. int ret;
  1334. struct ab8500_charger *di;
  1335. if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
  1336. di = to_ab8500_charger_ac_device_info(charger);
  1337. else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
  1338. di = to_ab8500_charger_usb_device_info(charger);
  1339. else
  1340. return -ENXIO;
  1341. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1342. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  1343. if (ret)
  1344. dev_err(di->dev, "Failed to kick WD!\n");
  1345. return ret;
  1346. }
  1347. /**
  1348. * ab8500_charger_update_charger_current() - update charger current
  1349. * @di: pointer to the ab8500_charger structure
  1350. *
  1351. * Update the charger output current for the specified charger
  1352. * Returns error code in case of failure else 0(on success)
  1353. */
  1354. static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
  1355. int ich_out)
  1356. {
  1357. int ret;
  1358. struct ab8500_charger *di;
  1359. if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
  1360. di = to_ab8500_charger_ac_device_info(charger);
  1361. else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
  1362. di = to_ab8500_charger_usb_device_info(charger);
  1363. else
  1364. return -ENXIO;
  1365. ret = ab8500_charger_set_output_curr(di, ich_out);
  1366. if (ret) {
  1367. dev_err(di->dev, "%s "
  1368. "Failed to set ChOutputCurentLevel\n",
  1369. __func__);
  1370. return ret;
  1371. }
  1372. /* Reset the main and usb drop input current measurement counter */
  1373. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1374. AB8500_CHARGER_CTRL,
  1375. 0x1);
  1376. if (ret) {
  1377. dev_err(di->dev, "%s write failed\n", __func__);
  1378. return ret;
  1379. }
  1380. return ret;
  1381. }
  1382. static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
  1383. {
  1384. struct power_supply *psy;
  1385. struct power_supply *ext;
  1386. struct ab8500_charger *di;
  1387. union power_supply_propval ret;
  1388. int i, j;
  1389. bool psy_found = false;
  1390. struct ux500_charger *usb_chg;
  1391. usb_chg = (struct ux500_charger *)data;
  1392. psy = &usb_chg->psy;
  1393. di = to_ab8500_charger_usb_device_info(usb_chg);
  1394. ext = dev_get_drvdata(dev);
  1395. /* For all psy where the driver name appears in any supplied_to */
  1396. for (i = 0; i < ext->num_supplicants; i++) {
  1397. if (!strcmp(ext->supplied_to[i], psy->name))
  1398. psy_found = true;
  1399. }
  1400. if (!psy_found)
  1401. return 0;
  1402. /* Go through all properties for the psy */
  1403. for (j = 0; j < ext->num_properties; j++) {
  1404. enum power_supply_property prop;
  1405. prop = ext->properties[j];
  1406. if (ext->get_property(ext, prop, &ret))
  1407. continue;
  1408. switch (prop) {
  1409. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1410. switch (ext->type) {
  1411. case POWER_SUPPLY_TYPE_BATTERY:
  1412. di->vbat = ret.intval / 1000;
  1413. break;
  1414. default:
  1415. break;
  1416. }
  1417. break;
  1418. default:
  1419. break;
  1420. }
  1421. }
  1422. return 0;
  1423. }
  1424. /**
  1425. * ab8500_charger_check_vbat_work() - keep vbus current within spec
  1426. * @work pointer to the work_struct structure
  1427. *
  1428. * Due to a asic bug it is necessary to lower the input current to the vbus
  1429. * charger when charging with at some specific levels. This issue is only valid
  1430. * for below a certain battery voltage. This function makes sure that the
  1431. * the allowed current limit isn't exceeded.
  1432. */
  1433. static void ab8500_charger_check_vbat_work(struct work_struct *work)
  1434. {
  1435. int t = 10;
  1436. struct ab8500_charger *di = container_of(work,
  1437. struct ab8500_charger, check_vbat_work.work);
  1438. class_for_each_device(power_supply_class, NULL,
  1439. &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
  1440. /* First run old_vbat is 0. */
  1441. if (di->old_vbat == 0)
  1442. di->old_vbat = di->vbat;
  1443. if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
  1444. di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
  1445. (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
  1446. di->vbat > VBAT_TRESH_IP_CUR_RED))) {
  1447. dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
  1448. " old: %d\n", di->max_usb_in_curr, di->vbat,
  1449. di->old_vbat);
  1450. ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
  1451. power_supply_changed(&di->usb_chg.psy);
  1452. }
  1453. di->old_vbat = di->vbat;
  1454. /*
  1455. * No need to check the battery voltage every second when not close to
  1456. * the threshold.
  1457. */
  1458. if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
  1459. (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
  1460. t = 1;
  1461. queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
  1462. }
  1463. /**
  1464. * ab8500_charger_check_hw_failure_work() - check main charger failure
  1465. * @work: pointer to the work_struct structure
  1466. *
  1467. * Work queue function for checking the main charger status
  1468. */
  1469. static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
  1470. {
  1471. int ret;
  1472. u8 reg_value;
  1473. struct ab8500_charger *di = container_of(work,
  1474. struct ab8500_charger, check_hw_failure_work.work);
  1475. /* Check if the status bits for HW failure is still active */
  1476. if (di->flags.mainextchnotok) {
  1477. ret = abx500_get_register_interruptible(di->dev,
  1478. AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
  1479. if (ret < 0) {
  1480. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1481. return;
  1482. }
  1483. if (!(reg_value & MAIN_CH_NOK)) {
  1484. di->flags.mainextchnotok = false;
  1485. ab8500_power_supply_changed(di, &di->ac_chg.psy);
  1486. }
  1487. }
  1488. if (di->flags.vbus_ovv) {
  1489. ret = abx500_get_register_interruptible(di->dev,
  1490. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
  1491. &reg_value);
  1492. if (ret < 0) {
  1493. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1494. return;
  1495. }
  1496. if (!(reg_value & VBUS_OVV_TH)) {
  1497. di->flags.vbus_ovv = false;
  1498. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1499. }
  1500. }
  1501. /* If we still have a failure, schedule a new check */
  1502. if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
  1503. queue_delayed_work(di->charger_wq,
  1504. &di->check_hw_failure_work, round_jiffies(HZ));
  1505. }
  1506. }
  1507. /**
  1508. * ab8500_charger_kick_watchdog_work() - kick the watchdog
  1509. * @work: pointer to the work_struct structure
  1510. *
  1511. * Work queue function for kicking the charger watchdog.
  1512. *
  1513. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  1514. * logic. That means we have to continously kick the charger
  1515. * watchdog even when no charger is connected. This is only
  1516. * valid once the AC charger has been enabled. This is
  1517. * a bug that is not handled by the algorithm and the
  1518. * watchdog have to be kicked by the charger driver
  1519. * when the AC charger is disabled
  1520. */
  1521. static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
  1522. {
  1523. int ret;
  1524. struct ab8500_charger *di = container_of(work,
  1525. struct ab8500_charger, kick_wd_work.work);
  1526. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1527. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  1528. if (ret)
  1529. dev_err(di->dev, "Failed to kick WD!\n");
  1530. /* Schedule a new watchdog kick */
  1531. queue_delayed_work(di->charger_wq,
  1532. &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
  1533. }
  1534. /**
  1535. * ab8500_charger_ac_work() - work to get and set main charger status
  1536. * @work: pointer to the work_struct structure
  1537. *
  1538. * Work queue function for checking the main charger status
  1539. */
  1540. static void ab8500_charger_ac_work(struct work_struct *work)
  1541. {
  1542. int ret;
  1543. struct ab8500_charger *di = container_of(work,
  1544. struct ab8500_charger, ac_work);
  1545. /*
  1546. * Since we can't be sure that the events are received
  1547. * synchronously, we have the check if the main charger is
  1548. * connected by reading the status register
  1549. */
  1550. ret = ab8500_charger_detect_chargers(di);
  1551. if (ret < 0)
  1552. return;
  1553. if (ret & AC_PW_CONN) {
  1554. di->ac.charger_connected = 1;
  1555. di->ac_conn = true;
  1556. } else {
  1557. di->ac.charger_connected = 0;
  1558. }
  1559. ab8500_power_supply_changed(di, &di->ac_chg.psy);
  1560. sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
  1561. }
  1562. static void ab8500_charger_usb_attached_work(struct work_struct *work)
  1563. {
  1564. struct ab8500_charger *di = container_of(work,
  1565. struct ab8500_charger,
  1566. usb_charger_attached_work.work);
  1567. int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
  1568. int ret, i;
  1569. u8 statval;
  1570. for (i = 0; i < 10; i++) {
  1571. ret = abx500_get_register_interruptible(di->dev,
  1572. AB8500_CHARGER,
  1573. AB8500_CH_USBCH_STAT1_REG,
  1574. &statval);
  1575. if (ret < 0) {
  1576. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1577. goto reschedule;
  1578. }
  1579. if ((statval & usbch) != usbch)
  1580. goto reschedule;
  1581. msleep(CHARGER_STATUS_POLL);
  1582. }
  1583. ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
  1584. mutex_lock(&di->charger_attached_mutex);
  1585. mutex_unlock(&di->charger_attached_mutex);
  1586. return;
  1587. reschedule:
  1588. queue_delayed_work(di->charger_wq,
  1589. &di->usb_charger_attached_work,
  1590. HZ);
  1591. }
  1592. static void ab8500_charger_ac_attached_work(struct work_struct *work)
  1593. {
  1594. struct ab8500_charger *di = container_of(work,
  1595. struct ab8500_charger,
  1596. ac_charger_attached_work.work);
  1597. int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
  1598. MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
  1599. int ret, i;
  1600. u8 statval;
  1601. for (i = 0; i < 10; i++) {
  1602. ret = abx500_get_register_interruptible(di->dev,
  1603. AB8500_CHARGER,
  1604. AB8500_CH_STATUS2_REG,
  1605. &statval);
  1606. if (ret < 0) {
  1607. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1608. goto reschedule;
  1609. }
  1610. if ((statval & mainch) != mainch)
  1611. goto reschedule;
  1612. msleep(CHARGER_STATUS_POLL);
  1613. }
  1614. ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
  1615. queue_work(di->charger_wq, &di->ac_work);
  1616. mutex_lock(&di->charger_attached_mutex);
  1617. mutex_unlock(&di->charger_attached_mutex);
  1618. return;
  1619. reschedule:
  1620. queue_delayed_work(di->charger_wq,
  1621. &di->ac_charger_attached_work,
  1622. HZ);
  1623. }
  1624. /**
  1625. * ab8500_charger_detect_usb_type_work() - work to detect USB type
  1626. * @work: Pointer to the work_struct structure
  1627. *
  1628. * Detect the type of USB plugged
  1629. */
  1630. static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
  1631. {
  1632. int ret;
  1633. struct ab8500_charger *di = container_of(work,
  1634. struct ab8500_charger, detect_usb_type_work);
  1635. /*
  1636. * Since we can't be sure that the events are received
  1637. * synchronously, we have the check if is
  1638. * connected by reading the status register
  1639. */
  1640. ret = ab8500_charger_detect_chargers(di);
  1641. if (ret < 0)
  1642. return;
  1643. if (!(ret & USB_PW_CONN)) {
  1644. di->vbus_detected = 0;
  1645. ab8500_charger_set_usb_connected(di, false);
  1646. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1647. } else {
  1648. di->vbus_detected = 1;
  1649. if (is_ab8500_1p1_or_earlier(di->parent)) {
  1650. ret = ab8500_charger_detect_usb_type(di);
  1651. if (!ret) {
  1652. ab8500_charger_set_usb_connected(di, true);
  1653. ab8500_power_supply_changed(di,
  1654. &di->usb_chg.psy);
  1655. }
  1656. } else {
  1657. /* For ABB cut2.0 and onwards we have an IRQ,
  1658. * USB_LINK_STATUS that will be triggered when the USB
  1659. * link status changes. The exception is USB connected
  1660. * during startup. Then we don't get a
  1661. * USB_LINK_STATUS IRQ
  1662. */
  1663. if (di->vbus_detected_start) {
  1664. di->vbus_detected_start = false;
  1665. ret = ab8500_charger_detect_usb_type(di);
  1666. if (!ret) {
  1667. ab8500_charger_set_usb_connected(di,
  1668. true);
  1669. ab8500_power_supply_changed(di,
  1670. &di->usb_chg.psy);
  1671. }
  1672. }
  1673. }
  1674. }
  1675. }
  1676. /**
  1677. * ab8500_charger_usb_link_status_work() - work to detect USB type
  1678. * @work: pointer to the work_struct structure
  1679. *
  1680. * Detect the type of USB plugged
  1681. */
  1682. static void ab8500_charger_usb_link_status_work(struct work_struct *work)
  1683. {
  1684. int ret;
  1685. struct ab8500_charger *di = container_of(work,
  1686. struct ab8500_charger, usb_link_status_work);
  1687. /*
  1688. * Since we can't be sure that the events are received
  1689. * synchronously, we have the check if is
  1690. * connected by reading the status register
  1691. */
  1692. ret = ab8500_charger_detect_chargers(di);
  1693. if (ret < 0)
  1694. return;
  1695. if (!(ret & USB_PW_CONN)) {
  1696. di->vbus_detected = 0;
  1697. ab8500_charger_set_usb_connected(di, false);
  1698. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1699. } else {
  1700. di->vbus_detected = 1;
  1701. ret = ab8500_charger_read_usb_type(di);
  1702. if (!ret) {
  1703. /* Update maximum input current */
  1704. ret = ab8500_charger_set_vbus_in_curr(di,
  1705. di->max_usb_in_curr);
  1706. if (ret)
  1707. return;
  1708. ab8500_charger_set_usb_connected(di, true);
  1709. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1710. } else if (ret == -ENXIO) {
  1711. /* No valid charger type detected */
  1712. ab8500_charger_set_usb_connected(di, false);
  1713. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1714. }
  1715. }
  1716. }
  1717. static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
  1718. {
  1719. int ret;
  1720. unsigned long flags;
  1721. struct ab8500_charger *di = container_of(work,
  1722. struct ab8500_charger, usb_state_changed_work);
  1723. if (!di->vbus_detected)
  1724. return;
  1725. spin_lock_irqsave(&di->usb_state.usb_lock, flags);
  1726. di->usb_state.usb_changed = false;
  1727. spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
  1728. /*
  1729. * wait for some time until you get updates from the usb stack
  1730. * and negotiations are completed
  1731. */
  1732. msleep(250);
  1733. if (di->usb_state.usb_changed)
  1734. return;
  1735. dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
  1736. __func__, di->usb_state.state, di->usb_state.usb_current);
  1737. switch (di->usb_state.state) {
  1738. case AB8500_BM_USB_STATE_RESET_HS:
  1739. case AB8500_BM_USB_STATE_RESET_FS:
  1740. case AB8500_BM_USB_STATE_SUSPEND:
  1741. case AB8500_BM_USB_STATE_MAX:
  1742. ab8500_charger_set_usb_connected(di, false);
  1743. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1744. break;
  1745. case AB8500_BM_USB_STATE_RESUME:
  1746. /*
  1747. * when suspend->resume there should be delay
  1748. * of 1sec for enabling charging
  1749. */
  1750. msleep(1000);
  1751. /* Intentional fall through */
  1752. case AB8500_BM_USB_STATE_CONFIGURED:
  1753. /*
  1754. * USB is configured, enable charging with the charging
  1755. * input current obtained from USB driver
  1756. */
  1757. if (!ab8500_charger_get_usb_cur(di)) {
  1758. /* Update maximum input current */
  1759. ret = ab8500_charger_set_vbus_in_curr(di,
  1760. di->max_usb_in_curr);
  1761. if (ret)
  1762. return;
  1763. ab8500_charger_set_usb_connected(di, true);
  1764. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1765. }
  1766. break;
  1767. default:
  1768. break;
  1769. };
  1770. }
  1771. /**
  1772. * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
  1773. * @work: pointer to the work_struct structure
  1774. *
  1775. * Work queue function for checking the USB charger Not OK status
  1776. */
  1777. static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
  1778. {
  1779. int ret;
  1780. u8 reg_value;
  1781. bool prev_status;
  1782. struct ab8500_charger *di = container_of(work,
  1783. struct ab8500_charger, check_usbchgnotok_work.work);
  1784. /* Check if the status bit for usbchargernotok is still active */
  1785. ret = abx500_get_register_interruptible(di->dev,
  1786. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
  1787. if (ret < 0) {
  1788. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1789. return;
  1790. }
  1791. prev_status = di->flags.usbchargernotok;
  1792. if (reg_value & VBUS_CH_NOK) {
  1793. di->flags.usbchargernotok = true;
  1794. /* Check again in 1sec */
  1795. queue_delayed_work(di->charger_wq,
  1796. &di->check_usbchgnotok_work, HZ);
  1797. } else {
  1798. di->flags.usbchargernotok = false;
  1799. di->flags.vbus_collapse = false;
  1800. }
  1801. if (prev_status != di->flags.usbchargernotok)
  1802. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1803. }
  1804. /**
  1805. * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
  1806. * @work: pointer to the work_struct structure
  1807. *
  1808. * Work queue function for checking the Main thermal prot status
  1809. */
  1810. static void ab8500_charger_check_main_thermal_prot_work(
  1811. struct work_struct *work)
  1812. {
  1813. int ret;
  1814. u8 reg_value;
  1815. struct ab8500_charger *di = container_of(work,
  1816. struct ab8500_charger, check_main_thermal_prot_work);
  1817. /* Check if the status bit for main_thermal_prot is still active */
  1818. ret = abx500_get_register_interruptible(di->dev,
  1819. AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
  1820. if (ret < 0) {
  1821. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1822. return;
  1823. }
  1824. if (reg_value & MAIN_CH_TH_PROT)
  1825. di->flags.main_thermal_prot = true;
  1826. else
  1827. di->flags.main_thermal_prot = false;
  1828. ab8500_power_supply_changed(di, &di->ac_chg.psy);
  1829. }
  1830. /**
  1831. * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
  1832. * @work: pointer to the work_struct structure
  1833. *
  1834. * Work queue function for checking the USB thermal prot status
  1835. */
  1836. static void ab8500_charger_check_usb_thermal_prot_work(
  1837. struct work_struct *work)
  1838. {
  1839. int ret;
  1840. u8 reg_value;
  1841. struct ab8500_charger *di = container_of(work,
  1842. struct ab8500_charger, check_usb_thermal_prot_work);
  1843. /* Check if the status bit for usb_thermal_prot is still active */
  1844. ret = abx500_get_register_interruptible(di->dev,
  1845. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
  1846. if (ret < 0) {
  1847. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1848. return;
  1849. }
  1850. if (reg_value & USB_CH_TH_PROT)
  1851. di->flags.usb_thermal_prot = true;
  1852. else
  1853. di->flags.usb_thermal_prot = false;
  1854. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  1855. }
  1856. /**
  1857. * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
  1858. * @irq: interrupt number
  1859. * @_di: pointer to the ab8500_charger structure
  1860. *
  1861. * Returns IRQ status(IRQ_HANDLED)
  1862. */
  1863. static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
  1864. {
  1865. struct ab8500_charger *di = _di;
  1866. dev_dbg(di->dev, "Main charger unplugged\n");
  1867. queue_work(di->charger_wq, &di->ac_work);
  1868. cancel_delayed_work_sync(&di->ac_charger_attached_work);
  1869. mutex_lock(&di->charger_attached_mutex);
  1870. mutex_unlock(&di->charger_attached_mutex);
  1871. return IRQ_HANDLED;
  1872. }
  1873. /**
  1874. * ab8500_charger_mainchplugdet_handler() - main charger plugged
  1875. * @irq: interrupt number
  1876. * @_di: pointer to the ab8500_charger structure
  1877. *
  1878. * Returns IRQ status(IRQ_HANDLED)
  1879. */
  1880. static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
  1881. {
  1882. struct ab8500_charger *di = _di;
  1883. dev_dbg(di->dev, "Main charger plugged\n");
  1884. queue_work(di->charger_wq, &di->ac_work);
  1885. mutex_lock(&di->charger_attached_mutex);
  1886. mutex_unlock(&di->charger_attached_mutex);
  1887. queue_delayed_work(di->charger_wq,
  1888. &di->ac_charger_attached_work,
  1889. HZ);
  1890. return IRQ_HANDLED;
  1891. }
  1892. /**
  1893. * ab8500_charger_mainextchnotok_handler() - main charger not ok
  1894. * @irq: interrupt number
  1895. * @_di: pointer to the ab8500_charger structure
  1896. *
  1897. * Returns IRQ status(IRQ_HANDLED)
  1898. */
  1899. static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
  1900. {
  1901. struct ab8500_charger *di = _di;
  1902. dev_dbg(di->dev, "Main charger not ok\n");
  1903. di->flags.mainextchnotok = true;
  1904. ab8500_power_supply_changed(di, &di->ac_chg.psy);
  1905. /* Schedule a new HW failure check */
  1906. queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
  1907. return IRQ_HANDLED;
  1908. }
  1909. /**
  1910. * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
  1911. * thermal protection threshold
  1912. * @irq: interrupt number
  1913. * @_di: pointer to the ab8500_charger structure
  1914. *
  1915. * Returns IRQ status(IRQ_HANDLED)
  1916. */
  1917. static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
  1918. {
  1919. struct ab8500_charger *di = _di;
  1920. dev_dbg(di->dev,
  1921. "Die temp above Main charger thermal protection threshold\n");
  1922. queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
  1923. return IRQ_HANDLED;
  1924. }
  1925. /**
  1926. * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
  1927. * thermal protection threshold
  1928. * @irq: interrupt number
  1929. * @_di: pointer to the ab8500_charger structure
  1930. *
  1931. * Returns IRQ status(IRQ_HANDLED)
  1932. */
  1933. static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
  1934. {
  1935. struct ab8500_charger *di = _di;
  1936. dev_dbg(di->dev,
  1937. "Die temp ok for Main charger thermal protection threshold\n");
  1938. queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
  1939. return IRQ_HANDLED;
  1940. }
  1941. /**
  1942. * ab8500_charger_vbusdetf_handler() - VBUS falling detected
  1943. * @irq: interrupt number
  1944. * @_di: pointer to the ab8500_charger structure
  1945. *
  1946. * Returns IRQ status(IRQ_HANDLED)
  1947. */
  1948. static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
  1949. {
  1950. struct ab8500_charger *di = _di;
  1951. dev_dbg(di->dev, "VBUS falling detected\n");
  1952. queue_work(di->charger_wq, &di->detect_usb_type_work);
  1953. return IRQ_HANDLED;
  1954. }
  1955. /**
  1956. * ab8500_charger_vbusdetr_handler() - VBUS rising detected
  1957. * @irq: interrupt number
  1958. * @_di: pointer to the ab8500_charger structure
  1959. *
  1960. * Returns IRQ status(IRQ_HANDLED)
  1961. */
  1962. static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
  1963. {
  1964. struct ab8500_charger *di = _di;
  1965. di->vbus_detected = true;
  1966. dev_dbg(di->dev, "VBUS rising detected\n");
  1967. queue_work(di->charger_wq, &di->detect_usb_type_work);
  1968. return IRQ_HANDLED;
  1969. }
  1970. /**
  1971. * ab8500_charger_usblinkstatus_handler() - USB link status has changed
  1972. * @irq: interrupt number
  1973. * @_di: pointer to the ab8500_charger structure
  1974. *
  1975. * Returns IRQ status(IRQ_HANDLED)
  1976. */
  1977. static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
  1978. {
  1979. struct ab8500_charger *di = _di;
  1980. dev_dbg(di->dev, "USB link status changed\n");
  1981. queue_work(di->charger_wq, &di->usb_link_status_work);
  1982. return IRQ_HANDLED;
  1983. }
  1984. /**
  1985. * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
  1986. * thermal protection threshold
  1987. * @irq: interrupt number
  1988. * @_di: pointer to the ab8500_charger structure
  1989. *
  1990. * Returns IRQ status(IRQ_HANDLED)
  1991. */
  1992. static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
  1993. {
  1994. struct ab8500_charger *di = _di;
  1995. dev_dbg(di->dev,
  1996. "Die temp above USB charger thermal protection threshold\n");
  1997. queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
  1998. return IRQ_HANDLED;
  1999. }
  2000. /**
  2001. * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
  2002. * thermal protection threshold
  2003. * @irq: interrupt number
  2004. * @_di: pointer to the ab8500_charger structure
  2005. *
  2006. * Returns IRQ status(IRQ_HANDLED)
  2007. */
  2008. static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
  2009. {
  2010. struct ab8500_charger *di = _di;
  2011. dev_dbg(di->dev,
  2012. "Die temp ok for USB charger thermal protection threshold\n");
  2013. queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
  2014. return IRQ_HANDLED;
  2015. }
  2016. /**
  2017. * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
  2018. * @irq: interrupt number
  2019. * @_di: pointer to the ab8500_charger structure
  2020. *
  2021. * Returns IRQ status(IRQ_HANDLED)
  2022. */
  2023. static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
  2024. {
  2025. struct ab8500_charger *di = _di;
  2026. dev_dbg(di->dev, "Not allowed USB charger detected\n");
  2027. queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
  2028. return IRQ_HANDLED;
  2029. }
  2030. /**
  2031. * ab8500_charger_chwdexp_handler() - Charger watchdog expired
  2032. * @irq: interrupt number
  2033. * @_di: pointer to the ab8500_charger structure
  2034. *
  2035. * Returns IRQ status(IRQ_HANDLED)
  2036. */
  2037. static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
  2038. {
  2039. struct ab8500_charger *di = _di;
  2040. dev_dbg(di->dev, "Charger watchdog expired\n");
  2041. /*
  2042. * The charger that was online when the watchdog expired
  2043. * needs to be restarted for charging to start again
  2044. */
  2045. if (di->ac.charger_online) {
  2046. di->ac.wd_expired = true;
  2047. ab8500_power_supply_changed(di, &di->ac_chg.psy);
  2048. }
  2049. if (di->usb.charger_online) {
  2050. di->usb.wd_expired = true;
  2051. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  2052. }
  2053. return IRQ_HANDLED;
  2054. }
  2055. /**
  2056. * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
  2057. * @irq: interrupt number
  2058. * @_di: pointer to the ab8500_charger structure
  2059. *
  2060. * Returns IRQ status(IRQ_HANDLED)
  2061. */
  2062. static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
  2063. {
  2064. struct ab8500_charger *di = _di;
  2065. dev_dbg(di->dev, "VBUS overvoltage detected\n");
  2066. di->flags.vbus_ovv = true;
  2067. ab8500_power_supply_changed(di, &di->usb_chg.psy);
  2068. /* Schedule a new HW failure check */
  2069. queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
  2070. return IRQ_HANDLED;
  2071. }
  2072. /**
  2073. * ab8500_charger_ac_get_property() - get the ac/mains properties
  2074. * @psy: pointer to the power_supply structure
  2075. * @psp: pointer to the power_supply_property structure
  2076. * @val: pointer to the power_supply_propval union
  2077. *
  2078. * This function gets called when an application tries to get the ac/mains
  2079. * properties by reading the sysfs files.
  2080. * AC/Mains properties are online, present and voltage.
  2081. * online: ac/mains charging is in progress or not
  2082. * present: presence of the ac/mains
  2083. * voltage: AC/Mains voltage
  2084. * Returns error code in case of failure else 0(on success)
  2085. */
  2086. static int ab8500_charger_ac_get_property(struct power_supply *psy,
  2087. enum power_supply_property psp,
  2088. union power_supply_propval *val)
  2089. {
  2090. struct ab8500_charger *di;
  2091. di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
  2092. switch (psp) {
  2093. case POWER_SUPPLY_PROP_HEALTH:
  2094. if (di->flags.mainextchnotok)
  2095. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  2096. else if (di->ac.wd_expired || di->usb.wd_expired)
  2097. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  2098. else if (di->flags.main_thermal_prot)
  2099. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  2100. else
  2101. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  2102. break;
  2103. case POWER_SUPPLY_PROP_ONLINE:
  2104. val->intval = di->ac.charger_online;
  2105. break;
  2106. case POWER_SUPPLY_PROP_PRESENT:
  2107. val->intval = di->ac.charger_connected;
  2108. break;
  2109. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  2110. di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
  2111. val->intval = di->ac.charger_voltage * 1000;
  2112. break;
  2113. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  2114. /*
  2115. * This property is used to indicate when CV mode is entered
  2116. * for the AC charger
  2117. */
  2118. di->ac.cv_active = ab8500_charger_ac_cv(di);
  2119. val->intval = di->ac.cv_active;
  2120. break;
  2121. case POWER_SUPPLY_PROP_CURRENT_NOW:
  2122. val->intval = ab8500_charger_get_ac_current(di) * 1000;
  2123. break;
  2124. default:
  2125. return -EINVAL;
  2126. }
  2127. return 0;
  2128. }
  2129. /**
  2130. * ab8500_charger_usb_get_property() - get the usb properties
  2131. * @psy: pointer to the power_supply structure
  2132. * @psp: pointer to the power_supply_property structure
  2133. * @val: pointer to the power_supply_propval union
  2134. *
  2135. * This function gets called when an application tries to get the usb
  2136. * properties by reading the sysfs files.
  2137. * USB properties are online, present and voltage.
  2138. * online: usb charging is in progress or not
  2139. * present: presence of the usb
  2140. * voltage: vbus voltage
  2141. * Returns error code in case of failure else 0(on success)
  2142. */
  2143. static int ab8500_charger_usb_get_property(struct power_supply *psy,
  2144. enum power_supply_property psp,
  2145. union power_supply_propval *val)
  2146. {
  2147. struct ab8500_charger *di;
  2148. di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
  2149. switch (psp) {
  2150. case POWER_SUPPLY_PROP_HEALTH:
  2151. if (di->flags.usbchargernotok)
  2152. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  2153. else if (di->ac.wd_expired || di->usb.wd_expired)
  2154. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  2155. else if (di->flags.usb_thermal_prot)
  2156. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  2157. else if (di->flags.vbus_ovv)
  2158. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  2159. else
  2160. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  2161. break;
  2162. case POWER_SUPPLY_PROP_ONLINE:
  2163. val->intval = di->usb.charger_online;
  2164. break;
  2165. case POWER_SUPPLY_PROP_PRESENT:
  2166. val->intval = di->usb.charger_connected;
  2167. break;
  2168. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  2169. di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
  2170. val->intval = di->usb.charger_voltage * 1000;
  2171. break;
  2172. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  2173. /*
  2174. * This property is used to indicate when CV mode is entered
  2175. * for the USB charger
  2176. */
  2177. di->usb.cv_active = ab8500_charger_usb_cv(di);
  2178. val->intval = di->usb.cv_active;
  2179. break;
  2180. case POWER_SUPPLY_PROP_CURRENT_NOW:
  2181. val->intval = ab8500_charger_get_usb_current(di) * 1000;
  2182. break;
  2183. case POWER_SUPPLY_PROP_CURRENT_AVG:
  2184. /*
  2185. * This property is used to indicate when VBUS has collapsed
  2186. * due to too high output current from the USB charger
  2187. */
  2188. if (di->flags.vbus_collapse)
  2189. val->intval = 1;
  2190. else
  2191. val->intval = 0;
  2192. break;
  2193. default:
  2194. return -EINVAL;
  2195. }
  2196. return 0;
  2197. }
  2198. /**
  2199. * ab8500_charger_init_hw_registers() - Set up charger related registers
  2200. * @di: pointer to the ab8500_charger structure
  2201. *
  2202. * Set up charger OVV, watchdog and maximum voltage registers as well as
  2203. * charging of the backup battery
  2204. */
  2205. static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
  2206. {
  2207. int ret = 0;
  2208. /* Setup maximum charger current and voltage for ABB cut2.0 */
  2209. if (!is_ab8500_1p1_or_earlier(di->parent)) {
  2210. ret = abx500_set_register_interruptible(di->dev,
  2211. AB8500_CHARGER,
  2212. AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
  2213. if (ret) {
  2214. dev_err(di->dev,
  2215. "failed to set CH_VOLT_LVL_MAX_REG\n");
  2216. goto out;
  2217. }
  2218. ret = abx500_set_register_interruptible(di->dev,
  2219. AB8500_CHARGER,
  2220. AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
  2221. if (ret) {
  2222. dev_err(di->dev,
  2223. "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
  2224. goto out;
  2225. }
  2226. }
  2227. /* VBUS OVV set to 6.3V and enable automatic current limitiation */
  2228. ret = abx500_set_register_interruptible(di->dev,
  2229. AB8500_CHARGER,
  2230. AB8500_USBCH_CTRL2_REG,
  2231. VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
  2232. if (ret) {
  2233. dev_err(di->dev, "failed to set VBUS OVV\n");
  2234. goto out;
  2235. }
  2236. /* Enable main watchdog in OTP */
  2237. ret = abx500_set_register_interruptible(di->dev,
  2238. AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
  2239. if (ret) {
  2240. dev_err(di->dev, "failed to enable main WD in OTP\n");
  2241. goto out;
  2242. }
  2243. /* Enable main watchdog */
  2244. ret = abx500_set_register_interruptible(di->dev,
  2245. AB8500_SYS_CTRL2_BLOCK,
  2246. AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
  2247. if (ret) {
  2248. dev_err(di->dev, "faile to enable main watchdog\n");
  2249. goto out;
  2250. }
  2251. /*
  2252. * Due to internal synchronisation, Enable and Kick watchdog bits
  2253. * cannot be enabled in a single write.
  2254. * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
  2255. * between writing Enable then Kick bits.
  2256. */
  2257. udelay(63);
  2258. /* Kick main watchdog */
  2259. ret = abx500_set_register_interruptible(di->dev,
  2260. AB8500_SYS_CTRL2_BLOCK,
  2261. AB8500_MAIN_WDOG_CTRL_REG,
  2262. (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
  2263. if (ret) {
  2264. dev_err(di->dev, "failed to kick main watchdog\n");
  2265. goto out;
  2266. }
  2267. /* Disable main watchdog */
  2268. ret = abx500_set_register_interruptible(di->dev,
  2269. AB8500_SYS_CTRL2_BLOCK,
  2270. AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
  2271. if (ret) {
  2272. dev_err(di->dev, "failed to disable main watchdog\n");
  2273. goto out;
  2274. }
  2275. /* Set watchdog timeout */
  2276. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  2277. AB8500_CH_WD_TIMER_REG, WD_TIMER);
  2278. if (ret) {
  2279. dev_err(di->dev, "failed to set charger watchdog timeout\n");
  2280. goto out;
  2281. }
  2282. /* Backup battery voltage and current */
  2283. ret = abx500_set_register_interruptible(di->dev,
  2284. AB8500_RTC,
  2285. AB8500_RTC_BACKUP_CHG_REG,
  2286. di->bm->bkup_bat_v |
  2287. di->bm->bkup_bat_i);
  2288. if (ret) {
  2289. dev_err(di->dev, "failed to setup backup battery charging\n");
  2290. goto out;
  2291. }
  2292. /* Enable backup battery charging */
  2293. abx500_mask_and_set_register_interruptible(di->dev,
  2294. AB8500_RTC, AB8500_RTC_CTRL_REG,
  2295. RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
  2296. if (ret < 0)
  2297. dev_err(di->dev, "%s mask and set failed\n", __func__);
  2298. out:
  2299. return ret;
  2300. }
  2301. /*
  2302. * ab8500 charger driver interrupts and their respective isr
  2303. */
  2304. static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
  2305. {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
  2306. {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
  2307. {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
  2308. {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
  2309. {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
  2310. {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
  2311. {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
  2312. {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
  2313. {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
  2314. {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
  2315. {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
  2316. {"VBUS_OVV", ab8500_charger_vbusovv_handler},
  2317. {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
  2318. };
  2319. static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
  2320. unsigned long event, void *power)
  2321. {
  2322. struct ab8500_charger *di =
  2323. container_of(nb, struct ab8500_charger, nb);
  2324. enum ab8500_usb_state bm_usb_state;
  2325. unsigned mA = *((unsigned *)power);
  2326. if (event != USB_EVENT_VBUS) {
  2327. dev_dbg(di->dev, "not a standard host, returning\n");
  2328. return NOTIFY_DONE;
  2329. }
  2330. /* TODO: State is fabricate here. See if charger really needs USB
  2331. * state or if mA is enough
  2332. */
  2333. if ((di->usb_state.usb_current == 2) && (mA > 2))
  2334. bm_usb_state = AB8500_BM_USB_STATE_RESUME;
  2335. else if (mA == 0)
  2336. bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
  2337. else if (mA == 2)
  2338. bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
  2339. else if (mA >= 8) /* 8, 100, 500 */
  2340. bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
  2341. else /* Should never occur */
  2342. bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
  2343. dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
  2344. __func__, bm_usb_state, mA);
  2345. spin_lock(&di->usb_state.usb_lock);
  2346. di->usb_state.usb_changed = true;
  2347. spin_unlock(&di->usb_state.usb_lock);
  2348. di->usb_state.state = bm_usb_state;
  2349. di->usb_state.usb_current = mA;
  2350. queue_work(di->charger_wq, &di->usb_state_changed_work);
  2351. return NOTIFY_OK;
  2352. }
  2353. #if defined(CONFIG_PM)
  2354. static int ab8500_charger_resume(struct platform_device *pdev)
  2355. {
  2356. int ret;
  2357. struct ab8500_charger *di = platform_get_drvdata(pdev);
  2358. /*
  2359. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  2360. * logic. That means we have to continously kick the charger
  2361. * watchdog even when no charger is connected. This is only
  2362. * valid once the AC charger has been enabled. This is
  2363. * a bug that is not handled by the algorithm and the
  2364. * watchdog have to be kicked by the charger driver
  2365. * when the AC charger is disabled
  2366. */
  2367. if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
  2368. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  2369. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  2370. if (ret)
  2371. dev_err(di->dev, "Failed to kick WD!\n");
  2372. /* If not already pending start a new timer */
  2373. if (!delayed_work_pending(
  2374. &di->kick_wd_work)) {
  2375. queue_delayed_work(di->charger_wq, &di->kick_wd_work,
  2376. round_jiffies(WD_KICK_INTERVAL));
  2377. }
  2378. }
  2379. /* If we still have a HW failure, schedule a new check */
  2380. if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
  2381. queue_delayed_work(di->charger_wq,
  2382. &di->check_hw_failure_work, 0);
  2383. }
  2384. return 0;
  2385. }
  2386. static int ab8500_charger_suspend(struct platform_device *pdev,
  2387. pm_message_t state)
  2388. {
  2389. struct ab8500_charger *di = platform_get_drvdata(pdev);
  2390. /* Cancel any pending HW failure check */
  2391. if (delayed_work_pending(&di->check_hw_failure_work))
  2392. cancel_delayed_work(&di->check_hw_failure_work);
  2393. return 0;
  2394. }
  2395. #else
  2396. #define ab8500_charger_suspend NULL
  2397. #define ab8500_charger_resume NULL
  2398. #endif
  2399. static int ab8500_charger_remove(struct platform_device *pdev)
  2400. {
  2401. struct ab8500_charger *di = platform_get_drvdata(pdev);
  2402. int i, irq, ret;
  2403. /* Disable AC charging */
  2404. ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
  2405. /* Disable USB charging */
  2406. ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
  2407. /* Disable interrupts */
  2408. for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
  2409. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  2410. free_irq(irq, di);
  2411. }
  2412. /* Backup battery voltage and current disable */
  2413. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2414. AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
  2415. if (ret < 0)
  2416. dev_err(di->dev, "%s mask and set failed\n", __func__);
  2417. usb_unregister_notifier(di->usb_phy, &di->nb);
  2418. usb_put_phy(di->usb_phy);
  2419. /* Delete the work queue */
  2420. destroy_workqueue(di->charger_wq);
  2421. flush_scheduled_work();
  2422. power_supply_unregister(&di->usb_chg.psy);
  2423. power_supply_unregister(&di->ac_chg.psy);
  2424. platform_set_drvdata(pdev, NULL);
  2425. return 0;
  2426. }
  2427. static char *supply_interface[] = {
  2428. "ab8500_chargalg",
  2429. "ab8500_fg",
  2430. "ab8500_btemp",
  2431. };
  2432. static int ab8500_charger_probe(struct platform_device *pdev)
  2433. {
  2434. struct device_node *np = pdev->dev.of_node;
  2435. struct abx500_bm_data *plat = pdev->dev.platform_data;
  2436. struct ab8500_charger *di;
  2437. int irq, i, charger_status, ret = 0, ch_stat;
  2438. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  2439. if (!di) {
  2440. dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
  2441. return -ENOMEM;
  2442. }
  2443. if (!plat) {
  2444. dev_err(&pdev->dev, "no battery management data supplied\n");
  2445. return -EINVAL;
  2446. }
  2447. di->bm = plat;
  2448. if (np) {
  2449. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  2450. if (ret) {
  2451. dev_err(&pdev->dev, "failed to get battery information\n");
  2452. return ret;
  2453. }
  2454. di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
  2455. } else
  2456. di->autopower_cfg = false;
  2457. /* get parent data */
  2458. di->dev = &pdev->dev;
  2459. di->parent = dev_get_drvdata(pdev->dev.parent);
  2460. di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  2461. /* initialize lock */
  2462. spin_lock_init(&di->usb_state.usb_lock);
  2463. di->autopower = false;
  2464. /* AC supply */
  2465. /* power_supply base class */
  2466. di->ac_chg.psy.name = "ab8500_ac";
  2467. di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
  2468. di->ac_chg.psy.properties = ab8500_charger_ac_props;
  2469. di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
  2470. di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
  2471. di->ac_chg.psy.supplied_to = supply_interface;
  2472. di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
  2473. /* ux500_charger sub-class */
  2474. di->ac_chg.ops.enable = &ab8500_charger_ac_en;
  2475. di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
  2476. di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
  2477. di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
  2478. ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
  2479. di->ac_chg.max_out_curr = ab8500_charger_current_map[
  2480. ARRAY_SIZE(ab8500_charger_current_map) - 1];
  2481. /* USB supply */
  2482. /* power_supply base class */
  2483. di->usb_chg.psy.name = "ab8500_usb";
  2484. di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
  2485. di->usb_chg.psy.properties = ab8500_charger_usb_props;
  2486. di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
  2487. di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
  2488. di->usb_chg.psy.supplied_to = supply_interface;
  2489. di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface),
  2490. /* ux500_charger sub-class */
  2491. di->usb_chg.ops.enable = &ab8500_charger_usb_en;
  2492. di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
  2493. di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
  2494. di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
  2495. ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
  2496. di->usb_chg.max_out_curr = ab8500_charger_current_map[
  2497. ARRAY_SIZE(ab8500_charger_current_map) - 1];
  2498. /* Create a work queue for the charger */
  2499. di->charger_wq =
  2500. create_singlethread_workqueue("ab8500_charger_wq");
  2501. if (di->charger_wq == NULL) {
  2502. dev_err(di->dev, "failed to create work queue\n");
  2503. return -ENOMEM;
  2504. }
  2505. mutex_init(&di->charger_attached_mutex);
  2506. /* Init work for HW failure check */
  2507. INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
  2508. ab8500_charger_check_hw_failure_work);
  2509. INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
  2510. ab8500_charger_check_usbchargernotok_work);
  2511. INIT_DELAYED_WORK(&di->ac_charger_attached_work,
  2512. ab8500_charger_ac_attached_work);
  2513. INIT_DELAYED_WORK(&di->usb_charger_attached_work,
  2514. ab8500_charger_usb_attached_work);
  2515. /*
  2516. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  2517. * logic. That means we have to continously kick the charger
  2518. * watchdog even when no charger is connected. This is only
  2519. * valid once the AC charger has been enabled. This is
  2520. * a bug that is not handled by the algorithm and the
  2521. * watchdog have to be kicked by the charger driver
  2522. * when the AC charger is disabled
  2523. */
  2524. INIT_DEFERRABLE_WORK(&di->kick_wd_work,
  2525. ab8500_charger_kick_watchdog_work);
  2526. INIT_DEFERRABLE_WORK(&di->check_vbat_work,
  2527. ab8500_charger_check_vbat_work);
  2528. /* Init work for charger detection */
  2529. INIT_WORK(&di->usb_link_status_work,
  2530. ab8500_charger_usb_link_status_work);
  2531. INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
  2532. INIT_WORK(&di->detect_usb_type_work,
  2533. ab8500_charger_detect_usb_type_work);
  2534. INIT_WORK(&di->usb_state_changed_work,
  2535. ab8500_charger_usb_state_changed_work);
  2536. /* Init work for checking HW status */
  2537. INIT_WORK(&di->check_main_thermal_prot_work,
  2538. ab8500_charger_check_main_thermal_prot_work);
  2539. INIT_WORK(&di->check_usb_thermal_prot_work,
  2540. ab8500_charger_check_usb_thermal_prot_work);
  2541. /*
  2542. * VDD ADC supply needs to be enabled from this driver when there
  2543. * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
  2544. * interrupts during charging
  2545. */
  2546. di->regu = devm_regulator_get(di->dev, "vddadc");
  2547. if (IS_ERR(di->regu)) {
  2548. ret = PTR_ERR(di->regu);
  2549. dev_err(di->dev, "failed to get vddadc regulator\n");
  2550. goto free_charger_wq;
  2551. }
  2552. /* Initialize OVV, and other registers */
  2553. ret = ab8500_charger_init_hw_registers(di);
  2554. if (ret) {
  2555. dev_err(di->dev, "failed to initialize ABB registers\n");
  2556. goto free_charger_wq;
  2557. }
  2558. /* Register AC charger class */
  2559. ret = power_supply_register(di->dev, &di->ac_chg.psy);
  2560. if (ret) {
  2561. dev_err(di->dev, "failed to register AC charger\n");
  2562. goto free_charger_wq;
  2563. }
  2564. /* Register USB charger class */
  2565. ret = power_supply_register(di->dev, &di->usb_chg.psy);
  2566. if (ret) {
  2567. dev_err(di->dev, "failed to register USB charger\n");
  2568. goto free_ac;
  2569. }
  2570. di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
  2571. if (IS_ERR_OR_NULL(di->usb_phy)) {
  2572. dev_err(di->dev, "failed to get usb transceiver\n");
  2573. ret = -EINVAL;
  2574. goto free_usb;
  2575. }
  2576. di->nb.notifier_call = ab8500_charger_usb_notifier_call;
  2577. ret = usb_register_notifier(di->usb_phy, &di->nb);
  2578. if (ret) {
  2579. dev_err(di->dev, "failed to register usb notifier\n");
  2580. goto put_usb_phy;
  2581. }
  2582. /* Identify the connected charger types during startup */
  2583. charger_status = ab8500_charger_detect_chargers(di);
  2584. if (charger_status & AC_PW_CONN) {
  2585. di->ac.charger_connected = 1;
  2586. di->ac_conn = true;
  2587. ab8500_power_supply_changed(di, &di->ac_chg.psy);
  2588. sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
  2589. }
  2590. if (charger_status & USB_PW_CONN) {
  2591. dev_dbg(di->dev, "VBUS Detect during startup\n");
  2592. di->vbus_detected = true;
  2593. di->vbus_detected_start = true;
  2594. queue_work(di->charger_wq,
  2595. &di->detect_usb_type_work);
  2596. }
  2597. /* Register interrupts */
  2598. for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
  2599. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  2600. ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
  2601. IRQF_SHARED | IRQF_NO_SUSPEND,
  2602. ab8500_charger_irq[i].name, di);
  2603. if (ret != 0) {
  2604. dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
  2605. , ab8500_charger_irq[i].name, irq, ret);
  2606. goto free_irq;
  2607. }
  2608. dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
  2609. ab8500_charger_irq[i].name, irq, ret);
  2610. }
  2611. platform_set_drvdata(pdev, di);
  2612. mutex_lock(&di->charger_attached_mutex);
  2613. ch_stat = ab8500_charger_detect_chargers(di);
  2614. if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
  2615. queue_delayed_work(di->charger_wq,
  2616. &di->ac_charger_attached_work,
  2617. HZ);
  2618. }
  2619. if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
  2620. queue_delayed_work(di->charger_wq,
  2621. &di->usb_charger_attached_work,
  2622. HZ);
  2623. }
  2624. mutex_unlock(&di->charger_attached_mutex);
  2625. return ret;
  2626. free_irq:
  2627. usb_unregister_notifier(di->usb_phy, &di->nb);
  2628. /* We also have to free all successfully registered irqs */
  2629. for (i = i - 1; i >= 0; i--) {
  2630. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  2631. free_irq(irq, di);
  2632. }
  2633. put_usb_phy:
  2634. usb_put_phy(di->usb_phy);
  2635. free_usb:
  2636. power_supply_unregister(&di->usb_chg.psy);
  2637. free_ac:
  2638. power_supply_unregister(&di->ac_chg.psy);
  2639. free_charger_wq:
  2640. destroy_workqueue(di->charger_wq);
  2641. return ret;
  2642. }
  2643. static const struct of_device_id ab8500_charger_match[] = {
  2644. { .compatible = "stericsson,ab8500-charger", },
  2645. { },
  2646. };
  2647. static struct platform_driver ab8500_charger_driver = {
  2648. .probe = ab8500_charger_probe,
  2649. .remove = ab8500_charger_remove,
  2650. .suspend = ab8500_charger_suspend,
  2651. .resume = ab8500_charger_resume,
  2652. .driver = {
  2653. .name = "ab8500-charger",
  2654. .owner = THIS_MODULE,
  2655. .of_match_table = ab8500_charger_match,
  2656. },
  2657. };
  2658. static int __init ab8500_charger_init(void)
  2659. {
  2660. return platform_driver_register(&ab8500_charger_driver);
  2661. }
  2662. static void __exit ab8500_charger_exit(void)
  2663. {
  2664. platform_driver_unregister(&ab8500_charger_driver);
  2665. }
  2666. subsys_initcall_sync(ab8500_charger_init);
  2667. module_exit(ab8500_charger_exit);
  2668. MODULE_LICENSE("GPL v2");
  2669. MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
  2670. MODULE_ALIAS("platform:ab8500-charger");
  2671. MODULE_DESCRIPTION("AB8500 charger management driver");