ab8500_charger.c 102 KB

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