ab8500_charger.c 97 KB

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