ab8500_charger.c 87 KB

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