ab8500_charger.c 86 KB

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