ab8500_charger.c 81 KB

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