ab8500_charger.c 85 KB

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