qti_battery_charger.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "BATTERY_CHG: %s: " fmt, __func__
  7. #include <linux/debugfs.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/firmware.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reboot.h>
  15. #include <linux/rpmsg.h>
  16. #include <linux/mutex.h>
  17. #include <linux/pm_wakeup.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/reboot.h>
  20. #include <linux/thermal.h>
  21. #include <linux/soc/qcom/pmic_glink.h>
  22. #include <linux/soc/qcom/battery_charger.h>
  23. #include <linux/soc/qcom/panel_event_notifier.h>
  24. #define MSG_OWNER_BC 32778
  25. #define MSG_TYPE_REQ_RESP 1
  26. #define MSG_TYPE_NOTIFY 2
  27. /* opcode for battery charger */
  28. #define BC_SET_NOTIFY_REQ 0x04
  29. #define BC_DISABLE_NOTIFY_REQ 0x05
  30. #define BC_NOTIFY_IND 0x07
  31. #define BC_BATTERY_STATUS_GET 0x30
  32. #define BC_BATTERY_STATUS_SET 0x31
  33. #define BC_USB_STATUS_GET(port_id) (0x32 | (port_id) << 16)
  34. #define BC_USB_STATUS_SET(port_id) (0x33 | (port_id) << 16)
  35. #define BC_WLS_STATUS_GET 0x34
  36. #define BC_WLS_STATUS_SET 0x35
  37. #define BC_SHIP_MODE_REQ_SET 0x36
  38. #define BC_WLS_FW_CHECK_UPDATE 0x40
  39. #define BC_WLS_FW_PUSH_BUF_REQ 0x41
  40. #define BC_WLS_FW_UPDATE_STATUS_RESP 0x42
  41. #define BC_WLS_FW_PUSH_BUF_RESP 0x43
  42. #define BC_WLS_FW_GET_VERSION 0x44
  43. #define BC_SHUTDOWN_NOTIFY 0x47
  44. #define BC_CHG_CTRL_LIMIT_EN 0x48
  45. #define BC_HBOOST_VMAX_CLAMP_NOTIFY 0x79
  46. #define BC_GENERIC_NOTIFY 0x80
  47. /* Generic definitions */
  48. #define MAX_STR_LEN 128
  49. #define BC_WAIT_TIME_MS 1000
  50. #define WLS_FW_PREPARE_TIME_MS 1000
  51. #define WLS_FW_WAIT_TIME_MS 500
  52. #define WLS_FW_UPDATE_TIME_MS 1000
  53. #define WLS_FW_BUF_SIZE 128
  54. #define DEFAULT_RESTRICT_FCC_UA 1000000
  55. enum usb_port_id {
  56. USB_1_PORT_ID,
  57. USB_2_PORT_ID,
  58. NUM_USB_PORTS,
  59. };
  60. enum psy_type {
  61. PSY_TYPE_BATTERY,
  62. PSY_TYPE_USB,
  63. PSY_TYPE_USB_2,
  64. PSY_TYPE_WLS,
  65. PSY_TYPE_MAX,
  66. };
  67. enum ship_mode_type {
  68. SHIP_MODE_PMIC,
  69. SHIP_MODE_PACK_SIDE,
  70. };
  71. /* property ids */
  72. enum battery_property_id {
  73. BATT_STATUS,
  74. BATT_HEALTH,
  75. BATT_PRESENT,
  76. BATT_CHG_TYPE,
  77. BATT_CAPACITY,
  78. BATT_SOH,
  79. BATT_VOLT_OCV,
  80. BATT_VOLT_NOW,
  81. BATT_VOLT_MAX,
  82. BATT_CURR_NOW,
  83. BATT_CHG_CTRL_LIM,
  84. BATT_CHG_CTRL_LIM_MAX,
  85. BATT_TEMP,
  86. BATT_TECHNOLOGY,
  87. BATT_CHG_COUNTER,
  88. BATT_CYCLE_COUNT,
  89. BATT_CHG_FULL_DESIGN,
  90. BATT_CHG_FULL,
  91. BATT_MODEL_NAME,
  92. BATT_TTF_AVG,
  93. BATT_TTE_AVG,
  94. BATT_RESISTANCE,
  95. BATT_POWER_NOW,
  96. BATT_POWER_AVG,
  97. BATT_CHG_CTRL_EN,
  98. BATT_CHG_CTRL_START_THR,
  99. BATT_CHG_CTRL_END_THR,
  100. BATT_CURR_AVG,
  101. BATT_PROP_MAX,
  102. };
  103. enum usb_property_id {
  104. USB_ONLINE,
  105. USB_VOLT_NOW,
  106. USB_VOLT_MAX,
  107. USB_CURR_NOW,
  108. USB_CURR_MAX,
  109. USB_INPUT_CURR_LIMIT,
  110. USB_TYPE,
  111. USB_ADAP_TYPE,
  112. USB_MOISTURE_DET_EN,
  113. USB_MOISTURE_DET_STS,
  114. USB_TEMP,
  115. USB_REAL_TYPE,
  116. USB_TYPEC_COMPLIANT,
  117. USB_PROP_MAX,
  118. };
  119. enum wireless_property_id {
  120. WLS_ONLINE,
  121. WLS_VOLT_NOW,
  122. WLS_VOLT_MAX,
  123. WLS_CURR_NOW,
  124. WLS_CURR_MAX,
  125. WLS_TYPE,
  126. WLS_BOOST_EN,
  127. WLS_HBOOST_VMAX,
  128. WLS_INPUT_CURR_LIMIT,
  129. WLS_ADAP_TYPE,
  130. WLS_CONN_TEMP,
  131. WLS_PROP_MAX,
  132. };
  133. enum {
  134. QTI_POWER_SUPPLY_USB_TYPE_HVDCP = 0x80,
  135. QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3,
  136. QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3P5,
  137. };
  138. struct battery_charger_set_notify_msg {
  139. struct pmic_glink_hdr hdr;
  140. u32 battery_id;
  141. u32 power_state;
  142. u32 low_capacity;
  143. u32 high_capacity;
  144. };
  145. struct battery_charger_notify_msg {
  146. struct pmic_glink_hdr hdr;
  147. u32 notification;
  148. };
  149. struct battery_charger_req_msg {
  150. struct pmic_glink_hdr hdr;
  151. u32 battery_id;
  152. u32 property_id;
  153. u32 value;
  154. };
  155. struct battery_charger_resp_msg {
  156. struct pmic_glink_hdr hdr;
  157. u32 property_id;
  158. u32 value;
  159. u32 ret_code;
  160. };
  161. struct battery_model_resp_msg {
  162. struct pmic_glink_hdr hdr;
  163. u32 property_id;
  164. char model[MAX_STR_LEN];
  165. };
  166. struct wireless_fw_check_req {
  167. struct pmic_glink_hdr hdr;
  168. u32 fw_version;
  169. u32 fw_size;
  170. u32 fw_crc;
  171. };
  172. struct wireless_fw_check_resp {
  173. struct pmic_glink_hdr hdr;
  174. u32 ret_code;
  175. };
  176. struct wireless_fw_push_buf_req {
  177. struct pmic_glink_hdr hdr;
  178. u8 buf[WLS_FW_BUF_SIZE];
  179. u32 fw_chunk_id;
  180. };
  181. struct wireless_fw_push_buf_resp {
  182. struct pmic_glink_hdr hdr;
  183. u32 fw_update_status;
  184. };
  185. struct wireless_fw_update_status {
  186. struct pmic_glink_hdr hdr;
  187. u32 fw_update_done;
  188. };
  189. struct wireless_fw_get_version_req {
  190. struct pmic_glink_hdr hdr;
  191. };
  192. struct wireless_fw_get_version_resp {
  193. struct pmic_glink_hdr hdr;
  194. u32 fw_version;
  195. };
  196. struct battery_charger_ship_mode_req_msg {
  197. struct pmic_glink_hdr hdr;
  198. u32 ship_mode_type;
  199. };
  200. struct battery_charger_chg_ctrl_msg {
  201. struct pmic_glink_hdr hdr;
  202. u32 enable;
  203. u32 target_soc;
  204. u32 delta_soc;
  205. };
  206. struct psy_state {
  207. struct power_supply *psy;
  208. char *model;
  209. const int *map;
  210. u32 *prop;
  211. u32 prop_count;
  212. u32 opcode_get;
  213. u32 opcode_set;
  214. };
  215. struct battery_chg_dev {
  216. struct device *dev;
  217. struct class battery_class;
  218. struct pmic_glink_client *client;
  219. struct mutex rw_lock;
  220. struct rw_semaphore state_sem;
  221. struct completion ack;
  222. struct completion fw_buf_ack;
  223. struct completion fw_update_ack;
  224. struct psy_state psy_list[PSY_TYPE_MAX];
  225. struct dentry *debugfs_dir;
  226. void *notifier_cookie;
  227. u32 *thermal_levels;
  228. const char *wls_fw_name;
  229. int curr_thermal_level;
  230. int num_thermal_levels;
  231. int shutdown_volt_mv;
  232. atomic_t state;
  233. struct work_struct subsys_up_work;
  234. struct work_struct usb_type_work;
  235. struct work_struct battery_check_work;
  236. int fake_soc;
  237. bool block_tx;
  238. bool ship_mode_en;
  239. bool debug_battery_detected;
  240. bool wls_fw_update_reqd;
  241. u32 wls_fw_version;
  242. u16 wls_fw_crc;
  243. u32 wls_fw_update_time_ms;
  244. struct notifier_block reboot_notifier;
  245. u32 thermal_fcc_ua;
  246. u32 restrict_fcc_ua;
  247. u32 last_fcc_ua;
  248. u32 usb_icl_ua[NUM_USB_PORTS];
  249. u32 thermal_fcc_step;
  250. bool restrict_chg_en;
  251. u8 chg_ctrl_start_thr;
  252. u8 chg_ctrl_end_thr;
  253. bool chg_ctrl_en;
  254. bool usb_active[NUM_USB_PORTS];
  255. /* To track the driver initialization status */
  256. bool initialized;
  257. bool notify_en;
  258. bool error_prop;
  259. bool has_usb_2;
  260. };
  261. static const int battery_prop_map[BATT_PROP_MAX] = {
  262. [BATT_STATUS] = POWER_SUPPLY_PROP_STATUS,
  263. [BATT_HEALTH] = POWER_SUPPLY_PROP_HEALTH,
  264. [BATT_PRESENT] = POWER_SUPPLY_PROP_PRESENT,
  265. [BATT_CHG_TYPE] = POWER_SUPPLY_PROP_CHARGE_TYPE,
  266. [BATT_CAPACITY] = POWER_SUPPLY_PROP_CAPACITY,
  267. [BATT_VOLT_OCV] = POWER_SUPPLY_PROP_VOLTAGE_OCV,
  268. [BATT_VOLT_NOW] = POWER_SUPPLY_PROP_VOLTAGE_NOW,
  269. [BATT_VOLT_MAX] = POWER_SUPPLY_PROP_VOLTAGE_MAX,
  270. [BATT_CURR_NOW] = POWER_SUPPLY_PROP_CURRENT_NOW,
  271. [BATT_CHG_CTRL_LIM] = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
  272. [BATT_CHG_CTRL_LIM_MAX] = POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
  273. [BATT_TEMP] = POWER_SUPPLY_PROP_TEMP,
  274. [BATT_TECHNOLOGY] = POWER_SUPPLY_PROP_TECHNOLOGY,
  275. [BATT_CHG_COUNTER] = POWER_SUPPLY_PROP_CHARGE_COUNTER,
  276. [BATT_CYCLE_COUNT] = POWER_SUPPLY_PROP_CYCLE_COUNT,
  277. [BATT_CHG_FULL_DESIGN] = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  278. [BATT_CHG_FULL] = POWER_SUPPLY_PROP_CHARGE_FULL,
  279. [BATT_MODEL_NAME] = POWER_SUPPLY_PROP_MODEL_NAME,
  280. [BATT_TTF_AVG] = POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  281. [BATT_TTE_AVG] = POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  282. [BATT_POWER_NOW] = POWER_SUPPLY_PROP_POWER_NOW,
  283. [BATT_POWER_AVG] = POWER_SUPPLY_PROP_POWER_AVG,
  284. [BATT_CHG_CTRL_START_THR] = POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD,
  285. [BATT_CHG_CTRL_END_THR] = POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
  286. [BATT_CURR_AVG] = POWER_SUPPLY_PROP_CURRENT_AVG,
  287. };
  288. static const int usb_prop_map[USB_PROP_MAX] = {
  289. [USB_ONLINE] = POWER_SUPPLY_PROP_ONLINE,
  290. [USB_VOLT_NOW] = POWER_SUPPLY_PROP_VOLTAGE_NOW,
  291. [USB_VOLT_MAX] = POWER_SUPPLY_PROP_VOLTAGE_MAX,
  292. [USB_CURR_NOW] = POWER_SUPPLY_PROP_CURRENT_NOW,
  293. [USB_CURR_MAX] = POWER_SUPPLY_PROP_CURRENT_MAX,
  294. [USB_INPUT_CURR_LIMIT] = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  295. [USB_ADAP_TYPE] = POWER_SUPPLY_PROP_USB_TYPE,
  296. [USB_TEMP] = POWER_SUPPLY_PROP_TEMP,
  297. };
  298. static const int wls_prop_map[WLS_PROP_MAX] = {
  299. [WLS_ONLINE] = POWER_SUPPLY_PROP_ONLINE,
  300. [WLS_VOLT_NOW] = POWER_SUPPLY_PROP_VOLTAGE_NOW,
  301. [WLS_VOLT_MAX] = POWER_SUPPLY_PROP_VOLTAGE_MAX,
  302. [WLS_CURR_NOW] = POWER_SUPPLY_PROP_CURRENT_NOW,
  303. [WLS_CURR_MAX] = POWER_SUPPLY_PROP_CURRENT_MAX,
  304. [WLS_INPUT_CURR_LIMIT] = POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  305. [WLS_CONN_TEMP] = POWER_SUPPLY_PROP_TEMP,
  306. };
  307. /* Standard usb_type definitions similar to power_supply_sysfs.c */
  308. static const char * const power_supply_usb_type_text[] = {
  309. "Unknown", "SDP", "DCP", "CDP", "ACA", "C",
  310. "PD", "PD_DRP", "PD_PPS", "BrickID"
  311. };
  312. /* Custom usb_type definitions */
  313. static const char * const qc_power_supply_usb_type_text[] = {
  314. "HVDCP", "HVDCP_3", "HVDCP_3P5"
  315. };
  316. /* wireless_type definitions */
  317. static const char * const qc_power_supply_wls_type_text[] = {
  318. "Unknown", "BPP", "EPP", "HPP"
  319. };
  320. static RAW_NOTIFIER_HEAD(hboost_notifier);
  321. int register_hboost_event_notifier(struct notifier_block *nb)
  322. {
  323. return raw_notifier_chain_register(&hboost_notifier, nb);
  324. }
  325. EXPORT_SYMBOL(register_hboost_event_notifier);
  326. int unregister_hboost_event_notifier(struct notifier_block *nb)
  327. {
  328. return raw_notifier_chain_unregister(&hboost_notifier, nb);
  329. }
  330. EXPORT_SYMBOL(unregister_hboost_event_notifier);
  331. static int battery_chg_fw_write(struct battery_chg_dev *bcdev, void *data,
  332. int len)
  333. {
  334. int rc;
  335. down_read(&bcdev->state_sem);
  336. if (atomic_read(&bcdev->state) == PMIC_GLINK_STATE_DOWN) {
  337. pr_debug("glink state is down\n");
  338. up_read(&bcdev->state_sem);
  339. return -ENOTCONN;
  340. }
  341. reinit_completion(&bcdev->fw_buf_ack);
  342. rc = pmic_glink_write(bcdev->client, data, len);
  343. if (!rc) {
  344. rc = wait_for_completion_timeout(&bcdev->fw_buf_ack,
  345. msecs_to_jiffies(WLS_FW_WAIT_TIME_MS));
  346. if (!rc) {
  347. pr_err("Error, timed out sending message\n");
  348. up_read(&bcdev->state_sem);
  349. return -ETIMEDOUT;
  350. }
  351. rc = 0;
  352. }
  353. up_read(&bcdev->state_sem);
  354. return rc;
  355. }
  356. static int battery_chg_write(struct battery_chg_dev *bcdev, void *data,
  357. int len)
  358. {
  359. int rc;
  360. /*
  361. * When the subsystem goes down, it's better to return the last
  362. * known values until it comes back up. Hence, return 0 so that
  363. * pmic_glink_write() is not attempted until pmic glink is up.
  364. */
  365. down_read(&bcdev->state_sem);
  366. if (atomic_read(&bcdev->state) == PMIC_GLINK_STATE_DOWN) {
  367. pr_debug("glink state is down\n");
  368. up_read(&bcdev->state_sem);
  369. return 0;
  370. }
  371. if (bcdev->debug_battery_detected && bcdev->block_tx) {
  372. up_read(&bcdev->state_sem);
  373. return 0;
  374. }
  375. mutex_lock(&bcdev->rw_lock);
  376. reinit_completion(&bcdev->ack);
  377. bcdev->error_prop = false;
  378. rc = pmic_glink_write(bcdev->client, data, len);
  379. if (!rc) {
  380. rc = wait_for_completion_timeout(&bcdev->ack,
  381. msecs_to_jiffies(BC_WAIT_TIME_MS));
  382. if (!rc) {
  383. pr_err("Error, timed out sending message\n");
  384. up_read(&bcdev->state_sem);
  385. mutex_unlock(&bcdev->rw_lock);
  386. return -ETIMEDOUT;
  387. }
  388. rc = 0;
  389. /*
  390. * In case the opcode used is not supported, the remote
  391. * processor might ack it immediately with a return code indicating
  392. * an error. This additional check is to check if such an error has
  393. * happened and return immediately with error in that case. This
  394. * avoids wasting time waiting in the above timeout condition for this
  395. * type of error.
  396. */
  397. if (bcdev->error_prop) {
  398. bcdev->error_prop = false;
  399. rc = -ENODATA;
  400. }
  401. }
  402. mutex_unlock(&bcdev->rw_lock);
  403. up_read(&bcdev->state_sem);
  404. return rc;
  405. }
  406. static int write_property_id(struct battery_chg_dev *bcdev,
  407. struct psy_state *pst, u32 prop_id, u32 val)
  408. {
  409. struct battery_charger_req_msg req_msg = { { 0 } };
  410. req_msg.property_id = prop_id;
  411. req_msg.battery_id = 0;
  412. req_msg.value = val;
  413. req_msg.hdr.owner = MSG_OWNER_BC;
  414. req_msg.hdr.type = MSG_TYPE_REQ_RESP;
  415. req_msg.hdr.opcode = pst->opcode_set;
  416. if (pst->psy)
  417. pr_debug("psy: %s prop_id: %u val: %u\n", pst->psy->desc->name,
  418. req_msg.property_id, val);
  419. return battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
  420. }
  421. static int read_property_id(struct battery_chg_dev *bcdev,
  422. struct psy_state *pst, u32 prop_id)
  423. {
  424. struct battery_charger_req_msg req_msg = { { 0 } };
  425. req_msg.property_id = prop_id;
  426. req_msg.battery_id = 0;
  427. req_msg.value = 0;
  428. req_msg.hdr.owner = MSG_OWNER_BC;
  429. req_msg.hdr.type = MSG_TYPE_REQ_RESP;
  430. req_msg.hdr.opcode = pst->opcode_get;
  431. if (pst->psy)
  432. pr_debug("psy: %s prop_id: %u\n", pst->psy->desc->name,
  433. req_msg.property_id);
  434. return battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
  435. }
  436. static int get_property_id(struct psy_state *pst,
  437. enum power_supply_property prop)
  438. {
  439. u32 i;
  440. for (i = 0; i < pst->prop_count; i++)
  441. if (pst->map[i] == prop)
  442. return i;
  443. if (pst->psy)
  444. pr_err("No property id for property %d in psy %s\n", prop,
  445. pst->psy->desc->name);
  446. return -ENOENT;
  447. }
  448. static void battery_chg_notify_disable(struct battery_chg_dev *bcdev)
  449. {
  450. struct battery_charger_set_notify_msg req_msg = { { 0 } };
  451. int rc;
  452. if (bcdev->notify_en) {
  453. /* Send request to disable notification */
  454. req_msg.hdr.owner = MSG_OWNER_BC;
  455. req_msg.hdr.type = MSG_TYPE_NOTIFY;
  456. req_msg.hdr.opcode = BC_DISABLE_NOTIFY_REQ;
  457. rc = battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
  458. if (rc < 0)
  459. pr_err("Failed to disable notification rc=%d\n", rc);
  460. else
  461. bcdev->notify_en = false;
  462. }
  463. }
  464. static void battery_chg_notify_enable(struct battery_chg_dev *bcdev)
  465. {
  466. struct battery_charger_set_notify_msg req_msg = { { 0 } };
  467. int rc;
  468. if (!bcdev->notify_en) {
  469. /* Send request to enable notification */
  470. req_msg.hdr.owner = MSG_OWNER_BC;
  471. req_msg.hdr.type = MSG_TYPE_NOTIFY;
  472. req_msg.hdr.opcode = BC_SET_NOTIFY_REQ;
  473. rc = battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
  474. if (rc < 0)
  475. pr_err("Failed to enable notification rc=%d\n", rc);
  476. else
  477. bcdev->notify_en = true;
  478. }
  479. }
  480. static void battery_chg_state_cb(void *priv, enum pmic_glink_state state)
  481. {
  482. struct battery_chg_dev *bcdev = priv;
  483. pr_debug("state: %d\n", state);
  484. down_write(&bcdev->state_sem);
  485. if (!bcdev->initialized) {
  486. pr_warn("Driver not initialized, pmic_glink state %d\n", state);
  487. up_write(&bcdev->state_sem);
  488. return;
  489. }
  490. atomic_set(&bcdev->state, state);
  491. up_write(&bcdev->state_sem);
  492. if (state == PMIC_GLINK_STATE_UP)
  493. schedule_work(&bcdev->subsys_up_work);
  494. else if (state == PMIC_GLINK_STATE_DOWN)
  495. bcdev->notify_en = false;
  496. }
  497. /**
  498. * qti_battery_charger_get_prop() - Gets the property being requested
  499. *
  500. * @name: Power supply name
  501. * @prop_id: Property id to be read
  502. * @val: Pointer to value that needs to be updated
  503. *
  504. * Return: 0 if success, negative on error.
  505. */
  506. int qti_battery_charger_get_prop(const char *name,
  507. enum battery_charger_prop prop_id, int *val)
  508. {
  509. struct power_supply *psy;
  510. struct battery_chg_dev *bcdev;
  511. struct psy_state *pst;
  512. int rc = 0;
  513. if (prop_id >= BATTERY_CHARGER_PROP_MAX)
  514. return -EINVAL;
  515. if (strcmp(name, "battery") && strcmp(name, "usb") &&
  516. strcmp(name, "usb-2") && strcmp(name, "wireless"))
  517. return -EINVAL;
  518. psy = power_supply_get_by_name(name);
  519. if (!psy)
  520. return -ENODEV;
  521. bcdev = power_supply_get_drvdata(psy);
  522. if (!bcdev)
  523. return -ENODEV;
  524. power_supply_put(psy);
  525. switch (prop_id) {
  526. case BATTERY_RESISTANCE:
  527. pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  528. rc = read_property_id(bcdev, pst, BATT_RESISTANCE);
  529. if (!rc)
  530. *val = pst->prop[BATT_RESISTANCE];
  531. break;
  532. default:
  533. break;
  534. }
  535. return rc;
  536. }
  537. EXPORT_SYMBOL(qti_battery_charger_get_prop);
  538. static bool validate_message(struct battery_chg_dev *bcdev,
  539. struct battery_charger_resp_msg *resp_msg, size_t len)
  540. {
  541. if (len != sizeof(*resp_msg)) {
  542. pr_err("Incorrect response length %zu for opcode %#x\n", len,
  543. resp_msg->hdr.opcode);
  544. return false;
  545. }
  546. if (resp_msg->ret_code) {
  547. pr_err_ratelimited("Error in response for opcode %#x prop_id %u, rc=%d\n",
  548. resp_msg->hdr.opcode, resp_msg->property_id,
  549. (int)resp_msg->ret_code);
  550. bcdev->error_prop = true;
  551. return false;
  552. }
  553. return true;
  554. }
  555. #define MODEL_DEBUG_BOARD "Debug_Board"
  556. static void handle_message(struct battery_chg_dev *bcdev, void *data,
  557. size_t len)
  558. {
  559. struct battery_charger_resp_msg *resp_msg = data;
  560. struct battery_model_resp_msg *model_resp_msg = data;
  561. struct wireless_fw_check_resp *fw_check_msg;
  562. struct wireless_fw_push_buf_resp *fw_resp_msg;
  563. struct wireless_fw_update_status *fw_update_msg;
  564. struct wireless_fw_get_version_resp *fw_ver_msg;
  565. struct psy_state *pst;
  566. bool ack_set = false;
  567. switch (resp_msg->hdr.opcode) {
  568. case BC_BATTERY_STATUS_GET:
  569. pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  570. /* Handle model response uniquely as it's a string */
  571. if (pst->model && len == sizeof(*model_resp_msg)) {
  572. memcpy(pst->model, model_resp_msg->model, MAX_STR_LEN);
  573. ack_set = true;
  574. bcdev->debug_battery_detected = !strcmp(pst->model,
  575. MODEL_DEBUG_BOARD);
  576. break;
  577. }
  578. /* Other response should be of same type as they've u32 value */
  579. if (validate_message(bcdev, resp_msg, len) &&
  580. resp_msg->property_id < pst->prop_count) {
  581. pst->prop[resp_msg->property_id] = resp_msg->value;
  582. ack_set = true;
  583. }
  584. break;
  585. case BC_USB_STATUS_GET(USB_1_PORT_ID):
  586. pst = &bcdev->psy_list[PSY_TYPE_USB];
  587. if (validate_message(bcdev, resp_msg, len) &&
  588. resp_msg->property_id < pst->prop_count) {
  589. pst->prop[resp_msg->property_id] = resp_msg->value;
  590. ack_set = true;
  591. }
  592. break;
  593. case BC_USB_STATUS_GET(USB_2_PORT_ID):
  594. if (!bcdev->has_usb_2) {
  595. pr_debug("opcode: %u for missing USB_2 port\n",
  596. resp_msg->hdr.opcode);
  597. break;
  598. }
  599. pst = &bcdev->psy_list[PSY_TYPE_USB_2];
  600. if (validate_message(bcdev, resp_msg, len) &&
  601. resp_msg->property_id < pst->prop_count) {
  602. pst->prop[resp_msg->property_id] = resp_msg->value;
  603. ack_set = true;
  604. }
  605. break;
  606. case BC_WLS_STATUS_GET:
  607. pst = &bcdev->psy_list[PSY_TYPE_WLS];
  608. if (validate_message(bcdev, resp_msg, len) &&
  609. resp_msg->property_id < pst->prop_count) {
  610. pst->prop[resp_msg->property_id] = resp_msg->value;
  611. ack_set = true;
  612. }
  613. break;
  614. case BC_USB_STATUS_SET(USB_2_PORT_ID):
  615. if (!bcdev->has_usb_2) {
  616. pr_debug("opcode: %u for missing USB_2 port\n",
  617. resp_msg->hdr.opcode);
  618. break;
  619. }
  620. fallthrough;
  621. case BC_BATTERY_STATUS_SET:
  622. case BC_USB_STATUS_SET(USB_1_PORT_ID):
  623. case BC_WLS_STATUS_SET:
  624. if (validate_message(bcdev, data, len))
  625. ack_set = true;
  626. break;
  627. case BC_SET_NOTIFY_REQ:
  628. case BC_DISABLE_NOTIFY_REQ:
  629. case BC_SHUTDOWN_NOTIFY:
  630. case BC_SHIP_MODE_REQ_SET:
  631. case BC_CHG_CTRL_LIMIT_EN:
  632. /* Always ACK response for notify or ship_mode request */
  633. ack_set = true;
  634. break;
  635. case BC_WLS_FW_CHECK_UPDATE:
  636. if (len == sizeof(*fw_check_msg)) {
  637. fw_check_msg = data;
  638. if (fw_check_msg->ret_code == 1)
  639. bcdev->wls_fw_update_reqd = true;
  640. ack_set = true;
  641. } else {
  642. pr_err("Incorrect response length %zu for wls_fw_check_update\n",
  643. len);
  644. }
  645. break;
  646. case BC_WLS_FW_PUSH_BUF_RESP:
  647. if (len == sizeof(*fw_resp_msg)) {
  648. fw_resp_msg = data;
  649. if (fw_resp_msg->fw_update_status == 1)
  650. complete(&bcdev->fw_buf_ack);
  651. } else {
  652. pr_err("Incorrect response length %zu for wls_fw_push_buf_resp\n",
  653. len);
  654. }
  655. break;
  656. case BC_WLS_FW_UPDATE_STATUS_RESP:
  657. if (len == sizeof(*fw_update_msg)) {
  658. fw_update_msg = data;
  659. if (fw_update_msg->fw_update_done == 1)
  660. complete(&bcdev->fw_update_ack);
  661. else
  662. pr_err("Wireless FW update not done %d\n",
  663. (int)fw_update_msg->fw_update_done);
  664. } else {
  665. pr_err("Incorrect response length %zu for wls_fw_update_status_resp\n",
  666. len);
  667. }
  668. break;
  669. case BC_WLS_FW_GET_VERSION:
  670. if (len == sizeof(*fw_ver_msg)) {
  671. fw_ver_msg = data;
  672. bcdev->wls_fw_version = fw_ver_msg->fw_version;
  673. ack_set = true;
  674. } else {
  675. pr_err("Incorrect response length %zu for wls_fw_get_version\n",
  676. len);
  677. }
  678. break;
  679. default:
  680. pr_err("Unknown opcode: %u\n", resp_msg->hdr.opcode);
  681. break;
  682. }
  683. if (ack_set || bcdev->error_prop)
  684. complete(&bcdev->ack);
  685. }
  686. static struct power_supply_desc usb_psy_desc[NUM_USB_PORTS];
  687. static void battery_chg_update_usb_type_work(struct work_struct *work)
  688. {
  689. struct battery_chg_dev *bcdev = container_of(work,
  690. struct battery_chg_dev, usb_type_work);
  691. struct power_supply_desc *desc;
  692. struct psy_state *pst;
  693. int rc, i;
  694. for (i = 0; i < NUM_USB_PORTS; i++) {
  695. if (!bcdev->usb_active[i])
  696. continue;
  697. bcdev->usb_active[i] = false;
  698. switch (i) {
  699. case USB_1_PORT_ID:
  700. pst = &bcdev->psy_list[PSY_TYPE_USB];
  701. break;
  702. case USB_2_PORT_ID:
  703. pst = &bcdev->psy_list[PSY_TYPE_USB_2];
  704. break;
  705. default:
  706. pr_err_ratelimited("USB port %d not supported\n", i);
  707. continue;
  708. }
  709. desc = &usb_psy_desc[i];
  710. rc = read_property_id(bcdev, pst, USB_ADAP_TYPE);
  711. if (rc < 0) {
  712. pr_err("Failed to read USB_ADAP_TYPE rc=%d\n", rc);
  713. continue;
  714. }
  715. /* Reset usb_icl_ua whenever USB adapter type changes */
  716. if (pst->prop[USB_ADAP_TYPE] != POWER_SUPPLY_USB_TYPE_SDP &&
  717. pst->prop[USB_ADAP_TYPE] != POWER_SUPPLY_USB_TYPE_PD)
  718. bcdev->usb_icl_ua[i] = 0;
  719. pr_debug("usb_adap_type: %u\n", pst->prop[USB_ADAP_TYPE]);
  720. switch (pst->prop[USB_ADAP_TYPE]) {
  721. case POWER_SUPPLY_USB_TYPE_SDP:
  722. desc->type = POWER_SUPPLY_TYPE_USB;
  723. break;
  724. case POWER_SUPPLY_USB_TYPE_DCP:
  725. case POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID:
  726. case QTI_POWER_SUPPLY_USB_TYPE_HVDCP:
  727. case QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3:
  728. case QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3P5:
  729. desc->type = POWER_SUPPLY_TYPE_USB_DCP;
  730. break;
  731. case POWER_SUPPLY_USB_TYPE_CDP:
  732. desc->type = POWER_SUPPLY_TYPE_USB_CDP;
  733. break;
  734. case POWER_SUPPLY_USB_TYPE_ACA:
  735. desc->type = POWER_SUPPLY_TYPE_USB_ACA;
  736. break;
  737. case POWER_SUPPLY_USB_TYPE_C:
  738. desc->type = POWER_SUPPLY_TYPE_USB_TYPE_C;
  739. break;
  740. case POWER_SUPPLY_USB_TYPE_PD:
  741. case POWER_SUPPLY_USB_TYPE_PD_DRP:
  742. case POWER_SUPPLY_USB_TYPE_PD_PPS:
  743. desc->type = POWER_SUPPLY_TYPE_USB_PD;
  744. break;
  745. default:
  746. desc->type = POWER_SUPPLY_TYPE_USB;
  747. break;
  748. }
  749. }
  750. }
  751. static void battery_chg_check_status_work(struct work_struct *work)
  752. {
  753. struct battery_chg_dev *bcdev = container_of(work,
  754. struct battery_chg_dev,
  755. battery_check_work);
  756. struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  757. int rc;
  758. rc = read_property_id(bcdev, pst, BATT_STATUS);
  759. if (rc < 0) {
  760. pr_err("Failed to read BATT_STATUS, rc=%d\n", rc);
  761. return;
  762. }
  763. if (pst->prop[BATT_STATUS] == POWER_SUPPLY_STATUS_CHARGING) {
  764. pr_debug("Battery is charging\n");
  765. return;
  766. }
  767. rc = read_property_id(bcdev, pst, BATT_CAPACITY);
  768. if (rc < 0) {
  769. pr_err("Failed to read BATT_CAPACITY, rc=%d\n", rc);
  770. return;
  771. }
  772. if (DIV_ROUND_CLOSEST(pst->prop[BATT_CAPACITY], 100) > 0) {
  773. pr_debug("Battery SOC is > 0\n");
  774. return;
  775. }
  776. /*
  777. * If we are here, then battery is not charging and SOC is 0.
  778. * Check the battery voltage and if it's lower than shutdown voltage,
  779. * then initiate an emergency shutdown.
  780. */
  781. rc = read_property_id(bcdev, pst, BATT_VOLT_NOW);
  782. if (rc < 0) {
  783. pr_err("Failed to read BATT_VOLT_NOW, rc=%d\n", rc);
  784. return;
  785. }
  786. if (pst->prop[BATT_VOLT_NOW] / 1000 > bcdev->shutdown_volt_mv) {
  787. pr_debug("Battery voltage is > %d mV\n",
  788. bcdev->shutdown_volt_mv);
  789. return;
  790. }
  791. pr_emerg("Initiating a shutdown in 100 ms\n");
  792. msleep(100);
  793. pr_emerg("Attempting kernel_power_off: Battery voltage low\n");
  794. kernel_power_off();
  795. }
  796. static void handle_notification(struct battery_chg_dev *bcdev, void *data,
  797. size_t len)
  798. {
  799. struct battery_charger_notify_msg *notify_msg = data;
  800. struct psy_state *pst = NULL;
  801. u32 hboost_vmax_mv, notification;
  802. if (len != sizeof(*notify_msg)) {
  803. pr_err("Incorrect response length %zu\n", len);
  804. return;
  805. }
  806. notification = notify_msg->notification;
  807. pr_debug("notification: %#x\n", notification);
  808. if ((notification & 0xffff) == BC_HBOOST_VMAX_CLAMP_NOTIFY) {
  809. hboost_vmax_mv = (notification >> 16) & 0xffff;
  810. raw_notifier_call_chain(&hboost_notifier, VMAX_CLAMP, &hboost_vmax_mv);
  811. pr_debug("hBoost is clamped at %u mV\n", hboost_vmax_mv);
  812. return;
  813. }
  814. switch (notification) {
  815. case BC_BATTERY_STATUS_GET:
  816. case BC_GENERIC_NOTIFY:
  817. pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  818. if (bcdev->shutdown_volt_mv > 0)
  819. schedule_work(&bcdev->battery_check_work);
  820. break;
  821. case BC_USB_STATUS_GET(USB_1_PORT_ID):
  822. bcdev->usb_active[USB_1_PORT_ID] = true;
  823. pst = &bcdev->psy_list[PSY_TYPE_USB];
  824. schedule_work(&bcdev->usb_type_work);
  825. break;
  826. case BC_USB_STATUS_GET(USB_2_PORT_ID):
  827. if (!bcdev->has_usb_2) {
  828. pr_debug("notification: %u for missing USB_2 port\n",
  829. notification);
  830. break;
  831. }
  832. bcdev->usb_active[USB_2_PORT_ID] = true;
  833. pst = &bcdev->psy_list[PSY_TYPE_USB_2];
  834. schedule_work(&bcdev->usb_type_work);
  835. break;
  836. case BC_WLS_STATUS_GET:
  837. pst = &bcdev->psy_list[PSY_TYPE_WLS];
  838. break;
  839. default:
  840. break;
  841. }
  842. if (pst && pst->psy) {
  843. /*
  844. * For charger mode, keep the device awake at least for 50 ms
  845. * so that device won't enter suspend when a non-SDP charger
  846. * is removed. This would allow the userspace process like
  847. * "charger" to be able to read power supply uevents to take
  848. * appropriate actions (e.g. shutting down when the charger is
  849. * unplugged).
  850. */
  851. power_supply_changed(pst->psy);
  852. pm_wakeup_dev_event(bcdev->dev, 50, true);
  853. }
  854. }
  855. static int battery_chg_callback(void *priv, void *data, size_t len)
  856. {
  857. struct pmic_glink_hdr *hdr = data;
  858. struct battery_chg_dev *bcdev = priv;
  859. pr_debug("owner: %u type: %u opcode: %#x len: %zu\n", hdr->owner,
  860. hdr->type, hdr->opcode, len);
  861. down_read(&bcdev->state_sem);
  862. if (!bcdev->initialized) {
  863. pr_debug("Driver initialization failed: Dropping glink callback message: state %d\n",
  864. bcdev->state);
  865. up_read(&bcdev->state_sem);
  866. return 0;
  867. }
  868. if (hdr->opcode == BC_NOTIFY_IND)
  869. handle_notification(bcdev, data, len);
  870. else
  871. handle_message(bcdev, data, len);
  872. up_read(&bcdev->state_sem);
  873. return 0;
  874. }
  875. static int wls_psy_get_prop(struct power_supply *psy,
  876. enum power_supply_property prop,
  877. union power_supply_propval *pval)
  878. {
  879. struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
  880. struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_WLS];
  881. int prop_id, rc;
  882. pval->intval = -ENODATA;
  883. prop_id = get_property_id(pst, prop);
  884. if (prop_id < 0)
  885. return prop_id;
  886. rc = read_property_id(bcdev, pst, prop_id);
  887. if (rc < 0)
  888. return rc;
  889. pval->intval = pst->prop[prop_id];
  890. return 0;
  891. }
  892. static int wls_psy_set_prop(struct power_supply *psy,
  893. enum power_supply_property prop,
  894. const union power_supply_propval *pval)
  895. {
  896. return 0;
  897. }
  898. static int wls_psy_prop_is_writeable(struct power_supply *psy,
  899. enum power_supply_property prop)
  900. {
  901. return 0;
  902. }
  903. static enum power_supply_property wls_props[] = {
  904. POWER_SUPPLY_PROP_ONLINE,
  905. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  906. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  907. POWER_SUPPLY_PROP_CURRENT_NOW,
  908. POWER_SUPPLY_PROP_CURRENT_MAX,
  909. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  910. POWER_SUPPLY_PROP_TEMP,
  911. };
  912. static const struct power_supply_desc wls_psy_desc = {
  913. .name = "wireless",
  914. .type = POWER_SUPPLY_TYPE_WIRELESS,
  915. .properties = wls_props,
  916. .num_properties = ARRAY_SIZE(wls_props),
  917. .get_property = wls_psy_get_prop,
  918. .set_property = wls_psy_set_prop,
  919. .property_is_writeable = wls_psy_prop_is_writeable,
  920. };
  921. static const char *get_wls_type_name(u32 wls_type)
  922. {
  923. if (wls_type >= ARRAY_SIZE(qc_power_supply_wls_type_text))
  924. return "Unknown";
  925. return qc_power_supply_wls_type_text[wls_type];
  926. }
  927. static const char *get_usb_type_name(u32 usb_type)
  928. {
  929. u32 i;
  930. if (usb_type >= QTI_POWER_SUPPLY_USB_TYPE_HVDCP &&
  931. usb_type <= QTI_POWER_SUPPLY_USB_TYPE_HVDCP_3P5) {
  932. for (i = 0; i < ARRAY_SIZE(qc_power_supply_usb_type_text);
  933. i++) {
  934. if (i == (usb_type - QTI_POWER_SUPPLY_USB_TYPE_HVDCP))
  935. return qc_power_supply_usb_type_text[i];
  936. }
  937. return "Unknown";
  938. }
  939. for (i = 0; i < ARRAY_SIZE(power_supply_usb_type_text); i++) {
  940. if (i == usb_type)
  941. return power_supply_usb_type_text[i];
  942. }
  943. return "Unknown";
  944. }
  945. static int usb_psy_set_icl(struct battery_chg_dev *bcdev,
  946. struct psy_state *pst, u32 prop_id, int val)
  947. {
  948. u32 temp;
  949. int rc;
  950. rc = read_property_id(bcdev, pst, USB_ADAP_TYPE);
  951. if (rc < 0) {
  952. pr_err("Failed to read prop USB_ADAP_TYPE, rc=%d\n", rc);
  953. return rc;
  954. }
  955. /* Allow this only for SDP, CDP or USB_PD and not for other charger types */
  956. switch (pst->prop[USB_ADAP_TYPE]) {
  957. case POWER_SUPPLY_USB_TYPE_SDP:
  958. case POWER_SUPPLY_USB_TYPE_PD:
  959. case POWER_SUPPLY_USB_TYPE_CDP:
  960. break;
  961. default:
  962. return -EINVAL;
  963. }
  964. /*
  965. * Input current limit (ICL) can be set by different clients. E.g. USB
  966. * driver can request for a current of 500/900 mA depending on the
  967. * port type. Also, clients like EUD driver can pass 0 or -22 to
  968. * suspend or unsuspend the input for its use case.
  969. */
  970. temp = val;
  971. if (val < 0)
  972. temp = UINT_MAX;
  973. rc = write_property_id(bcdev, pst, prop_id, temp);
  974. if (rc < 0) {
  975. pr_err("Failed to set ICL (%u uA) rc=%d\n", temp, rc);
  976. } else {
  977. pr_debug("Set ICL to %u\n", temp);
  978. if (!strcmp(pst->psy->desc->name, "usb-2"))
  979. bcdev->usb_icl_ua[USB_2_PORT_ID] = temp;
  980. else
  981. bcdev->usb_icl_ua[USB_1_PORT_ID] = temp;
  982. }
  983. return rc;
  984. }
  985. static int usb_psy_get_prop(struct power_supply *psy,
  986. enum power_supply_property prop,
  987. union power_supply_propval *pval)
  988. {
  989. struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
  990. struct psy_state *pst;
  991. int prop_id, rc;
  992. if (!strcmp(psy->desc->name, "usb-2"))
  993. pst = &bcdev->psy_list[PSY_TYPE_USB_2];
  994. else
  995. pst = &bcdev->psy_list[PSY_TYPE_USB];
  996. pval->intval = -ENODATA;
  997. prop_id = get_property_id(pst, prop);
  998. if (prop_id < 0)
  999. return prop_id;
  1000. rc = read_property_id(bcdev, pst, prop_id);
  1001. if (rc < 0)
  1002. return rc;
  1003. pval->intval = pst->prop[prop_id];
  1004. if (prop == POWER_SUPPLY_PROP_TEMP)
  1005. pval->intval = DIV_ROUND_CLOSEST((int)pval->intval, 10);
  1006. return 0;
  1007. }
  1008. static int usb_psy_set_prop(struct power_supply *psy,
  1009. enum power_supply_property prop,
  1010. const union power_supply_propval *pval)
  1011. {
  1012. struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
  1013. struct psy_state *pst;
  1014. int prop_id, rc = 0;
  1015. if (!strcmp(psy->desc->name, "usb-2"))
  1016. pst = &bcdev->psy_list[PSY_TYPE_USB_2];
  1017. else
  1018. pst = &bcdev->psy_list[PSY_TYPE_USB];
  1019. prop_id = get_property_id(pst, prop);
  1020. if (prop_id < 0)
  1021. return prop_id;
  1022. switch (prop) {
  1023. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  1024. rc = usb_psy_set_icl(bcdev, pst, prop_id, pval->intval);
  1025. break;
  1026. default:
  1027. break;
  1028. }
  1029. return rc;
  1030. }
  1031. static int usb_psy_prop_is_writeable(struct power_supply *psy,
  1032. enum power_supply_property prop)
  1033. {
  1034. switch (prop) {
  1035. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  1036. return 1;
  1037. default:
  1038. break;
  1039. }
  1040. return 0;
  1041. }
  1042. static enum power_supply_property usb_props[] = {
  1043. POWER_SUPPLY_PROP_ONLINE,
  1044. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  1045. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  1046. POWER_SUPPLY_PROP_CURRENT_NOW,
  1047. POWER_SUPPLY_PROP_CURRENT_MAX,
  1048. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  1049. POWER_SUPPLY_PROP_USB_TYPE,
  1050. POWER_SUPPLY_PROP_TEMP,
  1051. };
  1052. static enum power_supply_usb_type usb_psy_supported_types[] = {
  1053. POWER_SUPPLY_USB_TYPE_UNKNOWN,
  1054. POWER_SUPPLY_USB_TYPE_SDP,
  1055. POWER_SUPPLY_USB_TYPE_DCP,
  1056. POWER_SUPPLY_USB_TYPE_CDP,
  1057. POWER_SUPPLY_USB_TYPE_ACA,
  1058. POWER_SUPPLY_USB_TYPE_C,
  1059. POWER_SUPPLY_USB_TYPE_PD,
  1060. POWER_SUPPLY_USB_TYPE_PD_DRP,
  1061. POWER_SUPPLY_USB_TYPE_PD_PPS,
  1062. POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID,
  1063. };
  1064. static struct power_supply_desc usb_psy_desc[NUM_USB_PORTS] = {
  1065. [USB_1_PORT_ID] = {
  1066. .name = "usb",
  1067. .type = POWER_SUPPLY_TYPE_USB,
  1068. .properties = usb_props,
  1069. .num_properties = ARRAY_SIZE(usb_props),
  1070. .get_property = usb_psy_get_prop,
  1071. .set_property = usb_psy_set_prop,
  1072. .usb_types = usb_psy_supported_types,
  1073. .num_usb_types = ARRAY_SIZE(usb_psy_supported_types),
  1074. .property_is_writeable = usb_psy_prop_is_writeable,
  1075. },
  1076. [USB_2_PORT_ID] = {
  1077. .name = "usb-2",
  1078. .type = POWER_SUPPLY_TYPE_USB,
  1079. .properties = usb_props,
  1080. .num_properties = ARRAY_SIZE(usb_props),
  1081. .get_property = usb_psy_get_prop,
  1082. .set_property = usb_psy_set_prop,
  1083. .usb_types = usb_psy_supported_types,
  1084. .num_usb_types = ARRAY_SIZE(usb_psy_supported_types),
  1085. .property_is_writeable = usb_psy_prop_is_writeable,
  1086. }
  1087. };
  1088. #define CHARGE_CTRL_START_THR_MIN 50
  1089. #define CHARGE_CTRL_START_THR_MAX 95
  1090. #define CHARGE_CTRL_END_THR_MIN 55
  1091. #define CHARGE_CTRL_END_THR_MAX 100
  1092. #define CHARGE_CTRL_DELTA_SOC 5
  1093. static int battery_psy_set_charge_threshold(struct battery_chg_dev *bcdev,
  1094. u32 target_soc, u32 delta_soc)
  1095. {
  1096. struct battery_charger_chg_ctrl_msg msg = { { 0 } };
  1097. int rc;
  1098. if (!bcdev->chg_ctrl_en)
  1099. return 0;
  1100. if (target_soc > CHARGE_CTRL_END_THR_MAX)
  1101. target_soc = CHARGE_CTRL_END_THR_MAX;
  1102. msg.hdr.owner = MSG_OWNER_BC;
  1103. msg.hdr.type = MSG_TYPE_REQ_RESP;
  1104. msg.hdr.opcode = BC_CHG_CTRL_LIMIT_EN;
  1105. msg.enable = 1;
  1106. msg.target_soc = target_soc;
  1107. msg.delta_soc = delta_soc;
  1108. rc = battery_chg_write(bcdev, &msg, sizeof(msg));
  1109. if (rc < 0)
  1110. pr_err("Failed to set charge_control thresholds, rc=%d\n", rc);
  1111. else
  1112. pr_debug("target_soc: %u delta_soc: %u\n", target_soc, delta_soc);
  1113. return rc;
  1114. }
  1115. static int battery_psy_set_charge_end_threshold(struct battery_chg_dev *bcdev,
  1116. int val)
  1117. {
  1118. u32 delta_soc = CHARGE_CTRL_DELTA_SOC;
  1119. int rc;
  1120. if (val < CHARGE_CTRL_END_THR_MIN ||
  1121. val > CHARGE_CTRL_END_THR_MAX) {
  1122. pr_err("Charge control end_threshold should be within [%u %u]\n",
  1123. CHARGE_CTRL_END_THR_MIN, CHARGE_CTRL_END_THR_MAX);
  1124. return -EINVAL;
  1125. }
  1126. if (bcdev->chg_ctrl_start_thr && val > bcdev->chg_ctrl_start_thr)
  1127. delta_soc = val - bcdev->chg_ctrl_start_thr;
  1128. rc = battery_psy_set_charge_threshold(bcdev, val, delta_soc);
  1129. if (rc < 0)
  1130. pr_err("Failed to set charge control end threshold %u, rc=%d\n",
  1131. val, rc);
  1132. else
  1133. bcdev->chg_ctrl_end_thr = val;
  1134. return rc;
  1135. }
  1136. static int battery_psy_set_charge_start_threshold(struct battery_chg_dev *bcdev,
  1137. int val)
  1138. {
  1139. u32 target_soc, delta_soc;
  1140. int rc;
  1141. if (val < CHARGE_CTRL_START_THR_MIN ||
  1142. val > CHARGE_CTRL_START_THR_MAX) {
  1143. pr_err("Charge control start_threshold should be within [%u %u]\n",
  1144. CHARGE_CTRL_START_THR_MIN, CHARGE_CTRL_START_THR_MAX);
  1145. return -EINVAL;
  1146. }
  1147. if (val > bcdev->chg_ctrl_end_thr) {
  1148. target_soc = val + CHARGE_CTRL_DELTA_SOC;
  1149. delta_soc = CHARGE_CTRL_DELTA_SOC;
  1150. } else {
  1151. target_soc = bcdev->chg_ctrl_end_thr;
  1152. delta_soc = bcdev->chg_ctrl_end_thr - val;
  1153. }
  1154. rc = battery_psy_set_charge_threshold(bcdev, target_soc, delta_soc);
  1155. if (rc < 0)
  1156. pr_err("Failed to set charge control start threshold %u, rc=%d\n",
  1157. val, rc);
  1158. else
  1159. bcdev->chg_ctrl_start_thr = val;
  1160. return rc;
  1161. }
  1162. static int get_charge_control_en(struct battery_chg_dev *bcdev)
  1163. {
  1164. int rc;
  1165. rc = read_property_id(bcdev, &bcdev->psy_list[PSY_TYPE_BATTERY],
  1166. BATT_CHG_CTRL_EN);
  1167. if (rc < 0)
  1168. pr_err("Failed to read the CHG_CTRL_EN, rc = %d\n", rc);
  1169. else
  1170. bcdev->chg_ctrl_en =
  1171. bcdev->psy_list[PSY_TYPE_BATTERY].prop[BATT_CHG_CTRL_EN];
  1172. return rc;
  1173. }
  1174. static int __battery_psy_set_charge_current(struct battery_chg_dev *bcdev,
  1175. u32 fcc_ua)
  1176. {
  1177. int rc;
  1178. if (bcdev->restrict_chg_en) {
  1179. fcc_ua = min_t(u32, fcc_ua, bcdev->restrict_fcc_ua);
  1180. fcc_ua = min_t(u32, fcc_ua, bcdev->thermal_fcc_ua);
  1181. }
  1182. rc = write_property_id(bcdev, &bcdev->psy_list[PSY_TYPE_BATTERY],
  1183. BATT_CHG_CTRL_LIM, fcc_ua);
  1184. if (rc < 0) {
  1185. pr_err("Failed to set FCC %u, rc=%d\n", fcc_ua, rc);
  1186. } else {
  1187. pr_debug("Set FCC to %u uA\n", fcc_ua);
  1188. bcdev->last_fcc_ua = fcc_ua;
  1189. }
  1190. return rc;
  1191. }
  1192. static int battery_psy_set_charge_current(struct battery_chg_dev *bcdev,
  1193. int val)
  1194. {
  1195. int rc;
  1196. u32 fcc_ua, prev_fcc_ua;
  1197. if (!bcdev->num_thermal_levels)
  1198. return 0;
  1199. if (bcdev->num_thermal_levels < 0) {
  1200. pr_err("Incorrect num_thermal_levels\n");
  1201. return -EINVAL;
  1202. }
  1203. if (val < 0 || val > bcdev->num_thermal_levels)
  1204. return -EINVAL;
  1205. if (bcdev->thermal_fcc_step == 0)
  1206. fcc_ua = bcdev->thermal_levels[val];
  1207. else
  1208. fcc_ua = bcdev->psy_list[PSY_TYPE_BATTERY].prop[BATT_CHG_CTRL_LIM_MAX]
  1209. - (bcdev->thermal_fcc_step * val);
  1210. prev_fcc_ua = bcdev->thermal_fcc_ua;
  1211. bcdev->thermal_fcc_ua = fcc_ua;
  1212. rc = __battery_psy_set_charge_current(bcdev, fcc_ua);
  1213. if (!rc)
  1214. bcdev->curr_thermal_level = val;
  1215. else
  1216. bcdev->thermal_fcc_ua = prev_fcc_ua;
  1217. return rc;
  1218. }
  1219. static int battery_psy_get_prop(struct power_supply *psy,
  1220. enum power_supply_property prop,
  1221. union power_supply_propval *pval)
  1222. {
  1223. struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
  1224. struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  1225. int prop_id, rc;
  1226. pval->intval = -ENODATA;
  1227. /*
  1228. * The prop id of TIME_TO_FULL_NOW and TIME_TO_FULL_AVG is same.
  1229. * So, map the prop id of TIME_TO_FULL_AVG for TIME_TO_FULL_NOW.
  1230. */
  1231. if (prop == POWER_SUPPLY_PROP_TIME_TO_FULL_NOW)
  1232. prop = POWER_SUPPLY_PROP_TIME_TO_FULL_AVG;
  1233. prop_id = get_property_id(pst, prop);
  1234. if (prop_id < 0)
  1235. return prop_id;
  1236. rc = read_property_id(bcdev, pst, prop_id);
  1237. if (rc < 0)
  1238. return rc;
  1239. switch (prop) {
  1240. case POWER_SUPPLY_PROP_MODEL_NAME:
  1241. pval->strval = pst->model;
  1242. break;
  1243. case POWER_SUPPLY_PROP_CAPACITY:
  1244. pval->intval = DIV_ROUND_CLOSEST(pst->prop[prop_id], 100);
  1245. if (IS_ENABLED(CONFIG_QTI_PMIC_GLINK_CLIENT_DEBUG) &&
  1246. (bcdev->fake_soc >= 0 && bcdev->fake_soc <= 100))
  1247. pval->intval = bcdev->fake_soc;
  1248. break;
  1249. case POWER_SUPPLY_PROP_TEMP:
  1250. pval->intval = DIV_ROUND_CLOSEST((int)pst->prop[prop_id], 10);
  1251. break;
  1252. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  1253. pval->intval = bcdev->curr_thermal_level;
  1254. break;
  1255. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
  1256. pval->intval = bcdev->num_thermal_levels;
  1257. break;
  1258. default:
  1259. pval->intval = pst->prop[prop_id];
  1260. break;
  1261. }
  1262. return rc;
  1263. }
  1264. static int battery_psy_set_prop(struct power_supply *psy,
  1265. enum power_supply_property prop,
  1266. const union power_supply_propval *pval)
  1267. {
  1268. struct battery_chg_dev *bcdev = power_supply_get_drvdata(psy);
  1269. switch (prop) {
  1270. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  1271. return battery_psy_set_charge_current(bcdev, pval->intval);
  1272. case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
  1273. return battery_psy_set_charge_start_threshold(bcdev,
  1274. pval->intval);
  1275. case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
  1276. return battery_psy_set_charge_end_threshold(bcdev,
  1277. pval->intval);
  1278. default:
  1279. return -EINVAL;
  1280. }
  1281. return 0;
  1282. }
  1283. static int battery_psy_prop_is_writeable(struct power_supply *psy,
  1284. enum power_supply_property prop)
  1285. {
  1286. switch (prop) {
  1287. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  1288. case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
  1289. case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
  1290. return 1;
  1291. default:
  1292. break;
  1293. }
  1294. return 0;
  1295. }
  1296. static enum power_supply_property battery_props[] = {
  1297. POWER_SUPPLY_PROP_STATUS,
  1298. POWER_SUPPLY_PROP_HEALTH,
  1299. POWER_SUPPLY_PROP_PRESENT,
  1300. POWER_SUPPLY_PROP_CHARGE_TYPE,
  1301. POWER_SUPPLY_PROP_CAPACITY,
  1302. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  1303. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  1304. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  1305. POWER_SUPPLY_PROP_CURRENT_NOW,
  1306. POWER_SUPPLY_PROP_CURRENT_AVG,
  1307. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
  1308. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
  1309. POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD,
  1310. POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
  1311. POWER_SUPPLY_PROP_TEMP,
  1312. POWER_SUPPLY_PROP_TECHNOLOGY,
  1313. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  1314. POWER_SUPPLY_PROP_CYCLE_COUNT,
  1315. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  1316. POWER_SUPPLY_PROP_CHARGE_FULL,
  1317. POWER_SUPPLY_PROP_MODEL_NAME,
  1318. POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
  1319. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  1320. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  1321. POWER_SUPPLY_PROP_POWER_NOW,
  1322. POWER_SUPPLY_PROP_POWER_AVG,
  1323. };
  1324. static const struct power_supply_desc batt_psy_desc = {
  1325. .name = "battery",
  1326. .type = POWER_SUPPLY_TYPE_BATTERY,
  1327. .properties = battery_props,
  1328. .num_properties = ARRAY_SIZE(battery_props),
  1329. .get_property = battery_psy_get_prop,
  1330. .set_property = battery_psy_set_prop,
  1331. .property_is_writeable = battery_psy_prop_is_writeable,
  1332. };
  1333. static int battery_chg_init_psy(struct battery_chg_dev *bcdev)
  1334. {
  1335. struct power_supply_config psy_cfg = {};
  1336. int rc;
  1337. psy_cfg.drv_data = bcdev;
  1338. psy_cfg.of_node = bcdev->dev->of_node;
  1339. bcdev->psy_list[PSY_TYPE_USB].psy =
  1340. devm_power_supply_register(bcdev->dev, &usb_psy_desc[USB_1_PORT_ID], &psy_cfg);
  1341. if (IS_ERR(bcdev->psy_list[PSY_TYPE_USB].psy)) {
  1342. rc = PTR_ERR(bcdev->psy_list[PSY_TYPE_USB].psy);
  1343. bcdev->psy_list[PSY_TYPE_USB].psy = NULL;
  1344. pr_err("Failed to register USB power supply, rc=%d\n", rc);
  1345. return rc;
  1346. }
  1347. if (bcdev->has_usb_2) {
  1348. bcdev->psy_list[PSY_TYPE_USB_2].psy =
  1349. devm_power_supply_register(bcdev->dev,
  1350. &usb_psy_desc[USB_2_PORT_ID], &psy_cfg);
  1351. if (IS_ERR(bcdev->psy_list[PSY_TYPE_USB_2].psy)) {
  1352. rc = PTR_ERR(bcdev->psy_list[PSY_TYPE_USB_2].psy);
  1353. bcdev->psy_list[PSY_TYPE_USB_2].psy = NULL;
  1354. pr_err("Failed to register USB_2 power supply, rc=%d\n", rc);
  1355. return rc;
  1356. }
  1357. }
  1358. bcdev->psy_list[PSY_TYPE_WLS].psy =
  1359. devm_power_supply_register(bcdev->dev, &wls_psy_desc, &psy_cfg);
  1360. if (IS_ERR(bcdev->psy_list[PSY_TYPE_WLS].psy)) {
  1361. rc = PTR_ERR(bcdev->psy_list[PSY_TYPE_WLS].psy);
  1362. bcdev->psy_list[PSY_TYPE_WLS].psy = NULL;
  1363. pr_err("Failed to register wireless power supply, rc=%d\n", rc);
  1364. return rc;
  1365. }
  1366. bcdev->psy_list[PSY_TYPE_BATTERY].psy =
  1367. devm_power_supply_register(bcdev->dev, &batt_psy_desc,
  1368. &psy_cfg);
  1369. if (IS_ERR(bcdev->psy_list[PSY_TYPE_BATTERY].psy)) {
  1370. rc = PTR_ERR(bcdev->psy_list[PSY_TYPE_BATTERY].psy);
  1371. bcdev->psy_list[PSY_TYPE_BATTERY].psy = NULL;
  1372. pr_err("Failed to register battery power supply, rc=%d\n", rc);
  1373. return rc;
  1374. }
  1375. return 0;
  1376. }
  1377. static void battery_chg_subsys_up_work(struct work_struct *work)
  1378. {
  1379. struct battery_chg_dev *bcdev = container_of(work,
  1380. struct battery_chg_dev, subsys_up_work);
  1381. struct psy_state *pst;
  1382. int rc;
  1383. battery_chg_notify_enable(bcdev);
  1384. /*
  1385. * Give some time after enabling notification so that USB adapter type
  1386. * information can be obtained properly which is essential for setting
  1387. * USB ICL.
  1388. */
  1389. msleep(200);
  1390. if (bcdev->last_fcc_ua) {
  1391. rc = __battery_psy_set_charge_current(bcdev,
  1392. bcdev->last_fcc_ua);
  1393. if (rc < 0)
  1394. pr_err("Failed to set FCC (%u uA), rc=%d\n",
  1395. bcdev->last_fcc_ua, rc);
  1396. }
  1397. if (bcdev->usb_icl_ua[USB_1_PORT_ID]) {
  1398. pst = &bcdev->psy_list[PSY_TYPE_USB];
  1399. rc = usb_psy_set_icl(bcdev, pst, USB_INPUT_CURR_LIMIT,
  1400. bcdev->usb_icl_ua[USB_1_PORT_ID]);
  1401. if (rc < 0)
  1402. pr_err("Failed to set ICL(%u uA), rc=%d\n",
  1403. bcdev->usb_icl_ua[USB_1_PORT_ID], rc);
  1404. }
  1405. if (bcdev->has_usb_2 && bcdev->usb_icl_ua[USB_2_PORT_ID]) {
  1406. pst = &bcdev->psy_list[PSY_TYPE_USB_2];
  1407. rc = usb_psy_set_icl(bcdev, pst, USB_INPUT_CURR_LIMIT,
  1408. bcdev->usb_icl_ua[USB_2_PORT_ID]);
  1409. if (rc < 0)
  1410. pr_err("Failed to set USB-2 ICL(%u uA), rc=%d\n",
  1411. bcdev->usb_icl_ua[USB_2_PORT_ID], rc);
  1412. }
  1413. }
  1414. static int wireless_fw_send_firmware(struct battery_chg_dev *bcdev,
  1415. const struct firmware *fw)
  1416. {
  1417. struct wireless_fw_push_buf_req msg = {};
  1418. const u8 *ptr;
  1419. u32 i, num_chunks, partial_chunk_size;
  1420. int rc;
  1421. num_chunks = fw->size / WLS_FW_BUF_SIZE;
  1422. partial_chunk_size = fw->size % WLS_FW_BUF_SIZE;
  1423. if (!num_chunks)
  1424. return -EINVAL;
  1425. pr_debug("Updating FW...\n");
  1426. ptr = fw->data;
  1427. msg.hdr.owner = MSG_OWNER_BC;
  1428. msg.hdr.type = MSG_TYPE_REQ_RESP;
  1429. msg.hdr.opcode = BC_WLS_FW_PUSH_BUF_REQ;
  1430. for (i = 0; i < num_chunks; i++, ptr += WLS_FW_BUF_SIZE) {
  1431. msg.fw_chunk_id = i + 1;
  1432. memcpy(msg.buf, ptr, WLS_FW_BUF_SIZE);
  1433. pr_debug("sending FW chunk %u\n", i + 1);
  1434. rc = battery_chg_fw_write(bcdev, &msg, sizeof(msg));
  1435. if (rc < 0)
  1436. return rc;
  1437. }
  1438. if (partial_chunk_size) {
  1439. msg.fw_chunk_id = i + 1;
  1440. memset(msg.buf, 0, WLS_FW_BUF_SIZE);
  1441. memcpy(msg.buf, ptr, partial_chunk_size);
  1442. pr_debug("sending partial FW chunk %u\n", i + 1);
  1443. rc = battery_chg_fw_write(bcdev, &msg, sizeof(msg));
  1444. if (rc < 0)
  1445. return rc;
  1446. }
  1447. return 0;
  1448. }
  1449. static int wireless_fw_check_for_update(struct battery_chg_dev *bcdev,
  1450. u32 version, size_t size)
  1451. {
  1452. struct wireless_fw_check_req req_msg = {};
  1453. bcdev->wls_fw_update_reqd = false;
  1454. req_msg.hdr.owner = MSG_OWNER_BC;
  1455. req_msg.hdr.type = MSG_TYPE_REQ_RESP;
  1456. req_msg.hdr.opcode = BC_WLS_FW_CHECK_UPDATE;
  1457. req_msg.fw_version = version;
  1458. req_msg.fw_size = size;
  1459. req_msg.fw_crc = bcdev->wls_fw_crc;
  1460. return battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
  1461. }
  1462. #define IDT9415_FW_MAJOR_VER_OFFSET 0x84
  1463. #define IDT9415_FW_MINOR_VER_OFFSET 0x86
  1464. #define IDT_FW_MAJOR_VER_OFFSET 0x94
  1465. #define IDT_FW_MINOR_VER_OFFSET 0x96
  1466. static int wireless_fw_update(struct battery_chg_dev *bcdev, bool force)
  1467. {
  1468. const struct firmware *fw;
  1469. struct psy_state *pst;
  1470. u32 version;
  1471. u16 maj_ver, min_ver;
  1472. int rc;
  1473. if (!bcdev->wls_fw_name) {
  1474. pr_err("wireless FW name is not specified\n");
  1475. return -EINVAL;
  1476. }
  1477. pm_stay_awake(bcdev->dev);
  1478. /*
  1479. * Check for USB presence. If nothing is connected, check whether
  1480. * battery SOC is at least 50% before allowing FW update.
  1481. */
  1482. pst = &bcdev->psy_list[PSY_TYPE_USB];
  1483. rc = read_property_id(bcdev, pst, USB_ONLINE);
  1484. if (rc < 0)
  1485. goto out;
  1486. if (bcdev->has_usb_2 && !pst->prop[USB_ONLINE]) {
  1487. pst = &bcdev->psy_list[PSY_TYPE_USB_2];
  1488. rc = read_property_id(bcdev, pst, USB_ONLINE);
  1489. if (rc < 0)
  1490. goto out;
  1491. }
  1492. if (!pst->prop[USB_ONLINE]) {
  1493. pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  1494. rc = read_property_id(bcdev, pst, BATT_CAPACITY);
  1495. if (rc < 0)
  1496. goto out;
  1497. if ((pst->prop[BATT_CAPACITY] / 100) < 50) {
  1498. pr_err("Battery SOC should be at least 50%% or connect charger\n");
  1499. rc = -EINVAL;
  1500. goto out;
  1501. }
  1502. }
  1503. rc = firmware_request_nowarn(&fw, bcdev->wls_fw_name, bcdev->dev);
  1504. if (rc) {
  1505. pr_err("Couldn't get firmware rc=%d\n", rc);
  1506. goto out;
  1507. }
  1508. if (!fw || !fw->data || !fw->size) {
  1509. pr_err("Invalid firmware\n");
  1510. rc = -EINVAL;
  1511. goto release_fw;
  1512. }
  1513. if (fw->size < SZ_16K) {
  1514. pr_err("Invalid firmware size %zu\n", fw->size);
  1515. rc = -EINVAL;
  1516. goto release_fw;
  1517. }
  1518. if (strstr(bcdev->wls_fw_name, "9412")) {
  1519. maj_ver = le16_to_cpu(*(__le16 *)(fw->data + IDT_FW_MAJOR_VER_OFFSET));
  1520. min_ver = le16_to_cpu(*(__le16 *)(fw->data + IDT_FW_MINOR_VER_OFFSET));
  1521. } else {
  1522. maj_ver = le16_to_cpu(*(__le16 *)(fw->data + IDT9415_FW_MAJOR_VER_OFFSET));
  1523. min_ver = le16_to_cpu(*(__le16 *)(fw->data + IDT9415_FW_MINOR_VER_OFFSET));
  1524. }
  1525. version = maj_ver << 16 | min_ver;
  1526. if (force)
  1527. version = UINT_MAX;
  1528. pr_debug("FW size: %zu version: %#x\n", fw->size, version);
  1529. rc = wireless_fw_check_for_update(bcdev, version, fw->size);
  1530. if (rc < 0) {
  1531. pr_err("Wireless FW update not needed, rc=%d\n", rc);
  1532. goto release_fw;
  1533. }
  1534. if (!bcdev->wls_fw_update_reqd) {
  1535. pr_warn("Wireless FW update not required\n");
  1536. goto release_fw;
  1537. }
  1538. /* Wait for IDT to be setup by charger firmware */
  1539. msleep(WLS_FW_PREPARE_TIME_MS);
  1540. reinit_completion(&bcdev->fw_update_ack);
  1541. rc = wireless_fw_send_firmware(bcdev, fw);
  1542. if (rc < 0) {
  1543. pr_err("Failed to send FW chunk, rc=%d\n", rc);
  1544. goto release_fw;
  1545. }
  1546. pr_debug("Waiting for fw_update_ack\n");
  1547. rc = wait_for_completion_timeout(&bcdev->fw_update_ack,
  1548. msecs_to_jiffies(bcdev->wls_fw_update_time_ms));
  1549. if (!rc) {
  1550. pr_err("Error, timed out updating firmware\n");
  1551. rc = -ETIMEDOUT;
  1552. goto release_fw;
  1553. } else {
  1554. pr_debug("Waited for %d ms\n",
  1555. bcdev->wls_fw_update_time_ms - jiffies_to_msecs(rc));
  1556. rc = 0;
  1557. }
  1558. pr_info("Wireless FW update done\n");
  1559. release_fw:
  1560. bcdev->wls_fw_crc = 0;
  1561. release_firmware(fw);
  1562. out:
  1563. pm_relax(bcdev->dev);
  1564. return rc;
  1565. }
  1566. static ssize_t qti_charger_ro_show(struct class *c,
  1567. struct class_attribute *attr, char *buf,
  1568. int psy_type, int prop_id)
  1569. {
  1570. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1571. battery_class);
  1572. struct psy_state *pst = &bcdev->psy_list[psy_type];
  1573. int rc;
  1574. rc = read_property_id(bcdev, pst, prop_id);
  1575. if (rc < 0)
  1576. return rc;
  1577. return scnprintf(buf, PAGE_SIZE, "%d\n", (int)pst->prop[prop_id]);
  1578. }
  1579. #define QTI_CHARGER_RO_SHOW(name, psy_type, prop_id) \
  1580. static ssize_t name##_show(struct class *c, \
  1581. struct class_attribute *attr, \
  1582. char *buf) \
  1583. { \
  1584. return qti_charger_ro_show(c, attr, buf, psy_type, prop_id); \
  1585. } \
  1586. static CLASS_ATTR_RO(name) \
  1587. #define QTI_CHARGER_RW_SHOW(name, psy_type, prop_id) \
  1588. static ssize_t name##_show(struct class *c, \
  1589. struct class_attribute *attr, \
  1590. char *buf) \
  1591. { \
  1592. return qti_charger_ro_show(c, attr, buf, psy_type, prop_id); \
  1593. } \
  1594. static CLASS_ATTR_RW(name) \
  1595. #define QTI_CHARGER_RW_PROP_SHOW(name, field) \
  1596. static ssize_t name##_show(struct class *c, \
  1597. struct class_attribute *attr, \
  1598. char *buf) \
  1599. { \
  1600. struct battery_chg_dev *bcdev = container_of(c, \
  1601. struct battery_chg_dev, battery_class); \
  1602. return scnprintf(buf, PAGE_SIZE, "%d\n", bcdev->field); \
  1603. } \
  1604. static CLASS_ATTR_RW(name) \
  1605. #define QTI_CHARGER_TYPE_RO_SHOW(name, psy, psy_type, prop_id) \
  1606. static ssize_t name##_show(struct class *c, \
  1607. struct class_attribute *attr, \
  1608. char *buf) \
  1609. { \
  1610. struct battery_chg_dev *bcdev = container_of(c, \
  1611. struct battery_chg_dev, battery_class); \
  1612. struct psy_state *pst = &bcdev->psy_list[psy_type]; \
  1613. int rc; \
  1614. \
  1615. rc = read_property_id(bcdev, pst, prop_id); \
  1616. if (rc < 0) \
  1617. return rc; \
  1618. \
  1619. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  1620. get_##psy##_type_name(pst->prop[prop_id])); \
  1621. } \
  1622. static CLASS_ATTR_RO(name) \
  1623. static ssize_t wireless_fw_update_time_ms_store(struct class *c,
  1624. struct class_attribute *attr,
  1625. const char *buf, size_t count)
  1626. {
  1627. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1628. battery_class);
  1629. if (kstrtou32(buf, 0, &bcdev->wls_fw_update_time_ms))
  1630. return -EINVAL;
  1631. return count;
  1632. }
  1633. QTI_CHARGER_RW_PROP_SHOW(wireless_fw_update_time_ms, wls_fw_update_time_ms);
  1634. static ssize_t wireless_fw_crc_store(struct class *c,
  1635. struct class_attribute *attr,
  1636. const char *buf, size_t count)
  1637. {
  1638. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1639. battery_class);
  1640. u16 val;
  1641. if (kstrtou16(buf, 0, &val) || !val)
  1642. return -EINVAL;
  1643. bcdev->wls_fw_crc = val;
  1644. return count;
  1645. }
  1646. static CLASS_ATTR_WO(wireless_fw_crc);
  1647. static ssize_t wireless_fw_version_show(struct class *c,
  1648. struct class_attribute *attr,
  1649. char *buf)
  1650. {
  1651. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1652. battery_class);
  1653. struct wireless_fw_get_version_req req_msg = {};
  1654. int rc;
  1655. req_msg.hdr.owner = MSG_OWNER_BC;
  1656. req_msg.hdr.type = MSG_TYPE_REQ_RESP;
  1657. req_msg.hdr.opcode = BC_WLS_FW_GET_VERSION;
  1658. rc = battery_chg_write(bcdev, &req_msg, sizeof(req_msg));
  1659. if (rc < 0) {
  1660. pr_err("Failed to get FW version rc=%d\n", rc);
  1661. return rc;
  1662. }
  1663. return scnprintf(buf, PAGE_SIZE, "%#x\n", bcdev->wls_fw_version);
  1664. }
  1665. static CLASS_ATTR_RO(wireless_fw_version);
  1666. static ssize_t wireless_fw_force_update_store(struct class *c,
  1667. struct class_attribute *attr,
  1668. const char *buf, size_t count)
  1669. {
  1670. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1671. battery_class);
  1672. bool val;
  1673. int rc;
  1674. if (kstrtobool(buf, &val) || !val)
  1675. return -EINVAL;
  1676. rc = wireless_fw_update(bcdev, true);
  1677. if (rc < 0)
  1678. return rc;
  1679. return count;
  1680. }
  1681. static CLASS_ATTR_WO(wireless_fw_force_update);
  1682. static ssize_t wireless_fw_update_store(struct class *c,
  1683. struct class_attribute *attr,
  1684. const char *buf, size_t count)
  1685. {
  1686. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1687. battery_class);
  1688. bool val;
  1689. int rc;
  1690. if (kstrtobool(buf, &val) || !val)
  1691. return -EINVAL;
  1692. rc = wireless_fw_update(bcdev, false);
  1693. if (rc < 0)
  1694. return rc;
  1695. return count;
  1696. }
  1697. static CLASS_ATTR_WO(wireless_fw_update);
  1698. QTI_CHARGER_TYPE_RO_SHOW(wireless_type, wls, PSY_TYPE_WLS, WLS_ADAP_TYPE);
  1699. static ssize_t charge_control_en_store(struct class *c,
  1700. struct class_attribute *attr,
  1701. const char *buf, size_t count)
  1702. {
  1703. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1704. battery_class);
  1705. int rc;
  1706. bool val;
  1707. if (kstrtobool(buf, &val))
  1708. return -EINVAL;
  1709. if (val == bcdev->chg_ctrl_en)
  1710. return count;
  1711. rc = write_property_id(bcdev, &bcdev->psy_list[PSY_TYPE_BATTERY],
  1712. BATT_CHG_CTRL_EN, val);
  1713. if (rc < 0) {
  1714. pr_err("Failed to set charge_control_en, rc=%d\n", rc);
  1715. return rc;
  1716. }
  1717. bcdev->chg_ctrl_en = val;
  1718. return count;
  1719. }
  1720. static ssize_t charge_control_en_show(struct class *c,
  1721. struct class_attribute *attr, char *buf)
  1722. {
  1723. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1724. battery_class);
  1725. int rc;
  1726. rc = get_charge_control_en(bcdev);
  1727. if (rc < 0)
  1728. return rc;
  1729. return scnprintf(buf, PAGE_SIZE, "%d\n", bcdev->chg_ctrl_en);
  1730. }
  1731. static CLASS_ATTR_RW(charge_control_en);
  1732. QTI_CHARGER_RO_SHOW(usb_typec_compliant, PSY_TYPE_USB, USB_TYPEC_COMPLIANT);
  1733. QTI_CHARGER_TYPE_RO_SHOW(usb_real_type, usb, PSY_TYPE_USB, USB_REAL_TYPE);
  1734. QTI_CHARGER_RO_SHOW(usb_2_typec_compliant, PSY_TYPE_USB_2, USB_TYPEC_COMPLIANT);
  1735. QTI_CHARGER_TYPE_RO_SHOW(usb_2_real_type, usb, PSY_TYPE_USB_2, USB_REAL_TYPE);
  1736. static ssize_t restrict_cur_store(struct class *c, struct class_attribute *attr,
  1737. const char *buf, size_t count)
  1738. {
  1739. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1740. battery_class);
  1741. int rc;
  1742. u32 fcc_ua, prev_fcc_ua;
  1743. if (kstrtou32(buf, 0, &fcc_ua) || fcc_ua > bcdev->thermal_fcc_ua)
  1744. return -EINVAL;
  1745. prev_fcc_ua = bcdev->restrict_fcc_ua;
  1746. bcdev->restrict_fcc_ua = fcc_ua;
  1747. if (bcdev->restrict_chg_en) {
  1748. rc = __battery_psy_set_charge_current(bcdev, fcc_ua);
  1749. if (rc < 0) {
  1750. bcdev->restrict_fcc_ua = prev_fcc_ua;
  1751. return rc;
  1752. }
  1753. }
  1754. return count;
  1755. }
  1756. QTI_CHARGER_RW_PROP_SHOW(restrict_cur, restrict_fcc_ua);
  1757. static ssize_t restrict_chg_store(struct class *c, struct class_attribute *attr,
  1758. const char *buf, size_t count)
  1759. {
  1760. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1761. battery_class);
  1762. int rc;
  1763. bool val;
  1764. if (kstrtobool(buf, &val))
  1765. return -EINVAL;
  1766. bcdev->restrict_chg_en = val;
  1767. rc = __battery_psy_set_charge_current(bcdev, bcdev->restrict_chg_en ?
  1768. bcdev->restrict_fcc_ua : bcdev->thermal_fcc_ua);
  1769. if (rc < 0)
  1770. return rc;
  1771. return count;
  1772. }
  1773. QTI_CHARGER_RW_PROP_SHOW(restrict_chg, restrict_chg_en);
  1774. static ssize_t fake_soc_store(struct class *c, struct class_attribute *attr,
  1775. const char *buf, size_t count)
  1776. {
  1777. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1778. battery_class);
  1779. struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  1780. int val;
  1781. if (kstrtoint(buf, 0, &val))
  1782. return -EINVAL;
  1783. bcdev->fake_soc = val;
  1784. pr_debug("Set fake soc to %d\n", val);
  1785. if (IS_ENABLED(CONFIG_QTI_PMIC_GLINK_CLIENT_DEBUG) && pst->psy)
  1786. power_supply_changed(pst->psy);
  1787. return count;
  1788. }
  1789. QTI_CHARGER_RW_PROP_SHOW(fake_soc, fake_soc);
  1790. static ssize_t wireless_boost_en_store(struct class *c,
  1791. struct class_attribute *attr,
  1792. const char *buf, size_t count)
  1793. {
  1794. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1795. battery_class);
  1796. int rc;
  1797. bool val;
  1798. if (kstrtobool(buf, &val))
  1799. return -EINVAL;
  1800. rc = write_property_id(bcdev, &bcdev->psy_list[PSY_TYPE_WLS],
  1801. WLS_BOOST_EN, val);
  1802. if (rc < 0)
  1803. return rc;
  1804. return count;
  1805. }
  1806. QTI_CHARGER_RW_SHOW(wireless_boost_en, PSY_TYPE_WLS, WLS_BOOST_EN);
  1807. static ssize_t _moisture_detection_en_store(struct class *c,
  1808. struct class_attribute *attr, enum psy_type type,
  1809. const char *buf, size_t count)
  1810. {
  1811. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1812. battery_class);
  1813. int rc;
  1814. bool val;
  1815. if (kstrtobool(buf, &val))
  1816. return -EINVAL;
  1817. rc = write_property_id(bcdev, &bcdev->psy_list[type],
  1818. USB_MOISTURE_DET_EN, val);
  1819. if (rc < 0)
  1820. return rc;
  1821. return count;
  1822. }
  1823. static ssize_t moisture_detection_en_store(struct class *c,
  1824. struct class_attribute *attr,
  1825. const char *buf, size_t count)
  1826. {
  1827. return _moisture_detection_en_store(c, attr, PSY_TYPE_USB, buf, count);
  1828. }
  1829. QTI_CHARGER_RW_SHOW(moisture_detection_en, PSY_TYPE_USB, USB_MOISTURE_DET_EN);
  1830. static ssize_t moisture_detection_usb_2_en_store(struct class *c,
  1831. struct class_attribute *attr,
  1832. const char *buf, size_t count)
  1833. {
  1834. return _moisture_detection_en_store(c, attr, PSY_TYPE_USB_2, buf, count);
  1835. }
  1836. QTI_CHARGER_RW_SHOW(moisture_detection_usb_2_en, PSY_TYPE_USB_2, USB_MOISTURE_DET_EN);
  1837. QTI_CHARGER_RO_SHOW(moisture_detection_status, PSY_TYPE_USB, USB_MOISTURE_DET_STS);
  1838. QTI_CHARGER_RO_SHOW(moisture_detection_usb_2_status, PSY_TYPE_USB_2, USB_MOISTURE_DET_STS);
  1839. QTI_CHARGER_RO_SHOW(resistance, PSY_TYPE_BATTERY, BATT_RESISTANCE);
  1840. QTI_CHARGER_RO_SHOW(soh, PSY_TYPE_BATTERY, BATT_SOH);
  1841. static ssize_t ship_mode_en_store(struct class *c, struct class_attribute *attr,
  1842. const char *buf, size_t count)
  1843. {
  1844. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1845. battery_class);
  1846. if (kstrtobool(buf, &bcdev->ship_mode_en))
  1847. return -EINVAL;
  1848. return count;
  1849. }
  1850. static ssize_t ship_mode_en_show(struct class *c, struct class_attribute *attr,
  1851. char *buf)
  1852. {
  1853. struct battery_chg_dev *bcdev = container_of(c, struct battery_chg_dev,
  1854. battery_class);
  1855. return scnprintf(buf, PAGE_SIZE, "%d\n", bcdev->ship_mode_en);
  1856. }
  1857. static CLASS_ATTR_RW(ship_mode_en);
  1858. static struct attribute *battery_class_attrs[] = {
  1859. &class_attr_soh.attr,
  1860. &class_attr_resistance.attr,
  1861. &class_attr_moisture_detection_status.attr,
  1862. &class_attr_moisture_detection_en.attr,
  1863. &class_attr_wireless_boost_en.attr,
  1864. &class_attr_fake_soc.attr,
  1865. &class_attr_wireless_fw_update.attr,
  1866. &class_attr_wireless_fw_force_update.attr,
  1867. &class_attr_wireless_fw_version.attr,
  1868. &class_attr_wireless_fw_crc.attr,
  1869. &class_attr_wireless_fw_update_time_ms.attr,
  1870. &class_attr_wireless_type.attr,
  1871. &class_attr_ship_mode_en.attr,
  1872. &class_attr_restrict_chg.attr,
  1873. &class_attr_restrict_cur.attr,
  1874. &class_attr_usb_real_type.attr,
  1875. &class_attr_usb_typec_compliant.attr,
  1876. &class_attr_charge_control_en.attr,
  1877. NULL,
  1878. };
  1879. ATTRIBUTE_GROUPS(battery_class);
  1880. static struct attribute *battery_class_usb_2_attrs[] = {
  1881. &class_attr_soh.attr,
  1882. &class_attr_resistance.attr,
  1883. &class_attr_moisture_detection_status.attr,
  1884. &class_attr_moisture_detection_usb_2_status.attr,
  1885. &class_attr_moisture_detection_en.attr,
  1886. &class_attr_moisture_detection_usb_2_en.attr,
  1887. &class_attr_wireless_boost_en.attr,
  1888. &class_attr_fake_soc.attr,
  1889. &class_attr_wireless_fw_update.attr,
  1890. &class_attr_wireless_fw_force_update.attr,
  1891. &class_attr_wireless_fw_version.attr,
  1892. &class_attr_wireless_fw_crc.attr,
  1893. &class_attr_wireless_fw_update_time_ms.attr,
  1894. &class_attr_wireless_type.attr,
  1895. &class_attr_ship_mode_en.attr,
  1896. &class_attr_restrict_chg.attr,
  1897. &class_attr_restrict_cur.attr,
  1898. &class_attr_usb_real_type.attr,
  1899. &class_attr_usb_2_real_type.attr,
  1900. &class_attr_usb_typec_compliant.attr,
  1901. &class_attr_usb_2_typec_compliant.attr,
  1902. &class_attr_charge_control_en.attr,
  1903. NULL,
  1904. };
  1905. ATTRIBUTE_GROUPS(battery_class_usb_2);
  1906. #ifdef CONFIG_DEBUG_FS
  1907. static void battery_chg_add_debugfs(struct battery_chg_dev *bcdev)
  1908. {
  1909. int rc;
  1910. struct dentry *dir;
  1911. dir = debugfs_create_dir("battery_charger", NULL);
  1912. if (IS_ERR(dir)) {
  1913. rc = PTR_ERR(dir);
  1914. pr_err("Failed to create charger debugfs directory, rc=%d\n",
  1915. rc);
  1916. return;
  1917. }
  1918. bcdev->debugfs_dir = dir;
  1919. debugfs_create_bool("block_tx", 0600, dir, &bcdev->block_tx);
  1920. }
  1921. #else
  1922. static void battery_chg_add_debugfs(struct battery_chg_dev *bcdev) { }
  1923. #endif
  1924. static int battery_chg_parse_dt(struct battery_chg_dev *bcdev)
  1925. {
  1926. struct device_node *node = bcdev->dev->of_node;
  1927. struct psy_state *pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  1928. int i, rc, len;
  1929. u32 prev, val;
  1930. of_property_read_string(node, "qcom,wireless-fw-name",
  1931. &bcdev->wls_fw_name);
  1932. of_property_read_u32(node, "qcom,shutdown-voltage",
  1933. &bcdev->shutdown_volt_mv);
  1934. rc = read_property_id(bcdev, pst, BATT_CHG_CTRL_LIM_MAX);
  1935. if (rc < 0) {
  1936. pr_err("Failed to read prop BATT_CHG_CTRL_LIM_MAX, rc=%d\n",
  1937. rc);
  1938. return rc;
  1939. }
  1940. rc = of_property_count_elems_of_size(node, "qcom,thermal-mitigation",
  1941. sizeof(u32));
  1942. if (rc <= 0) {
  1943. rc = of_property_read_u32(node, "qcom,thermal-mitigation-step",
  1944. &val);
  1945. if (rc < 0)
  1946. return 0;
  1947. if (val < 500000 || val >= pst->prop[BATT_CHG_CTRL_LIM_MAX]) {
  1948. pr_err("thermal_fcc_step %d is invalid\n", val);
  1949. return -EINVAL;
  1950. }
  1951. bcdev->thermal_fcc_step = val;
  1952. len = pst->prop[BATT_CHG_CTRL_LIM_MAX] / bcdev->thermal_fcc_step;
  1953. /*
  1954. * FCC values must be above 500mA.
  1955. * Since len is truncated when calculated, check and adjust len so
  1956. * that the above requirement is met.
  1957. */
  1958. if (pst->prop[BATT_CHG_CTRL_LIM_MAX] - (bcdev->thermal_fcc_step * len) < 500000)
  1959. len = len - 1;
  1960. } else {
  1961. bcdev->thermal_fcc_step = 0;
  1962. len = rc;
  1963. prev = pst->prop[BATT_CHG_CTRL_LIM_MAX];
  1964. for (i = 0; i < len; i++) {
  1965. rc = of_property_read_u32_index(node, "qcom,thermal-mitigation",
  1966. i, &val);
  1967. if (rc < 0)
  1968. return rc;
  1969. if (val > prev) {
  1970. pr_err("Thermal levels should be in descending order\n");
  1971. bcdev->num_thermal_levels = -EINVAL;
  1972. return 0;
  1973. }
  1974. prev = val;
  1975. }
  1976. bcdev->thermal_levels = devm_kcalloc(bcdev->dev, len + 1,
  1977. sizeof(*bcdev->thermal_levels),
  1978. GFP_KERNEL);
  1979. if (!bcdev->thermal_levels)
  1980. return -ENOMEM;
  1981. /*
  1982. * Element 0 is for normal charging current. Elements from index 1
  1983. * onwards is for thermal mitigation charging currents.
  1984. */
  1985. bcdev->thermal_levels[0] = pst->prop[BATT_CHG_CTRL_LIM_MAX];
  1986. rc = of_property_read_u32_array(node, "qcom,thermal-mitigation",
  1987. &bcdev->thermal_levels[1], len);
  1988. if (rc < 0) {
  1989. pr_err("Error in reading qcom,thermal-mitigation, rc=%d\n", rc);
  1990. return rc;
  1991. }
  1992. }
  1993. bcdev->num_thermal_levels = len;
  1994. bcdev->thermal_fcc_ua = pst->prop[BATT_CHG_CTRL_LIM_MAX];
  1995. return 0;
  1996. }
  1997. static int battery_chg_ship_mode(struct notifier_block *nb, unsigned long code,
  1998. void *unused)
  1999. {
  2000. struct battery_charger_notify_msg msg_notify = { { 0 } };
  2001. struct battery_charger_ship_mode_req_msg msg = { { 0 } };
  2002. struct battery_chg_dev *bcdev = container_of(nb, struct battery_chg_dev,
  2003. reboot_notifier);
  2004. int rc;
  2005. msg_notify.hdr.owner = MSG_OWNER_BC;
  2006. msg_notify.hdr.type = MSG_TYPE_NOTIFY;
  2007. msg_notify.hdr.opcode = BC_SHUTDOWN_NOTIFY;
  2008. rc = battery_chg_write(bcdev, &msg_notify, sizeof(msg_notify));
  2009. if (rc < 0)
  2010. pr_err("Failed to send shutdown notification rc=%d\n", rc);
  2011. if (!bcdev->ship_mode_en)
  2012. return NOTIFY_DONE;
  2013. msg.hdr.owner = MSG_OWNER_BC;
  2014. msg.hdr.type = MSG_TYPE_REQ_RESP;
  2015. msg.hdr.opcode = BC_SHIP_MODE_REQ_SET;
  2016. msg.ship_mode_type = SHIP_MODE_PMIC;
  2017. if (code == SYS_POWER_OFF) {
  2018. rc = battery_chg_write(bcdev, &msg, sizeof(msg));
  2019. if (rc < 0)
  2020. pr_emerg("Failed to write ship mode: %d\n", rc);
  2021. }
  2022. return NOTIFY_DONE;
  2023. }
  2024. static void panel_event_notifier_callback(enum panel_event_notifier_tag tag,
  2025. struct panel_event_notification *notification, void *data)
  2026. {
  2027. struct battery_chg_dev *bcdev = data;
  2028. if (!notification) {
  2029. pr_debug("Invalid panel notification\n");
  2030. return;
  2031. }
  2032. pr_debug("panel event received, type: %d\n", notification->notif_type);
  2033. switch (notification->notif_type) {
  2034. case DRM_PANEL_EVENT_BLANK:
  2035. battery_chg_notify_disable(bcdev);
  2036. break;
  2037. case DRM_PANEL_EVENT_UNBLANK:
  2038. battery_chg_notify_enable(bcdev);
  2039. break;
  2040. default:
  2041. pr_debug("Ignore panel event: %d\n", notification->notif_type);
  2042. break;
  2043. }
  2044. }
  2045. static int battery_chg_register_panel_notifier(struct battery_chg_dev *bcdev)
  2046. {
  2047. struct device_node *np = bcdev->dev->of_node;
  2048. struct device_node *pnode;
  2049. struct drm_panel *panel, *active_panel = NULL;
  2050. void *cookie = NULL;
  2051. int i, count, rc;
  2052. count = of_count_phandle_with_args(np, "qcom,display-panels", NULL);
  2053. if (count <= 0)
  2054. return 0;
  2055. for (i = 0; i < count; i++) {
  2056. pnode = of_parse_phandle(np, "qcom,display-panels", i);
  2057. if (!pnode)
  2058. return -ENODEV;
  2059. panel = of_drm_find_panel(pnode);
  2060. of_node_put(pnode);
  2061. if (!IS_ERR(panel)) {
  2062. active_panel = panel;
  2063. break;
  2064. }
  2065. }
  2066. if (!active_panel) {
  2067. rc = PTR_ERR(panel);
  2068. if (rc != -EPROBE_DEFER)
  2069. dev_err(bcdev->dev, "Failed to find active panel, rc=%d\n", rc);
  2070. return rc;
  2071. }
  2072. cookie = panel_event_notifier_register(
  2073. PANEL_EVENT_NOTIFICATION_PRIMARY,
  2074. PANEL_EVENT_NOTIFIER_CLIENT_BATTERY_CHARGER,
  2075. active_panel,
  2076. panel_event_notifier_callback,
  2077. (void *)bcdev);
  2078. if (IS_ERR(cookie)) {
  2079. rc = PTR_ERR(cookie);
  2080. dev_err(bcdev->dev, "Failed to register panel event notifier, rc=%d\n", rc);
  2081. return rc;
  2082. }
  2083. pr_debug("register panel notifier successful\n");
  2084. bcdev->notifier_cookie = cookie;
  2085. return 0;
  2086. }
  2087. static int
  2088. battery_chg_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
  2089. unsigned long *state)
  2090. {
  2091. struct battery_chg_dev *bcdev = tcd->devdata;
  2092. *state = bcdev->num_thermal_levels;
  2093. return 0;
  2094. }
  2095. static int
  2096. battery_chg_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  2097. unsigned long *state)
  2098. {
  2099. struct battery_chg_dev *bcdev = tcd->devdata;
  2100. *state = bcdev->curr_thermal_level;
  2101. return 0;
  2102. }
  2103. static int
  2104. battery_chg_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
  2105. unsigned long state)
  2106. {
  2107. struct battery_chg_dev *bcdev = tcd->devdata;
  2108. return battery_psy_set_charge_current(bcdev, (int)state);
  2109. }
  2110. static const struct thermal_cooling_device_ops battery_tcd_ops = {
  2111. .get_max_state = battery_chg_get_max_charge_cntl_limit,
  2112. .get_cur_state = battery_chg_get_cur_charge_cntl_limit,
  2113. .set_cur_state = battery_chg_set_cur_charge_cntl_limit,
  2114. };
  2115. static int battery_chg_probe(struct platform_device *pdev)
  2116. {
  2117. struct battery_chg_dev *bcdev;
  2118. struct device *dev = &pdev->dev;
  2119. struct pmic_glink_client_data client_data = { };
  2120. struct thermal_cooling_device *tcd;
  2121. struct psy_state *pst;
  2122. int rc, i;
  2123. bcdev = devm_kzalloc(&pdev->dev, sizeof(*bcdev), GFP_KERNEL);
  2124. if (!bcdev)
  2125. return -ENOMEM;
  2126. bcdev->psy_list[PSY_TYPE_BATTERY].map = battery_prop_map;
  2127. bcdev->psy_list[PSY_TYPE_BATTERY].prop_count = BATT_PROP_MAX;
  2128. bcdev->psy_list[PSY_TYPE_BATTERY].opcode_get = BC_BATTERY_STATUS_GET;
  2129. bcdev->psy_list[PSY_TYPE_BATTERY].opcode_set = BC_BATTERY_STATUS_SET;
  2130. bcdev->psy_list[PSY_TYPE_USB].map = usb_prop_map;
  2131. bcdev->psy_list[PSY_TYPE_USB].prop_count = USB_PROP_MAX;
  2132. bcdev->psy_list[PSY_TYPE_USB].opcode_get = BC_USB_STATUS_GET(USB_1_PORT_ID);
  2133. bcdev->psy_list[PSY_TYPE_USB].opcode_set = BC_USB_STATUS_SET(USB_1_PORT_ID);
  2134. bcdev->psy_list[PSY_TYPE_WLS].map = wls_prop_map;
  2135. bcdev->psy_list[PSY_TYPE_WLS].prop_count = WLS_PROP_MAX;
  2136. bcdev->psy_list[PSY_TYPE_WLS].opcode_get = BC_WLS_STATUS_GET;
  2137. bcdev->psy_list[PSY_TYPE_WLS].opcode_set = BC_WLS_STATUS_SET;
  2138. bcdev->usb_active[USB_1_PORT_ID] = true;
  2139. bcdev->has_usb_2 = of_property_read_bool(dev->of_node, "qcom,multiport-usb");
  2140. if (bcdev->has_usb_2) {
  2141. bcdev->psy_list[PSY_TYPE_USB_2].map = usb_prop_map;
  2142. bcdev->psy_list[PSY_TYPE_USB_2].prop_count = USB_PROP_MAX;
  2143. bcdev->psy_list[PSY_TYPE_USB_2].opcode_get = BC_USB_STATUS_GET(USB_2_PORT_ID);
  2144. bcdev->psy_list[PSY_TYPE_USB_2].opcode_set = BC_USB_STATUS_SET(USB_2_PORT_ID);
  2145. bcdev->usb_active[USB_2_PORT_ID] = true;
  2146. }
  2147. for (i = 0; i < PSY_TYPE_MAX; i++) {
  2148. bcdev->psy_list[i].prop =
  2149. devm_kcalloc(&pdev->dev, bcdev->psy_list[i].prop_count,
  2150. sizeof(u32), GFP_KERNEL);
  2151. if (!bcdev->psy_list[i].prop)
  2152. return -ENOMEM;
  2153. }
  2154. bcdev->psy_list[PSY_TYPE_BATTERY].model =
  2155. devm_kzalloc(&pdev->dev, MAX_STR_LEN, GFP_KERNEL);
  2156. if (!bcdev->psy_list[PSY_TYPE_BATTERY].model)
  2157. return -ENOMEM;
  2158. mutex_init(&bcdev->rw_lock);
  2159. init_rwsem(&bcdev->state_sem);
  2160. init_completion(&bcdev->ack);
  2161. init_completion(&bcdev->fw_buf_ack);
  2162. init_completion(&bcdev->fw_update_ack);
  2163. INIT_WORK(&bcdev->subsys_up_work, battery_chg_subsys_up_work);
  2164. INIT_WORK(&bcdev->usb_type_work, battery_chg_update_usb_type_work);
  2165. INIT_WORK(&bcdev->battery_check_work, battery_chg_check_status_work);
  2166. bcdev->dev = dev;
  2167. rc = battery_chg_register_panel_notifier(bcdev);
  2168. if (rc < 0)
  2169. return rc;
  2170. client_data.id = MSG_OWNER_BC;
  2171. client_data.name = "battery_charger";
  2172. client_data.msg_cb = battery_chg_callback;
  2173. client_data.priv = bcdev;
  2174. client_data.state_cb = battery_chg_state_cb;
  2175. bcdev->client = pmic_glink_register_client(dev, &client_data);
  2176. if (IS_ERR(bcdev->client)) {
  2177. rc = PTR_ERR(bcdev->client);
  2178. if (rc != -EPROBE_DEFER)
  2179. dev_err(dev, "Error in registering with pmic_glink %d\n",
  2180. rc);
  2181. goto reg_error;
  2182. }
  2183. down_write(&bcdev->state_sem);
  2184. atomic_set(&bcdev->state, PMIC_GLINK_STATE_UP);
  2185. /*
  2186. * This should be initialized here so that battery_chg_callback
  2187. * can run successfully when battery_chg_parse_dt() starts
  2188. * reading BATT_CHG_CTRL_LIM_MAX parameter and waits for a response.
  2189. */
  2190. bcdev->initialized = true;
  2191. up_write(&bcdev->state_sem);
  2192. bcdev->reboot_notifier.notifier_call = battery_chg_ship_mode;
  2193. bcdev->reboot_notifier.priority = 255;
  2194. register_reboot_notifier(&bcdev->reboot_notifier);
  2195. rc = battery_chg_parse_dt(bcdev);
  2196. if (rc < 0) {
  2197. dev_err(dev, "Failed to parse dt rc=%d\n", rc);
  2198. goto error;
  2199. }
  2200. bcdev->restrict_fcc_ua = DEFAULT_RESTRICT_FCC_UA;
  2201. platform_set_drvdata(pdev, bcdev);
  2202. bcdev->fake_soc = -EINVAL;
  2203. rc = battery_chg_init_psy(bcdev);
  2204. if (rc < 0)
  2205. goto error;
  2206. bcdev->battery_class.name = "qcom-battery";
  2207. if (bcdev->has_usb_2)
  2208. bcdev->battery_class.class_groups = battery_class_usb_2_groups;
  2209. else
  2210. bcdev->battery_class.class_groups = battery_class_groups;
  2211. rc = class_register(&bcdev->battery_class);
  2212. if (rc < 0) {
  2213. dev_err(dev, "Failed to create battery_class rc=%d\n", rc);
  2214. goto error;
  2215. }
  2216. pst = &bcdev->psy_list[PSY_TYPE_BATTERY];
  2217. tcd = devm_thermal_of_cooling_device_register(dev, dev->of_node,
  2218. (char *)pst->psy->desc->name, bcdev, &battery_tcd_ops);
  2219. if (IS_ERR_OR_NULL(tcd)) {
  2220. rc = PTR_ERR_OR_ZERO(tcd);
  2221. dev_err(dev, "Failed to register thermal cooling device rc=%d\n",
  2222. rc);
  2223. class_unregister(&bcdev->battery_class);
  2224. goto error;
  2225. }
  2226. bcdev->wls_fw_update_time_ms = WLS_FW_UPDATE_TIME_MS;
  2227. battery_chg_add_debugfs(bcdev);
  2228. bcdev->notify_en = false;
  2229. battery_chg_notify_enable(bcdev);
  2230. device_init_wakeup(bcdev->dev, true);
  2231. schedule_work(&bcdev->usb_type_work);
  2232. rc = get_charge_control_en(bcdev);
  2233. if (rc < 0)
  2234. pr_debug("Failed to read charge_control_en, rc = %d\n", rc);
  2235. return 0;
  2236. error:
  2237. down_write(&bcdev->state_sem);
  2238. atomic_set(&bcdev->state, PMIC_GLINK_STATE_DOWN);
  2239. bcdev->initialized = false;
  2240. up_write(&bcdev->state_sem);
  2241. pmic_glink_unregister_client(bcdev->client);
  2242. cancel_work_sync(&bcdev->usb_type_work);
  2243. cancel_work_sync(&bcdev->subsys_up_work);
  2244. cancel_work_sync(&bcdev->battery_check_work);
  2245. complete(&bcdev->ack);
  2246. unregister_reboot_notifier(&bcdev->reboot_notifier);
  2247. reg_error:
  2248. if (bcdev->notifier_cookie)
  2249. panel_event_notifier_unregister(bcdev->notifier_cookie);
  2250. return rc;
  2251. }
  2252. static int battery_chg_remove(struct platform_device *pdev)
  2253. {
  2254. struct battery_chg_dev *bcdev = platform_get_drvdata(pdev);
  2255. down_write(&bcdev->state_sem);
  2256. atomic_set(&bcdev->state, PMIC_GLINK_STATE_DOWN);
  2257. bcdev->initialized = false;
  2258. up_write(&bcdev->state_sem);
  2259. if (bcdev->notifier_cookie)
  2260. panel_event_notifier_unregister(bcdev->notifier_cookie);
  2261. device_init_wakeup(bcdev->dev, false);
  2262. debugfs_remove_recursive(bcdev->debugfs_dir);
  2263. class_unregister(&bcdev->battery_class);
  2264. pmic_glink_unregister_client(bcdev->client);
  2265. cancel_work_sync(&bcdev->subsys_up_work);
  2266. cancel_work_sync(&bcdev->usb_type_work);
  2267. cancel_work_sync(&bcdev->battery_check_work);
  2268. unregister_reboot_notifier(&bcdev->reboot_notifier);
  2269. return 0;
  2270. }
  2271. static const struct of_device_id battery_chg_match_table[] = {
  2272. { .compatible = "qcom,battery-charger" },
  2273. {},
  2274. };
  2275. static struct platform_driver battery_chg_driver = {
  2276. .driver = {
  2277. .name = "qti_battery_charger",
  2278. .of_match_table = battery_chg_match_table,
  2279. },
  2280. .probe = battery_chg_probe,
  2281. .remove = battery_chg_remove,
  2282. };
  2283. module_platform_driver(battery_chg_driver);
  2284. MODULE_DESCRIPTION("QTI Glink battery charger driver");
  2285. MODULE_LICENSE("GPL v2");