extcon-max77804k.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585
  1. /*
  2. * max77804k-muic.c - MUIC driver for the Maxim 77804k
  3. *
  4. * Copyright (C) 2012 Samsung Electronics.
  5. * <sukdong.kim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/i2c.h>
  24. #include <linux/gpio.h>
  25. #include <linux/slab.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/err.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/input.h>
  31. #include <linux/mfd/max77804k.h>
  32. #include <linux/mfd/max77804k-private.h>
  33. #ifdef CONFIG_USB_HOST_NOTIFY
  34. #include <linux/host_notify.h>
  35. #endif
  36. #include <linux/wakelock.h>
  37. #include <linux/switch.h>
  38. #ifdef CONFIG_USBHUB_USB3804k
  39. #include <linux/usb3804k.h>
  40. #endif
  41. #include <linux/delay.h>
  42. #include <linux/extcon.h>
  43. #if defined(CONFIG_SWITCH_DUAL_MODEM) || defined(CONFIG_MUIC_RUSTPROOF_UART)
  44. #include <linux/sec_param.h>
  45. #endif
  46. #define DEV_NAME "max77804k-muic"
  47. #if defined(CONFIG_CHARGER_SMB1357)
  48. extern int smb1357_otg_control(int enable);
  49. #endif
  50. #if defined(CONFIG_MUIC_DET_JACK)
  51. #define MICEN_VALUE 1
  52. #else
  53. #define MICEN_VALUE 0
  54. #endif
  55. /* for providing API */
  56. static struct max77804k_muic_info *gInfo;
  57. /* For restore charger interrupt states */
  58. static u8 chg_int_state;
  59. /* MAX77804K MUIC CHG_TYP setting values */
  60. enum {
  61. /* No Valid voltage at VB (Vvb < Vvbdet) */
  62. CHGTYP_NO_VOLTAGE = 0x00,
  63. /* Unknown (D+/D- does not present a valid USB charger signature) */
  64. CHGTYP_USB = 0x01,
  65. /* Charging Downstream Port */
  66. CHGTYP_DOWNSTREAM_PORT = 0x02,
  67. /* Dedicated Charger (D+/D- shorted) */
  68. CHGTYP_DEDICATED_CHGR = 0x03,
  69. /* Special 500mA charger, max current 500mA */
  70. CHGTYP_500MA = 0x04,
  71. /* Special 1A charger, max current 1A */
  72. CHGTYP_1A = 0x05,
  73. /* 3.3V Bias on D+/D- */
  74. CHGTYP_SPECIAL_CHGR = 0x06,
  75. /* Dead Battery Charging, max current 100mA */
  76. CHGTYP_DB_100MA = 0x07,
  77. CHGTYP_MAX,
  78. CHGTYP_INIT,
  79. CHGTYP_MIN = CHGTYP_NO_VOLTAGE
  80. };
  81. #define CHGTYP (CHGTYP_USB | CHGTYP_DOWNSTREAM_PORT |\
  82. CHGTYP_DEDICATED_CHGR | CHGTYP_500MA | CHGTYP_1A)
  83. enum {
  84. ADC_GND = 0x00,
  85. ADC_MHL_OR_SENDEND = 0x01,
  86. ADC_BUTTON_S1 = 0x02,
  87. ADC_BUTTON_S2 = 0x03,
  88. ADC_BUTTON_S3 = 0x04,
  89. ADC_BUTTON_S4 = 0x05,
  90. ADC_BUTTON_S5 = 0x06,
  91. ADC_BUTTON_S6 = 0x07,
  92. ADC_BUTTON_S7 = 0x08,
  93. ADC_BUTTON_S8 = 0x09,
  94. ADC_BUTTON_S11 = 0x0c,
  95. ADC_BUTTON_S12 = 0x0d,
  96. ADC_VZW_USB_DOCK = 0x0e, /* 0x01110 28.7K ohm VZW Dock */
  97. ADC_VZW_INCOMPATIBLE = 0x0f, /* 0x01111 34K ohm VZW Incompatible */
  98. ADC_SMARTDOCK = 0x10, /* 0x10000 40.2K ohm */
  99. ADC_HMT = 0x11, /* 0x10001 49.9K ohm */
  100. ADC_AUDIODOCK = 0x12, /* 0x10010 64.9K ohm */
  101. ADC_LANHUB = 0x13, /* 0x10011 80.07K ohm */
  102. ADC_CHARGING_CABLE = 0x14, /* 0x10100 102K ohm */
  103. ADC_MPOS = 0x15, /* 0x10100 102K ohm */
  104. ADC_UART = 0x16, /* 0x10100 102K ohm */
  105. ADC_CEA936ATYPE1_CHG = 0x17, /* 0x10111 200K ohm */
  106. ADC_JIG_USB_OFF = 0x18, /* 0x11000 255K ohm */
  107. ADC_JIG_USB_ON = 0x19, /* 0x11001 301K ohm */
  108. ADC_DESKDOCK = 0x1a, /* 0x11010 365K ohm */
  109. ADC_CEA936ATYPE2_CHG = 0x1b, /* 0x11011 442K ohm */
  110. ADC_JIG_UART_OFF = 0x1c, /* 0x11100 523K ohm */
  111. ADC_JIG_UART_ON = 0x1d, /* 0x11101 619K ohm */
  112. ADC_CARDOCK = 0x1d, /* 0x11101 619K ohm */
  113. #if defined(CONFIG_MUIC_DET_JACK)
  114. ADC_EARJACK = 0x1e, /* 0x11110 1000 or 1002 ohm */
  115. ADC_DOCK_VOL_DN = 0x0a, /* 0x01010 14.46K ohm */
  116. ADC_DOCK_VOL_UP = 0x0b, /* 0x01011 17.26K ohm */
  117. #else
  118. ADC_PHONE_POWERED = 0x1e, /* 0x11110 1000 or 1002 ohm */
  119. ADC_BUTTON_S9 = 0x0a,
  120. ADC_BUTTON_S10 = 0x0b,
  121. #endif
  122. ADC_OPEN = 0x1f
  123. };
  124. struct max77804k_muic_info {
  125. struct device *dev;
  126. struct max77804k_dev *max77804k;
  127. struct i2c_client *muic;
  128. struct max77804k_muic_data *muic_data;
  129. struct extcon_dev *edev;
  130. int irq_adc;
  131. int irq_vbvolt;
  132. int irq_chgtype;
  133. int mansw;
  134. int path;
  135. int otg_test;
  136. u8 adc_mode;
  137. struct wake_lock muic_wake_lock;
  138. enum cable_type_muic cable_type;
  139. enum extcon_cable_name cable_name;
  140. struct delayed_work init_work;
  141. #if defined(CONFIG_MUIC_DET_JACK)
  142. struct input_dev *input;
  143. struct work_struct earjack_work;
  144. struct work_struct earjackkey_work;
  145. #endif
  146. struct mutex mutex;
  147. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK)
  148. bool is_factory_start;
  149. #endif /* !CONFIG_MUIC_MAX77804K_SUPORT_CAR_DOCK */
  150. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  151. bool uart_en;
  152. #endif
  153. u8 adc;
  154. u8 adc1k;
  155. u8 chgtyp;
  156. u8 vbvolt;
  157. bool is_adc_open_prev;
  158. #if defined(CONFIG_MUIC_DET_JACK)
  159. bool eardetected;
  160. bool earkeypressed;
  161. int init_detect_done;
  162. unsigned int old_keycode;
  163. unsigned int keycode;
  164. #endif
  165. #if defined(CONFIG_MUIC_SUPPORT_MMD_CTL)
  166. bool mmd_en;
  167. #endif
  168. #if defined(CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  169. unsigned int pre_state;
  170. unsigned int curr_state;
  171. bool lanhub_ta_status;
  172. #endif
  173. };
  174. #if defined(CONFIG_MUIC_DET_JACK)
  175. static struct switch_dev switch_earjack = {
  176. .name = "h2w", /* /sys/class/switch/h2w/state */
  177. };
  178. static struct switch_dev switch_earjackkey = {
  179. .name = "send_end", /* /sys/class/switch/send_end/state */
  180. };
  181. #endif
  182. static const char const *max77804k_path_name[] = {
  183. [PATH_OPEN] = "OPEN",
  184. [PATH_USB_AP] = "USB-AP",
  185. [PATH_AUDIO] = "AUDIO",
  186. [PATH_UART_AP] = "UART-AP",
  187. [PATH_USB_CP] = "USB-CP",
  188. [PATH_UART_CP] = "UART-CP",
  189. NULL,
  190. };
  191. static struct switch_dev switch_uart3 = {
  192. .name = "uart3", /* /sys/class/switch/uart3/state */
  193. };
  194. static int if_muic_info;
  195. static int switch_sel;
  196. static int if_pmic_rev;
  197. int is_cardock;
  198. static bool is_lpm_mode;
  199. EXPORT_SYMBOL(is_cardock);
  200. void max77804k_update_jig_state(struct max77804k_muic_info *info);
  201. /* func : get_if_pmic_inifo
  202. * switch_sel value get from bootloader comand line
  203. * switch_sel data consist 8 bits (xxxxzzzz)
  204. * first 4bits(zzzz) mean path infomation.
  205. * next 4bits(xxxx) mean if pmic version info
  206. */
  207. static int get_if_pmic_inifo(char *str)
  208. {
  209. get_option(&str, &if_muic_info);
  210. switch_sel = if_muic_info & 0x0f;
  211. if_pmic_rev = (if_muic_info & 0xf0) >> 4;
  212. pr_info("%s %s: switch_sel: %x if_pmic_rev:%x\n",
  213. __FILE__, __func__, switch_sel, if_pmic_rev);
  214. return if_muic_info;
  215. }
  216. __setup("pmic_info=", get_if_pmic_inifo);
  217. int get_switch_sel(void)
  218. {
  219. return switch_sel;
  220. }
  221. static int max77804k_muic_set_comp2_comn1_pass2
  222. (struct max77804k_muic_info *info, int type, int path)
  223. {
  224. /* type 0 == usb, type 1 == uart */
  225. u8 cntl1_val, cntl1_msk;
  226. int ret = 0;
  227. int val;
  228. dev_info(info->dev, "func: %s type: %d path: %d\n",
  229. __func__, type, path);
  230. if (type == 0) {
  231. if (path == PATH_USB_AP) {
  232. info->muic_data->usb_sel = PATH_USB_AP;
  233. val = MAX77804K_MUIC_CTRL1_BIN_1_001;
  234. } else if (path == PATH_USB_CP) {
  235. info->muic_data->usb_sel = PATH_USB_CP;
  236. val = MAX77804K_MUIC_CTRL1_BIN_4_100;
  237. } else {
  238. dev_err(info->dev, "func: %s invalid usb path\n"
  239. , __func__);
  240. return -EINVAL;
  241. }
  242. } else if (type == 1) {
  243. if (path == PATH_UART_AP) {
  244. info->muic_data->uart_sel = PATH_UART_AP;
  245. val = MAX77804K_MUIC_CTRL1_BIN_3_011;
  246. } else if (path == PATH_UART_CP) {
  247. info->muic_data->uart_sel = PATH_UART_CP;
  248. val = MAX77804K_MUIC_CTRL1_BIN_5_101;
  249. } else {
  250. dev_err(info->dev, "func: %s invalid uart path\n"
  251. , __func__);
  252. return -EINVAL;
  253. }
  254. }
  255. #if defined(CONFIG_MUIC_DET_JACK)
  256. else if (type == 2) {
  257. val = MAX77804K_MUIC_CTRL1_BIN_2_010;
  258. }
  259. #endif
  260. else {
  261. dev_err(info->dev, "func: %s invalid path type(%d)\n"
  262. , __func__, type);
  263. return -EINVAL;
  264. }
  265. cntl1_val = (val << COMN1SW_SHIFT) | (val << COMP2SW_SHIFT);
  266. cntl1_msk = COMN1SW_MASK | COMP2SW_MASK;
  267. #if defined(CONFIG_MUIC_DET_JACK)
  268. if (type == 2 && info->init_detect_done == INIT_START) {
  269. cntl1_val = cntl1_val | (MICEN_VALUE << MICEN_SHIFT);
  270. cntl1_msk = cntl1_msk | MICEN_MASK;
  271. }
  272. #endif
  273. max77804k_update_reg(info->muic, MAX77804K_MUIC_REG_CTRL1, cntl1_val,
  274. cntl1_msk);
  275. return ret;
  276. }
  277. static int max77804k_muic_set_uart_sel_pass2
  278. (struct max77804k_muic_info *info, int path)
  279. {
  280. int ret = 0;
  281. ret = max77804k_muic_set_comp2_comn1_pass2(info, 1/*uart*/, path);
  282. return ret;
  283. }
  284. #if defined(CONFIG_MUIC_DET_JACK)
  285. static int max77804k_muic_set_audio_path_pass2(struct max77804k_muic_info *info, int path)
  286. {
  287. int ret = 0;
  288. ret = max77804k_muic_set_comp2_comn1_pass2(info, 2/*audio*/, path);
  289. return ret;
  290. }
  291. #endif
  292. static ssize_t max77804k_muic_show_usb_state(struct device *dev,
  293. struct device_attribute *attr,
  294. char *buf)
  295. {
  296. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  297. dev_info(info->dev, "func:%s info->cable_name:[%s]\n",
  298. __func__, extcon_cable_name[info->cable_name]);
  299. switch (info->cable_name) {
  300. case EXTCON_USB:
  301. case EXTCON_JIG_USBOFF:
  302. case EXTCON_JIG_USBON:
  303. return sprintf(buf, "USB_STATE_CONFIGURED\n");
  304. default:
  305. break;
  306. }
  307. return sprintf(buf, "USB_STATE_NOTCONFIGURED\n");
  308. }
  309. static ssize_t max77804k_muic_show_attached_dev(struct device *dev,
  310. struct device_attribute *attr,
  311. char *buf)
  312. {
  313. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  314. dev_info(info->dev, "func:%s info->cable_name:[%s]\n",
  315. __func__, extcon_cable_name[info->cable_name]);
  316. switch (info->cable_name) {
  317. case EXTCON_NONE:
  318. return sprintf(buf, "No cable\n");
  319. case EXTCON_USB:
  320. return sprintf(buf, "USB\n");
  321. case EXTCON_USB_HOST:
  322. return sprintf(buf, "OTG\n");
  323. case EXTCON_USB_HOST_5V:
  324. return sprintf(buf, "OTG Test\n");
  325. case EXTCON_TA:
  326. return sprintf(buf, "TA\n");
  327. case EXTCON_UNDEFINED_CHARGER:
  328. return sprintf(buf, "Undefined TA\n");
  329. case EXTCON_CEA936_CHG:
  330. return sprintf(buf, "CEA936 TA\n");
  331. case EXTCON_CHARGE_DOWNSTREAM:
  332. return sprintf(buf, "CDP\n");
  333. case EXTCON_DESKDOCK:
  334. return sprintf(buf, "Desk Dock\n");
  335. case EXTCON_DESKDOCK_VB:
  336. return sprintf(buf, "Desk Dock with VB\n");
  337. case EXTCON_SMARTDOCK:
  338. return sprintf(buf, "Smart Dock\n");
  339. case EXTCON_SMARTDOCK_TA:
  340. return sprintf(buf, "Smart Dock with TA\n");
  341. case EXTCON_SMARTDOCK_USB:
  342. return sprintf(buf, "Smart Dock with USB\n");
  343. case EXTCON_AUDIODOCK:
  344. return sprintf(buf, "Audio Dock\n");
  345. case EXTCON_CARDOCK:
  346. return sprintf(buf, "Car Dock\n");
  347. case EXTCON_JIG_UARTOFF:
  348. return sprintf(buf, "JIG UART OFF\n");
  349. case EXTCON_JIG_UARTOFF_VB:
  350. return sprintf(buf, "JIG UART OFF/VB\n");
  351. case EXTCON_JIG_UARTON:
  352. return sprintf(buf, "JIG UART ON\n");
  353. case EXTCON_JIG_USBOFF:
  354. return sprintf(buf, "JIG USB OFF\n");
  355. case EXTCON_JIG_USBON:
  356. return sprintf(buf, "JIG USB ON\n");
  357. case EXTCON_MHL:
  358. return sprintf(buf, "mHL\n");
  359. case EXTCON_MHL_VB:
  360. return sprintf(buf, "mHL charging\n");
  361. case EXTCON_INCOMPATIBLE:
  362. return sprintf(buf, "Incompatible TA\n");
  363. case EXTCON_CHARGING_CABLE:
  364. return sprintf(buf, "Charging Cable\n");
  365. #if defined(CONFIG_MUIC_MAX77804K_SUPPORT_HMT_DETECTION)
  366. case EXTCON_HMT:
  367. return sprintf(buf, "HMT\n");
  368. #endif
  369. #if defined(CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  370. case EXTCON_LANHUB:
  371. return sprintf(buf, "Lanhub\n");
  372. case EXTCON_LANHUB_TA:
  373. return sprintf(buf, "Lanhub with TA\n");
  374. #endif
  375. #if defined(CONFIG_MUIC_DET_JACK)
  376. case EXTCON_EARJACK:
  377. return sprintf(buf, "Earjack\n");
  378. #endif
  379. default:
  380. break;
  381. }
  382. return sprintf(buf, "UNKNOWN\n");
  383. }
  384. static ssize_t max77804k_muic_show_manualsw(struct device *dev,
  385. struct device_attribute *attr,
  386. char *buf)
  387. {
  388. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  389. dev_info(info->dev, "func:%s ap(0),cp(1),vps(2)usb_sel:%d\n",
  390. __func__, info->muic_data->usb_sel);/*For debuging*/
  391. switch (info->muic_data->usb_sel) {
  392. case PATH_USB_AP:
  393. return sprintf(buf, "PDA\n");
  394. case PATH_USB_CP:
  395. return sprintf(buf, "MODEM\n");
  396. case PATH_AUDIO:
  397. return sprintf(buf, "Audio\n");
  398. default:
  399. break;
  400. }
  401. return sprintf(buf, "UNKNOWN\n");
  402. }
  403. static int max77804k_muic_set_usb_path(struct max77804k_muic_info *info,
  404. int path);
  405. static ssize_t max77804k_muic_set_manualsw(struct device *dev,
  406. struct device_attribute *attr,
  407. const char *buf, size_t count)
  408. {
  409. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  410. dev_info(info->dev, "func:%s buf:%s,count:%d\n", __func__, buf, count);
  411. if (!strncasecmp(buf, "PDA", 3)) {
  412. info->muic_data->usb_sel = PATH_USB_AP;
  413. dev_info(info->dev, "%s: PATH_USB_AP\n", __func__);
  414. } else if (!strncasecmp(buf, "MODEM", 5)) {
  415. info->muic_data->usb_sel = PATH_USB_CP;
  416. dev_info(info->dev, "%s: PATH_USB_CP\n", __func__);
  417. max77804k_muic_set_usb_path(info, PATH_USB_CP);
  418. } else
  419. dev_warn(info->dev, "%s: Wrong command\n", __func__);
  420. return count;
  421. }
  422. static ssize_t max77804k_muic_show_adc(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  426. int ret;
  427. u8 val;
  428. ret = max77804k_read_reg(info->muic, MAX77804K_MUIC_REG_STATUS1, &val);
  429. dev_info(info->dev, "func:%s ret:%d val:%x\n", __func__, ret, val);
  430. if (ret) {
  431. dev_err(info->dev, "%s: fail to read muic reg\n", __func__);
  432. return sprintf(buf, "UNKNOWN\n");
  433. }
  434. return sprintf(buf, "%x\n", (val & STATUS1_ADC_MASK));
  435. }
  436. static ssize_t max77804k_muic_show_audio_path(struct device *dev,
  437. struct device_attribute *attr,
  438. char *buf)
  439. {
  440. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  441. int ret;
  442. u8 val;
  443. ret = max77804k_read_reg(info->muic, MAX77804K_MUIC_REG_CTRL1, &val);
  444. dev_info(info->dev, "func:%s ret:%d val:%x\n", __func__, ret, val);
  445. if (ret) {
  446. dev_err(info->dev, "%s: fail to read muic reg\n", __func__);
  447. return sprintf(buf, "UNKNOWN\n");
  448. }
  449. return sprintf(buf, "%x\n", val);
  450. }
  451. static ssize_t max77804k_muic_set_audio_path(struct device *dev,
  452. struct device_attribute *attr,
  453. const char *buf, size_t count)
  454. {
  455. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  456. struct i2c_client *client = info->muic;
  457. u8 cntl1_val, cntl1_msk;
  458. u8 val;
  459. dev_info(info->dev, "func:%s buf:%s\n", __func__, buf);
  460. if (!strncmp(buf, "0", 1))
  461. val = MAX77804K_MUIC_CTRL1_BIN_0_000;
  462. else if (!strncmp(buf, "1", 1))
  463. val = MAX77804K_MUIC_CTRL1_BIN_2_010;
  464. else {
  465. dev_warn(info->dev, "%s: Wrong command\n", __func__);
  466. return count;
  467. }
  468. cntl1_val = (val << COMN1SW_SHIFT) | (val << COMP2SW_SHIFT) |
  469. (MICEN_VALUE << MICEN_SHIFT);
  470. cntl1_msk = COMN1SW_MASK | COMP2SW_MASK | MICEN_MASK;
  471. max77804k_update_reg(client, MAX77804K_MUIC_REG_CTRL1, cntl1_val,
  472. cntl1_msk);
  473. dev_info(info->dev, "MUIC cntl1_val:%x, cntl1_msk:%x\n", cntl1_val,
  474. cntl1_msk);
  475. cntl1_val = MAX77804K_MUIC_CTRL1_BIN_0_000;
  476. max77804k_read_reg(client, MAX77804K_MUIC_REG_CTRL1, &cntl1_val);
  477. dev_info(info->dev, "%s: CNTL1(0x%02x)\n", __func__, cntl1_val);
  478. return count;
  479. }
  480. static ssize_t max77804k_muic_show_otg_test(struct device *dev,
  481. struct device_attribute *attr,
  482. char *buf)
  483. {
  484. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  485. int ret;
  486. u8 val;
  487. ret = max77804k_read_reg(info->muic, MAX77804K_MUIC_REG_CDETCTRL1, &val);
  488. dev_info(info->dev, "func:%s ret:%d val:%x buf%s\n",
  489. __func__, ret, val, buf);
  490. if (ret) {
  491. dev_err(info->dev, "%s: fail to read muic reg\n", __func__);
  492. return sprintf(buf, "UNKNOWN\n");
  493. }
  494. val &= CHGDETEN_MASK;
  495. return sprintf(buf, "%x\n", val);
  496. }
  497. static ssize_t max77804k_muic_set_otg_test(struct device *dev,
  498. struct device_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  502. struct i2c_client *client = info->muic;
  503. u8 val;
  504. dev_info(info->dev, "func:%s buf:%s\n", __func__, buf);
  505. if (!strncmp(buf, "0", 1))
  506. val = 0;
  507. else if (!strncmp(buf, "1", 1))
  508. val = 1;
  509. else {
  510. dev_warn(info->dev, "%s: Wrong command\n", __func__);
  511. return count;
  512. }
  513. max77804k_update_reg(client, MAX77804K_MUIC_REG_CDETCTRL1,
  514. val << CHGDETEN_SHIFT, CHGDETEN_MASK);
  515. val = 0;
  516. max77804k_read_reg(client, MAX77804K_MUIC_REG_CDETCTRL1, &val);
  517. dev_info(info->dev, "%s: CDETCTRL(0x%02x)\n", __func__, val);
  518. return count;
  519. }
  520. #if defined(CONFIG_ADC_ONESHOT)
  521. static int max77804k_muic_set_adcmode(struct max77804k_muic_info *info,
  522. int value)
  523. {
  524. int ret;
  525. u8 val;
  526. if (info->adc_mode == value) {
  527. pr_info("%s: same value:%02x\n", __func__, value);
  528. return 1;
  529. }
  530. dev_info(info->dev,"value:%02x\n", value);
  531. if (!info->muic) {
  532. dev_err(info->dev, "%s: no muic i2c client\n", __func__);
  533. return -EINVAL;
  534. }
  535. val = value << CTRL4_ADCMODE_SHIFT;
  536. dev_info(info->dev, "ADC MODE(0x%02x)\n", val);
  537. ret = max77804k_update_reg(info->muic, MAX77804K_MUIC_REG_CTRL4, val,
  538. CTRL4_ADCMODE_MASK);
  539. if (ret < 0) {
  540. dev_err(info->dev, "%s: fail to update reg\n", __func__);
  541. return -EINVAL;
  542. }
  543. info->adc_mode = value;
  544. return 0;
  545. }
  546. #endif
  547. static void max77804k_muic_set_adcdbset(struct max77804k_muic_info *info,
  548. int value)
  549. {
  550. int ret;
  551. u8 val;
  552. dev_info(info->dev, "func:%s value:%x\n", __func__, value);
  553. if (value > 3) {
  554. dev_err(info->dev, "%s: invalid value(%x)\n", __func__, value);
  555. return;
  556. }
  557. if (!info->muic) {
  558. dev_err(info->dev, "%s: no muic i2c client\n", __func__);
  559. return;
  560. }
  561. val = value << CTRL4_ADCDBSET_SHIFT;
  562. dev_info(info->dev, "%s: ADCDBSET(0x%02x)\n", __func__, val);
  563. ret = max77804k_update_reg(info->muic, MAX77804K_MUIC_REG_CTRL4, val,
  564. CTRL4_ADCDBSET_MASK);
  565. if (ret < 0)
  566. dev_err(info->dev, "%s: fail to update reg\n", __func__);
  567. }
  568. static ssize_t max77804k_muic_show_adc_debounce_time(struct device *dev,
  569. struct device_attribute *attr, char *buf)
  570. {
  571. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  572. int ret;
  573. u8 val;
  574. dev_info(info->dev, "func:%s buf:%s\n", __func__, buf);
  575. if (!info->muic)
  576. return sprintf(buf, "No I2C client\n");
  577. ret = max77804k_read_reg(info->muic, MAX77804K_MUIC_REG_CTRL4, &val);
  578. if (ret) {
  579. dev_err(info->dev, "%s: fail to read muic reg\n", __func__);
  580. return sprintf(buf, "UNKNOWN\n");
  581. }
  582. val &= CTRL4_ADCDBSET_MASK;
  583. val = val >> CTRL4_ADCDBSET_SHIFT;
  584. dev_info(info->dev, "func:%s val:%x\n", __func__, val);
  585. return sprintf(buf, "%x\n", val);
  586. }
  587. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  588. static ssize_t max77804k_muic_set_uart_en(struct device *dev,
  589. struct device_attribute *attr,
  590. const char *buf, size_t count)
  591. {
  592. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  593. dev_info(info->dev, "%s, buf : %s\n", __func__, buf);
  594. if (!strncasecmp(buf, "0", 1))
  595. info->uart_en = false;
  596. else if (!strncasecmp(buf, "1", 1))
  597. info->uart_en = true;
  598. else
  599. dev_warn(info->dev, "%s: Wrong command\n", __func__);
  600. return count;
  601. }
  602. static ssize_t max77804k_muic_show_uart_en(struct device *dev,
  603. struct device_attribute *attr,
  604. char *buf)
  605. {
  606. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  607. dev_info(info->dev, "func: UART is %s %s\n", __func__,
  608. ((info->uart_en)?"ENABLED":"DISABLED"));/*For debuging*/
  609. if(info->uart_en)
  610. return sprintf(buf, "1\n"); /* Enabled */
  611. else
  612. return sprintf(buf, "0\n"); /* Disabled */
  613. }
  614. #endif
  615. static ssize_t max77804k_muic_set_uart_sel(struct device *dev,
  616. struct device_attribute *attr,
  617. const char *buf, size_t count)
  618. {
  619. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  620. dev_info(info->dev, "%s, buf : %s\n",__func__,buf);
  621. if (!strncasecmp(buf, "AP", 2)) {
  622. int ret = max77804k_muic_set_uart_sel_pass2
  623. (info, PATH_UART_AP);
  624. if (ret >= 0){
  625. info->muic_data->uart_sel = PATH_UART_AP;
  626. dev_info(info->dev, "UART PATH(CP:0, AP:1):%d\n",
  627. info->muic_data->uart_sel);
  628. }else
  629. dev_err(info->dev, "%s: Change(AP) fail!!", __func__);
  630. #if defined(CONFIG_SWITCH_DUAL_MODEM)
  631. } else if (!strncasecmp(buf, "CP2", 3)) {
  632. int ret = max77804k_muic_set_uart_sel_pass2
  633. (info, PATH_UART_CP);
  634. if (ret >= 0){
  635. info->muic_data->uart_sel = PATH_UART_CP;
  636. dev_info(info->dev, "UART PATH(CP:0, AP:1):%d\n",
  637. info->muic_data->uart_sel);
  638. }else
  639. dev_err(info->dev, "%s: Change(CP) fail!!", __func__);
  640. #endif
  641. } else if (!strncasecmp(buf, "CP", 2)) {
  642. int ret = max77804k_muic_set_uart_sel_pass2
  643. (info, PATH_UART_CP);
  644. if (ret >= 0){
  645. info->muic_data->uart_sel = PATH_UART_CP;
  646. dev_info(info->dev, "UART PATH(CP:0, AP:1):%d\n",
  647. info->muic_data->uart_sel);
  648. }else
  649. dev_err(info->dev, "%s: Change(CP) fail!!", __func__);
  650. } else {
  651. dev_warn(info->dev, "%s: Wrong command\n", __func__);
  652. }
  653. return count;
  654. }
  655. static ssize_t max77804k_muic_show_uart_sel(struct device *dev,
  656. struct device_attribute *attr,
  657. char *buf)
  658. {
  659. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  660. dev_info(info->dev, "func:%s cp(0),ap(1)usb_sel:%d\n",
  661. __func__, info->muic_data->uart_sel);/*For debuging*/
  662. switch (info->muic_data->uart_sel) {
  663. case PATH_UART_AP:
  664. return sprintf(buf, "AP\n");
  665. break;
  666. case PATH_UART_CP:
  667. #if defined(CONFIG_SWITCH_DUAL_MODEM)
  668. return sprintf(buf, "CP2\n");
  669. #else
  670. return sprintf(buf, "CP\n");
  671. #endif
  672. break;
  673. default:
  674. break;
  675. }
  676. return sprintf(buf, "UNKNOWN\n");
  677. }
  678. static ssize_t max77804k_muic_show_charger_type(struct device *dev,
  679. struct device_attribute *attr,
  680. char *buf)
  681. {
  682. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  683. u8 adc, status;
  684. int ret;
  685. ret = max77804k_read_reg(info->muic, MAX77804K_MUIC_REG_STATUS1, &status);
  686. adc = status & STATUS1_ADC_MASK;
  687. /* SYSFS Node : chg_type
  688. * SYSFS State
  689. * 0 : Dedicated Charger
  690. * 1 : Non-Dedicated Charger
  691. * 2 : Cigar Jack
  692. */
  693. switch (adc){
  694. case ADC_MHL_OR_SENDEND:
  695. case ADC_VZW_USB_DOCK:
  696. case ADC_SMARTDOCK:
  697. case ADC_AUDIODOCK:
  698. case ADC_DESKDOCK:
  699. case ADC_CARDOCK:
  700. case ADC_OPEN:
  701. dev_info(info->dev, "%s: Dedicated Charger State\n", __func__);
  702. return snprintf(buf, 4, "%d\n", 0);
  703. break;
  704. case ADC_CEA936ATYPE1_CHG:
  705. dev_info(info->dev, "%s: Undedicated Charger State\n", __func__);
  706. return snprintf(buf, 4, "%d\n", 1);
  707. break;
  708. case ADC_CEA936ATYPE2_CHG:
  709. dev_info(info->dev, "%s: Dedicated Charger State\n", __func__);
  710. return snprintf(buf, 4, "%d\n", 2);
  711. break;
  712. default:
  713. dev_info(info->dev, "%s: Undeclared State\n", __func__);
  714. break;
  715. }
  716. return snprintf(buf, 9, "%s\n", "UNKNOWN");
  717. }
  718. #if defined(CONFIG_MUIC_SUPPORT_MMD_CTL)
  719. static ssize_t max77804k_muic_show_mmd_en(struct device *dev,
  720. struct device_attribute *attr,
  721. char *buf)
  722. {
  723. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  724. /* mmd_en
  725. * -> ture : 1 / false : 0
  726. */
  727. if (info->mmd_en)
  728. return sprintf(buf, "%d\n", 1);
  729. else
  730. return sprintf(buf, "%d\n", 0);
  731. }
  732. static ssize_t max77804k_muic_set_mmd_en(struct device *dev,
  733. struct device_attribute *attr,
  734. const char *buf, size_t count)
  735. {
  736. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  737. pr_info("%s:%s buf:%s\n", DEV_NAME, __func__, buf);
  738. if (!strncmp(buf, "1", 1))
  739. info->mmd_en = true;
  740. else
  741. info->mmd_en = false;
  742. pr_info("%s:%s Multimedia dock is %s\n",
  743. DEV_NAME, __func__, info->mmd_en?"Enabled":"Disabled");
  744. return count;
  745. }
  746. #endif
  747. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK)
  748. static ssize_t max77804k_muic_show_apo_factory(struct device *dev,
  749. struct device_attribute *attr,
  750. char *buf)
  751. {
  752. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  753. const char *mode;
  754. /* true: Factory mode, false: not Factory mode */
  755. if (info->is_factory_start)
  756. mode = "FACTORY_MODE";
  757. else
  758. mode = "NOT_FACTORY_MODE";
  759. pr_info("%s:%s apo factory=%s\n", DEV_NAME, __func__, mode);
  760. return sprintf(buf, "%s\n", mode);
  761. }
  762. static ssize_t max77804k_muic_set_apo_factory(struct device *dev,
  763. struct device_attribute *attr,
  764. const char *buf, size_t count)
  765. {
  766. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  767. const char *mode;
  768. pr_info("%s:%s buf:%s\n", DEV_NAME, __func__, buf);
  769. /* "FACTORY_START": factory mode */
  770. if (!strncmp(buf, "FACTORY_START", 13)) {
  771. info->is_factory_start = true;
  772. mode = "FACTORY_MODE";
  773. } else {
  774. pr_warn("%s:%s Wrong command\n", DEV_NAME, __func__);
  775. return count;
  776. }
  777. pr_info("%s:%s apo factory=%s\n", DEV_NAME, __func__, mode);
  778. return count;
  779. }
  780. #endif /* !CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK */
  781. #if defined(CONFIG_MUIC_DET_JACK)
  782. static ssize_t key_state_onoff_show(struct device *dev, struct device_attribute *attr, char *buf)
  783. {
  784. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  785. int value = 0;
  786. if (info->earkeypressed && info->keycode == KEY_MEDIA)
  787. value = 1;
  788. return snprintf(buf, 4, "%d\n", value);
  789. }
  790. static ssize_t earjack_state_onoff_show(struct device *dev, struct device_attribute *attr, char *buf)
  791. {
  792. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  793. int value = 0;
  794. if (info->eardetected == true)
  795. value = 1;
  796. return snprintf(buf, 4, "%d\n", value);
  797. }
  798. #endif
  799. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK)
  800. static DEVICE_ATTR(apo_factory, 0664,
  801. max77804k_muic_show_apo_factory,
  802. max77804k_muic_set_apo_factory);
  803. #endif /* !CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK */
  804. #if defined(CONFIG_MUIC_SUPPORT_MMD_CTL)
  805. static DEVICE_ATTR(mmd_en, 0664, max77804k_muic_show_mmd_en, max77804k_muic_set_mmd_en);
  806. #endif
  807. static DEVICE_ATTR(chg_type, 0664, max77804k_muic_show_charger_type, NULL);
  808. static DEVICE_ATTR(uart_sel, 0664, max77804k_muic_show_uart_sel,
  809. max77804k_muic_set_uart_sel);
  810. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  811. static DEVICE_ATTR(uart_en, 0664, max77804k_muic_show_uart_en,
  812. max77804k_muic_set_uart_en);
  813. #endif
  814. static DEVICE_ATTR(usb_state, S_IRUGO, max77804k_muic_show_usb_state, NULL);
  815. static DEVICE_ATTR(attached_dev, S_IRUGO, max77804k_muic_show_attached_dev, NULL);
  816. static DEVICE_ATTR(usb_sel, 0664,
  817. max77804k_muic_show_manualsw, max77804k_muic_set_manualsw);
  818. static DEVICE_ATTR(adc, S_IRUGO, max77804k_muic_show_adc, NULL);
  819. static DEVICE_ATTR(audio_path, 0664,
  820. max77804k_muic_show_audio_path, max77804k_muic_set_audio_path);
  821. static DEVICE_ATTR(otg_test, 0664,
  822. max77804k_muic_show_otg_test, max77804k_muic_set_otg_test);
  823. static DEVICE_ATTR(adc_debounce_time, 0664,
  824. max77804k_muic_show_adc_debounce_time,
  825. NULL);
  826. #if defined(CONFIG_MUIC_DET_JACK)
  827. static DEVICE_ATTR(key_state, 0444,
  828. key_state_onoff_show, NULL);
  829. static DEVICE_ATTR(state, 0444,
  830. earjack_state_onoff_show, NULL);
  831. #endif
  832. static struct attribute *max77804k_muic_attributes[] = {
  833. &dev_attr_uart_sel.attr,
  834. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  835. &dev_attr_uart_en.attr,
  836. #endif
  837. &dev_attr_usb_state.attr,
  838. &dev_attr_attached_dev.attr,
  839. &dev_attr_usb_sel.attr,
  840. &dev_attr_adc.attr,
  841. &dev_attr_audio_path.attr,
  842. &dev_attr_otg_test.attr,
  843. &dev_attr_adc_debounce_time.attr,
  844. &dev_attr_chg_type.attr,
  845. #if defined(CONFIG_MUIC_SUPPORT_MMD_CTL)
  846. &dev_attr_mmd_en.attr,
  847. #endif
  848. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK)
  849. &dev_attr_apo_factory.attr,
  850. #endif /* !CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK */
  851. NULL
  852. };
  853. static void max77804k_muic_uart_uevent(int state)
  854. {
  855. #if !defined(CONFIG_MUIC_DET_JACK)
  856. if (switch_uart3.dev != NULL) {
  857. switch_set_state(&switch_uart3, state);
  858. dev_info(gInfo->dev, "%s: state:%d\n", __func__, state);
  859. }
  860. #endif
  861. }
  862. static const struct attribute_group max77804k_muic_group = {
  863. .attrs = max77804k_muic_attributes,
  864. };
  865. static int max77804k_muic_set_usb_path(struct max77804k_muic_info *info, int path)
  866. {
  867. struct i2c_client *client = info->muic;
  868. int ret;
  869. u8 cntl1_val, cntl1_msk;
  870. int val;
  871. dev_info(info->dev, "func:%s path:%d, cable_name:%d\n", __func__, path, info->cable_name);
  872. ret = max77804k_muic_set_safeout(path);
  873. if (ret) {
  874. dev_err(info->dev, "%s: fail to set safout!\n",
  875. __func__);
  876. return ret;
  877. }
  878. switch (path) {
  879. case PATH_USB_AP:
  880. dev_info(info->dev, "%s: PATH_USB_AP\n", __func__);
  881. val = MAX77804K_MUIC_CTRL1_BIN_1_001;
  882. cntl1_val = (val << COMN1SW_SHIFT) | (val << COMP2SW_SHIFT);
  883. cntl1_msk = COMN1SW_MASK | COMP2SW_MASK;
  884. break;
  885. case PATH_AUDIO:
  886. dev_info(info->dev, "%s: PATH_AUDIO\n", __func__);
  887. /* SL1, SR2 */
  888. cntl1_val = (MAX77804K_MUIC_CTRL1_BIN_2_010 << COMN1SW_SHIFT)
  889. | (MAX77804K_MUIC_CTRL1_BIN_2_010 << COMP2SW_SHIFT) |
  890. (MICEN_VALUE << MICEN_SHIFT);
  891. cntl1_msk = COMN1SW_MASK | COMP2SW_MASK | MICEN_MASK;
  892. break;
  893. case PATH_USB_CP:
  894. dev_info(info->dev, "%s: PATH_USB_CP\n", __func__);
  895. val = MAX77804K_MUIC_CTRL1_BIN_4_100;
  896. cntl1_val = (val << COMN1SW_SHIFT) | (val << COMP2SW_SHIFT);
  897. cntl1_msk = COMN1SW_MASK | COMP2SW_MASK;
  898. break;
  899. case PATH_OPEN:
  900. dev_info(info->dev, "%s: PATH_OPEN\n", __func__);
  901. val = MAX77804K_MUIC_CTRL1_BIN_0_000;
  902. cntl1_val = (val << COMN1SW_SHIFT) | (val << COMP2SW_SHIFT);
  903. cntl1_msk = COMN1SW_MASK | COMP2SW_MASK;
  904. break;
  905. default:
  906. dev_warn(info->dev, "%s: invalid path(%d)\n", __func__, path);
  907. return -EINVAL;
  908. }
  909. dev_info(info->dev, "%s: Set manual path\n", __func__);
  910. if (info->cable_name != EXTCON_CARDOCK)
  911. max77804k_update_reg(client, MAX77804K_MUIC_REG_CTRL1, cntl1_val,
  912. cntl1_msk);
  913. max77804k_update_reg(client, MAX77804K_MUIC_REG_CTRL2,
  914. CTRL2_CPEn1_LOWPWD0,
  915. CTRL2_CPEn_MASK | CTRL2_LOWPWD_MASK);
  916. cntl1_val = MAX77804K_MUIC_CTRL1_BIN_0_000;
  917. max77804k_read_reg(client, MAX77804K_MUIC_REG_CTRL1, &cntl1_val);
  918. dev_info(info->dev, "%s: CNTL1(0x%02x)\n", __func__, cntl1_val);
  919. cntl1_val = MAX77804K_MUIC_CTRL1_BIN_0_000;
  920. max77804k_read_reg(client, MAX77804K_MUIC_REG_CTRL2, &cntl1_val);
  921. dev_info(info->dev, "%s: CNTL2(0x%02x)\n", __func__, cntl1_val);
  922. sysfs_notify(&switch_dev->kobj, NULL, "usb_sel");
  923. return 0;
  924. }
  925. void max77804k_muic_send_event(int val)
  926. {
  927. char *envp[2];
  928. if (val == 0)
  929. envp[0] = "USB_CONNECTION=READY";
  930. else
  931. envp[0] = "USB_CONNECTION=DISCONNECTED";
  932. envp[1] = NULL;
  933. dev_info(gInfo->dev, "%s\n", __func__);
  934. kobject_uevent_env(&gInfo->dev->kobj, KOBJ_CHANGE, envp);
  935. }
  936. extern unsigned int system_rev;
  937. #if defined(CONFIG_MUIC_DET_JACK)
  938. static void max77804k_muic_earjack_detect_workfunc(struct work_struct *work)
  939. {
  940. struct max77804k_muic_info *info = container_of(work, struct max77804k_muic_info, earjack_work);
  941. dev_info(info->dev, "%s:EarJack Work eardetected= %d\n", __func__, info->eardetected);
  942. /* send event jack type to upper layer */
  943. switch_set_state(&switch_earjack, info->eardetected);
  944. return;
  945. }
  946. static int max77804k_muic_earjack_detect(struct max77804k_muic_info *info, bool attached)
  947. {
  948. dev_info(info->dev, "%s:EarJack attached= %d\n", __func__,attached);
  949. if (info->init_detect_done == NOT_INIT)
  950. return 0;
  951. if (info->cable_type == CABLE_TYPE_EARJACK_MUIC) {
  952. dev_info(info->dev, "%s: duplicated(EarJack)\n", __func__);
  953. return 0;
  954. }
  955. info->eardetected = attached ? true : false;
  956. schedule_work(&info->earjack_work);
  957. max77804k_muic_set_audio_path_pass2(info, 0);
  958. return 0;
  959. }
  960. static void max77804k_muic_earjack_key_detect_workfunc(struct work_struct *work)
  961. {
  962. struct max77804k_muic_info *info = container_of(work, struct max77804k_muic_info, earjackkey_work);
  963. dev_info(info->dev, "%s:EarJackKey Work earkeypressed= %d\n", __func__, info->earkeypressed);
  964. /* send event sendend key type to upper layer */
  965. switch_set_state(&switch_earjackkey, info->earkeypressed);
  966. return;
  967. }
  968. static int max77804k_muic_earjack_key_detect(struct max77804k_muic_info *info, int adc)
  969. {
  970. unsigned int code;
  971. dev_info(info->dev, "%s:EarJackKey\n", __func__);
  972. if (info->earkeypressed) {
  973. switch (adc) {
  974. case ADC_MHL_OR_SENDEND:
  975. code = KEY_MEDIA;
  976. info->old_keycode = KEY_MEDIA;
  977. break;
  978. case ADC_DOCK_VOL_UP:
  979. code = KEY_VOLUMEUP;
  980. info->old_keycode = KEY_VOLUMEUP;
  981. break;
  982. case ADC_DOCK_VOL_DN:
  983. code = KEY_VOLUMEDOWN;
  984. info->old_keycode = KEY_VOLUMEDOWN;
  985. break;
  986. default:
  987. dev_info(info->dev, "%s: should not reach here(0x%x)\n", __func__, adc);
  988. return 0;
  989. }
  990. info->keycode = code;
  991. }
  992. else {
  993. code = info->old_keycode;
  994. }
  995. /* send event sendend key type to upper layer */
  996. input_event(info->input, EV_KEY, code, info->earkeypressed);
  997. input_sync(info->input);
  998. schedule_work(&info->earjackkey_work);
  999. return 0;
  1000. }
  1001. #endif
  1002. void max77804k_otg_control(struct max77804k_muic_info *info, int enable)
  1003. {
  1004. u8 int_mask, cdetctrl1, chg_cnfg_00;
  1005. #if defined(CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1006. u8 val;
  1007. #endif
  1008. pr_info("%s: enable(%d)\n", __func__, enable);
  1009. if (enable) {
  1010. /* disable charger interrupt */
  1011. max77804k_read_reg(info->max77804k->i2c,
  1012. MAX77804K_CHG_REG_CHG_INT_MASK, &int_mask);
  1013. chg_int_state = int_mask;
  1014. int_mask |= (1 << 4); /* disable chgin intr */
  1015. int_mask |= (1 << 6); /* disable chg */
  1016. int_mask &= ~(1 << 0); /* enable byp intr */
  1017. max77804k_write_reg(info->max77804k->i2c,
  1018. MAX77804K_CHG_REG_CHG_INT_MASK, int_mask);
  1019. /* disable charger detection */
  1020. max77804k_read_reg(info->max77804k->muic,
  1021. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  1022. cdetctrl1 &= ~(1 << 0);
  1023. max77804k_write_reg(info->max77804k->muic,
  1024. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  1025. #if defined(CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1026. if(info->lanhub_ta_status == false) {
  1027. pr_info("%s: adc: %d info->lanhub_ta_status = %d \n",__func__,info->adc, info->lanhub_ta_status);
  1028. /* OTG on, boost on, DIS_MUIC_CTRL=1 */
  1029. max77804k_update_reg(info->max77804k->i2c, MAX77804K_CHG_REG_CHG_CNFG_00,
  1030. CHG_CNFG_00_OTG_CTRL, CHG_CNFG_00_OTG_CTRL);
  1031. } else if (info->lanhub_ta_status == true) {
  1032. /* OTG on, boost on, CHGIN on, EN_MUIC_CTRL=1 */
  1033. pr_info("%s: adc: %d info->lanhub_ta_status = %d \n",__func__,info->adc, info->lanhub_ta_status);
  1034. max77804k_read_reg(info->max77804k->i2c, MAX77804K_CHG_REG_CHG_CNFG_00, &val);
  1035. val &= 0x0F;
  1036. max77804k_write_reg(info->max77804k->i2c, MAX77804K_CHG_REG_CHG_CNFG_00, val);
  1037. }
  1038. #else
  1039. /* OTG on, boost on, DIS_MUIC_CTRL=1 */
  1040. max77804k_update_reg(info->max77804k->i2c, MAX77804K_CHG_REG_CHG_CNFG_00,
  1041. CHG_CNFG_00_OTG_CTRL, CHG_CNFG_00_OTG_CTRL);
  1042. #endif
  1043. max77804k_read_reg(info->max77804k->i2c,
  1044. MAX77804K_CHG_REG_CHG_CNFG_00, &chg_cnfg_00);
  1045. /* Update CHG_CNFG_11 to 0x50(5V) */
  1046. max77804k_write_reg(info->max77804k->i2c,
  1047. MAX77804K_CHG_REG_CHG_CNFG_11, 0x50);
  1048. } else {
  1049. /* OTG off, boost off, (buck on),
  1050. DIS_MUIC_CTRL = 0 unless CHG_ENA = 1 */
  1051. max77804k_update_reg(info->max77804k->i2c, MAX77804K_CHG_REG_CHG_CNFG_00,
  1052. 0x00, CHG_CNFG_00_OTG_CTRL);
  1053. max77804k_read_reg(info->max77804k->i2c,
  1054. MAX77804K_CHG_REG_CHG_CNFG_00, &chg_cnfg_00);
  1055. /* Update CHG_CNFG_11 to 0x00(3V) */
  1056. max77804k_write_reg(info->max77804k->i2c,
  1057. MAX77804K_CHG_REG_CHG_CNFG_11, 0x00);
  1058. mdelay(50);
  1059. /* [MAX77804] Workaround to get rid of reading dummy(0x00) */
  1060. /* disable charger detection again */
  1061. max77804k_read_reg(info->max77804k->muic,
  1062. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  1063. cdetctrl1 &= ~(1 << 0);
  1064. max77804k_write_reg(info->max77804k->muic,
  1065. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  1066. mdelay(10);
  1067. /* enable charger detection */
  1068. max77804k_read_reg(info->max77804k->muic,
  1069. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  1070. cdetctrl1 |= (1 << 0);
  1071. max77804k_write_reg(info->max77804k->muic,
  1072. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  1073. /* enable charger interrupt */
  1074. max77804k_write_reg(info->max77804k->i2c,
  1075. MAX77804K_CHG_REG_CHG_INT_MASK, chg_int_state);
  1076. max77804k_read_reg(info->max77804k->i2c,
  1077. MAX77804K_CHG_REG_CHG_INT_MASK, &int_mask);
  1078. }
  1079. pr_info("%s: INT_MASK(0x%x), CDETCTRL1(0x%x), CHG_CNFG_00(0x%x)\n",
  1080. __func__, int_mask, cdetctrl1, chg_cnfg_00);
  1081. }
  1082. #if defined(CONFIG_CHARGER_SMB1357)
  1083. void max77804k_muic_only_otg_control(struct max77804k_muic_info *info, int enable)
  1084. {
  1085. u8 cdetctrl1;
  1086. pr_info("%s: enable(%d)\n", __func__, enable);
  1087. if (enable) {
  1088. /* disable charger detection */
  1089. max77804k_read_reg(info->max77804k->muic,
  1090. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  1091. cdetctrl1 &= ~(1 << 0);
  1092. max77804k_write_reg(info->max77804k->muic,
  1093. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  1094. } else {
  1095. /* enable charger detection */
  1096. max77804k_read_reg(info->max77804k->muic,
  1097. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  1098. cdetctrl1 |= (1 << 0);
  1099. max77804k_write_reg(info->max77804k->muic,
  1100. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  1101. }
  1102. pr_info("%s: CDETCTRL1(0x%x)\n", __func__, cdetctrl1);
  1103. }
  1104. #endif
  1105. void max77804k_powered_otg_control(struct max77804k_muic_info *info, int enable)
  1106. {
  1107. pr_info("%s: enable(%d)\n", __func__, enable);
  1108. if (enable) {
  1109. /* OTG on, boost on */
  1110. max77804k_write_reg(info->max77804k->i2c,
  1111. MAX77804K_CHG_REG_CHG_CNFG_00, 0x05);
  1112. max77804k_write_reg(info->max77804k->i2c,
  1113. MAX77804K_CHG_REG_CHG_CNFG_02, 0x0E);
  1114. /* Update CHG_CNFG_11 to 0x50(5V) */
  1115. max77804k_write_reg(info->max77804k->i2c,
  1116. MAX77804K_CHG_REG_CHG_CNFG_11, 0x50);
  1117. } else {
  1118. /* OTG off, boost off, (buck on) */
  1119. max77804k_write_reg(info->max77804k->i2c,
  1120. MAX77804K_CHG_REG_CHG_CNFG_00, 0x04);
  1121. /* Update CHG_CNFG_11 to 0x00(3V) */
  1122. max77804k_write_reg(info->max77804k->i2c,
  1123. MAX77804K_CHG_REG_CHG_CNFG_11, 0x00);
  1124. }
  1125. }
  1126. /* use in mach for otg */
  1127. int muic_otg_control(int enable)
  1128. {
  1129. pr_debug("%s: enable(%d)\n", __func__, enable);
  1130. gInfo->otg_test = enable;
  1131. #if defined(CONFIG_CHARGER_SMB1357)
  1132. max77804k_muic_only_otg_control(gInfo, enable);
  1133. smb1357_otg_control(enable);
  1134. #else
  1135. max77804k_otg_control(gInfo, enable);
  1136. #endif
  1137. return 0;
  1138. }
  1139. /* use in mach for powered-otg */
  1140. void powered_otg_control(int enable)
  1141. {
  1142. pr_debug("%s: enable(%d)\n", __func__, enable);
  1143. max77804k_powered_otg_control(gInfo, enable);
  1144. }
  1145. #if 0
  1146. static void max77804k_muic_set_cddelay(struct max77804k_muic_info *info)
  1147. {
  1148. u8 cdetctrl1;
  1149. int ret;
  1150. ret = max77804k_read_reg(info->max77804k->muic,
  1151. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  1152. pr_info("%s:%s read CDETCTRL1=0x%x, ret=%d\n", DEV_NAME, __func__,
  1153. cdetctrl1, ret);
  1154. if ((cdetctrl1 & 0x10) == 0x10) {
  1155. pr_info("%s:%s CDDelay already setted, return\n", DEV_NAME,
  1156. __func__);
  1157. return;
  1158. }
  1159. cdetctrl1 |= 0x10;
  1160. ret = max77804k_write_reg(info->max77804k->muic,
  1161. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  1162. pr_info("%s:%s write CDETCTRL1=0x%x, ret=%d\n", DEV_NAME,
  1163. __func__, cdetctrl1, ret);
  1164. }
  1165. static void max77804k_muic_clear_cddelay(struct max77804k_muic_info *info)
  1166. {
  1167. u8 cdetctrl1;
  1168. int ret;
  1169. ret = max77804k_read_reg(info->max77804k->muic,
  1170. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  1171. pr_info("%s:%s read CDETCTRL1=0x%x, ret=%d\n", DEV_NAME, __func__,
  1172. cdetctrl1, ret);
  1173. if ((cdetctrl1 & 0x10) == 0x0) {
  1174. pr_info("%s:%s CDDelay already cleared, return\n", DEV_NAME,
  1175. __func__);
  1176. return;
  1177. }
  1178. cdetctrl1 &= ~(0x10);
  1179. ret = max77804k_write_reg(info->max77804k->muic,
  1180. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  1181. pr_info("%s:%s write CDETCTRL1=0x%x, ret=%d\n", DEV_NAME,
  1182. __func__, cdetctrl1, ret);
  1183. }
  1184. #endif
  1185. static int max77804k_muic_set_path(struct max77804k_muic_info *info, int path)
  1186. {
  1187. int ret;
  1188. u8 ctrl1_val, ctrl1_msk;
  1189. u8 ctrl2_val, ctrl2_msk;
  1190. int val;
  1191. dev_info(info->dev, "set path to (%d)\n", path);
  1192. ret = max77804k_muic_set_safeout(path);
  1193. if (ret) {
  1194. dev_err(info->dev, "fail to set safout!(%d)\n", ret);
  1195. return ret;
  1196. }
  1197. if ((info->max77804k->pmic_rev == MAX77804K_REV_PASS1) &&
  1198. ((path == PATH_USB_CP) || (path == PATH_UART_CP))) {
  1199. dev_info(info->dev,
  1200. "PASS1 doesn't support manual path to CP.\n");
  1201. if (path == PATH_USB_CP)
  1202. path = PATH_USB_AP;
  1203. else if (path == PATH_UART_CP)
  1204. path = PATH_UART_AP;
  1205. }
  1206. switch (path) {
  1207. case PATH_OPEN:
  1208. val = MAX77804K_MUIC_CTRL1_BIN_0_000;
  1209. break;
  1210. case PATH_USB_AP:
  1211. val = MAX77804K_MUIC_CTRL1_BIN_1_001;
  1212. break;
  1213. case PATH_AUDIO:
  1214. val = MAX77804K_MUIC_CTRL1_BIN_2_010;
  1215. break;
  1216. case PATH_UART_AP:
  1217. val = MAX77804K_MUIC_CTRL1_BIN_3_011;
  1218. break;
  1219. case PATH_USB_CP:
  1220. val = MAX77804K_MUIC_CTRL1_BIN_4_100;
  1221. break;
  1222. case PATH_UART_CP:
  1223. val = MAX77804K_MUIC_CTRL1_BIN_5_101;
  1224. break;
  1225. default:
  1226. dev_warn(info->dev, "invalid path(%d)\n", path);
  1227. return -EINVAL;
  1228. }
  1229. ctrl1_val = (val << COMN1SW_SHIFT) | (val << COMP2SW_SHIFT);
  1230. ctrl1_msk = COMN1SW_MASK | COMP2SW_MASK;
  1231. if (path == PATH_AUDIO) {
  1232. ctrl1_val |= MICEN_VALUE << MICEN_SHIFT;
  1233. ctrl1_msk |= MICEN_MASK;
  1234. }
  1235. max77804k_update_reg(info->muic,
  1236. MAX77804K_MUIC_REG_CTRL1, ctrl1_val, ctrl1_msk);
  1237. ctrl2_val = CTRL2_CPEn1_LOWPWD0;
  1238. ctrl2_msk = CTRL2_CPEn_MASK | CTRL2_LOWPWD_MASK;
  1239. max77804k_update_reg(info->muic,
  1240. MAX77804K_MUIC_REG_CTRL2, ctrl2_val, ctrl2_msk);
  1241. return 0;
  1242. }
  1243. #if defined(CONFIG_MUIC_MAX77804K_SUPPORT_HMT_DETECTION)
  1244. #define MAX77804K_PATH_FIX_USB_MASK \
  1245. (BIT(EXTCON_USB_HOST) | BIT(EXTCON_SMARTDOCK) \
  1246. | BIT(EXTCON_AUDIODOCK) | BIT(EXTCON_HMT))
  1247. #else
  1248. #define MAX77804K_PATH_FIX_USB_MASK \
  1249. (BIT(EXTCON_USB_HOST) | BIT(EXTCON_SMARTDOCK) | BIT(EXTCON_AUDIODOCK))
  1250. #endif
  1251. #define MAX77804K_PATH_USB_MASK \
  1252. (BIT(EXTCON_USB) | BIT(EXTCON_JIG_USBOFF) | BIT(EXTCON_JIG_USBON))
  1253. #if defined(CONFIG_MUIC_DET_JACK)
  1254. #define MAX77804K_PATH_AUDIO_MASK \
  1255. (BIT(EXTCON_DESKDOCK) | BIT(EXTCON_CARDOCK) | BIT(EXTCON_EARJACK))
  1256. #else
  1257. #define MAX77804K_PATH_AUDIO_MASK \
  1258. (BIT(EXTCON_DESKDOCK) | BIT(EXTCON_CARDOCK))
  1259. #endif
  1260. #define MAX77804K_PATH_UART_MASK \
  1261. (BIT(EXTCON_JIG_UARTOFF) | BIT(EXTCON_JIG_UARTON) | BIT(EXTCON_USB_HOST_5V))
  1262. #if defined(CONFIG_CHARGER_SMB1357)
  1263. #define MAX77804K_PATH_TA_MASK \
  1264. (BIT(EXTCON_SMARTDOCK_TA) | BIT(EXTCON_TA))
  1265. #endif
  1266. static int set_muic_path(struct max77804k_muic_info *info)
  1267. {
  1268. u32 state = info->edev->state;
  1269. int path;
  1270. int ret = 0;
  1271. dev_info(info->dev, "%s: current state=0x%x, path=%s\n",
  1272. __func__, state, max77804k_path_name[info->path]);
  1273. if (state & MAX77804K_PATH_FIX_USB_MASK)
  1274. path = PATH_USB_AP;
  1275. else if (state & MAX77804K_PATH_USB_MASK) {
  1276. /* set path to usb following usb_sel */
  1277. if (info->muic_data->usb_sel == PATH_USB_CP)
  1278. path = PATH_USB_CP;
  1279. else
  1280. path = PATH_USB_AP;
  1281. } else if (state & MAX77804K_PATH_UART_MASK) {
  1282. /* set path to uart following uart_sel */
  1283. if (info->muic_data->uart_sel == PATH_UART_CP)
  1284. path = PATH_UART_CP;
  1285. else
  1286. path = PATH_UART_AP;
  1287. } else if (state & MAX77804K_PATH_AUDIO_MASK)
  1288. path = PATH_AUDIO;
  1289. #if defined(CONFIG_CHARGER_SMB1357)
  1290. else if (state & MAX77804K_PATH_TA_MASK) {
  1291. /* set path to TA */
  1292. path = PATH_USB_CP;
  1293. }
  1294. #endif
  1295. else {
  1296. dev_info(info->dev, "%s: don't have to set path\n", __func__);
  1297. return 0;
  1298. }
  1299. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1300. if (!(info->uart_en) && path == PATH_UART_AP) {
  1301. dev_info(info->dev, "%s: UART is Disabled for preventing water damage\n", __func__);
  1302. path = PATH_OPEN;
  1303. }
  1304. #endif
  1305. ret = max77804k_muic_set_path(info, path);
  1306. if (ret < 0) {
  1307. dev_err(info->dev, "fail to set path(%s->%s),stat=%x,ret=%d\n",
  1308. max77804k_path_name[info->path],
  1309. max77804k_path_name[path], state, ret);
  1310. return ret;
  1311. }
  1312. dev_info(info->dev, "%s: path: %s -> %s\n", __func__,
  1313. max77804k_path_name[info->path],
  1314. max77804k_path_name[path]);
  1315. info->path = path;
  1316. return 0;
  1317. }
  1318. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1319. extern int sec_otg_notify(int event);
  1320. static bool lanhub_otg_connected;
  1321. #endif
  1322. /**
  1323. * Desc: In general, it sets the extcon cable worker and sets muic path,
  1324. * One shot mode-LPM is set - if no state change(condition Occur in Nested interrupt call).
  1325. **/
  1326. static void _detected(struct max77804k_muic_info *info, u32 new_state)
  1327. {
  1328. u32 current_state;
  1329. u32 changed_state;
  1330. int index;
  1331. int attach;
  1332. int ret;
  1333. current_state = info->edev->state;
  1334. changed_state = current_state ^ new_state;
  1335. dev_info(info->dev, "state: cur=0x%x, new=0x%x, changed=0x%x\n",
  1336. current_state, new_state, changed_state);
  1337. for (index = 0; index < SUPPORTED_CABLE_MAX; index++) {
  1338. if (!(changed_state & BIT(index)))
  1339. continue;
  1340. if (new_state & BIT(index))
  1341. attach = 1;
  1342. else
  1343. attach = 0;
  1344. ret = extcon_set_cable_state(info->edev,
  1345. extcon_cable_name[index], attach);
  1346. if (unlikely(ret < 0))
  1347. dev_err(info->dev, "fail to notify %s(%d), %d, ret=%d\n",
  1348. extcon_cable_name[index], index, attach, ret);
  1349. }
  1350. #if defined(CONFIG_ADC_ONESHOT)
  1351. /* Set ADC Mode to oneshot */
  1352. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1353. if (!new_state && !lanhub_otg_connected) {
  1354. #else
  1355. if (!new_state) {
  1356. #endif
  1357. max77804k_muic_set_adcmode(info, ADC_ONESHOT);
  1358. msleep(50);
  1359. }
  1360. #endif
  1361. set_muic_path(info);
  1362. }
  1363. #if defined CONFIG_ID_BYPASS_SBL
  1364. int otg_attached = 0;
  1365. #endif
  1366. static int max77804k_muic_handle_attach(struct max77804k_muic_info *info,
  1367. u8 status1, u8 status2)
  1368. {
  1369. u8 adc;
  1370. u8 vbvolt;
  1371. u8 chgtyp;
  1372. u8 chgdetrun;
  1373. u8 adc1k;
  1374. u32 pre_state;
  1375. u32 new_state;
  1376. new_state = 0;
  1377. pre_state = info->edev->state;
  1378. adc = status1 & STATUS1_ADC_MASK;
  1379. /* Only MHL cable */
  1380. adc1k = status1 & STATUS1_ADC1K_MASK;
  1381. /* Charger type detection */
  1382. chgtyp = status2 & STATUS2_CHGTYP_MASK;
  1383. /* When vb >= vbdet then it is set */
  1384. vbvolt = status2 & STATUS2_VBVOLT_MASK;
  1385. /* To make MUIC detect charger detection.*/
  1386. chgdetrun = status2 & STATUS2_CHGDETRUN_MASK;
  1387. dev_info(info->dev, "%s: \n",__func__);
  1388. dev_info(info->dev, "st1:0x%x st2:0x%x\n", status1, status2);
  1389. dev_info(info->dev,
  1390. "adc:0x%x, adc1k:0x%x, chgtyp:0x%x, vbvolt:%d\n",
  1391. adc, adc1k, chgtyp, vbvolt);
  1392. dev_info(info->dev, "chgdetrun:0x%x, prev state:0x%x\n",
  1393. chgdetrun, pre_state);
  1394. /* Workaround for Factory mode in MUIC PASS2.
  1395. * Abandon adc interrupt of approximately +-100K range
  1396. * if previous cable status was JIG UART BOOT OFF.
  1397. * In uart path cp, adc is unstable state
  1398. * MUIC PASS2 turn to AP_UART mode automatically
  1399. * So, in this state set correct path manually.
  1400. */
  1401. #if 0
  1402. if (info->max77804k->pmic_rev > MAX77804K_REV_PASS1) {
  1403. if ((pre_state & BIT(EXTCON_JIG_UARTOFF)) &&
  1404. ((adc == ADC_JIG_UART_OFF + 1 ||
  1405. (adc == ADC_JIG_UART_OFF - 1))) &&
  1406. (info->muic_data->uart_sel == PATH_UART_CP)) {
  1407. dev_warn(info->dev, "abandon ADC\n");
  1408. new_state = BIT(EXTCON_JIG_UARTOFF);
  1409. goto __found_cable;
  1410. }
  1411. }
  1412. #endif
  1413. /* 1Kohm ID regiter detection (mHL)
  1414. * Old MUIC : ADC value:0x00 or 0x01, ADCLow:1
  1415. * New MUIC : ADC value is not set(Open), ADCLow:1, ADCError:1
  1416. */
  1417. if ((info->adc != ADC_OPEN) || (info->adc1k & STATUS1_ADC1K_MASK))
  1418. info->is_adc_open_prev = false;
  1419. if (adc1k) {
  1420. #if defined(CONFIG_USBID_STANDARD_VER_01)
  1421. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_MHL_CABLE_DETECTION)
  1422. if (vbvolt) {
  1423. new_state = BIT(EXTCON_TA);
  1424. info->cable_name = EXTCON_UNDEFINED_CHARGER;
  1425. }
  1426. #endif
  1427. #else
  1428. /* 9th bit gets set for MHL cable. */
  1429. new_state = BIT(EXTCON_MHL);
  1430. info->cable_name = EXTCON_MHL;
  1431. /* If is_otg_boost is true,
  1432. * it means that vbolt is from the device to accessory.
  1433. * Change in vbus detect */
  1434. if (vbvolt) {
  1435. new_state |= BIT(EXTCON_MHL_VB);
  1436. info->cable_name = EXTCON_MHL_VB;
  1437. }
  1438. #endif
  1439. goto __found_cable;
  1440. }
  1441. switch (adc) {
  1442. #if defined(CONFIG_USBID_STANDARD)
  1443. case (ADC_BUTTON_S1)...(ADC_BUTTON_S8):
  1444. #if !defined(CONFIG_MUIC_DET_JACK)
  1445. case ADC_MHL_OR_SENDEND:
  1446. case ADC_BUTTON_S9:
  1447. case ADC_BUTTON_S10:
  1448. case ADC_PHONE_POWERED:
  1449. #endif
  1450. case ADC_BUTTON_S11:
  1451. case ADC_BUTTON_S12:
  1452. case ADC_UART:
  1453. #if defined(CONFIG_USBID_STANDARD_VER_01)
  1454. case ADC_MPOS:
  1455. case ADC_VZW_USB_DOCK:
  1456. #if !defined(CONFIG_USB_HOST_NOTIFY)
  1457. case ADC_GND:
  1458. #endif
  1459. #if !defined(CONFIG_MUIC_SUPPORT_INCOMPATIBLE_CHARGER)
  1460. case ADC_VZW_INCOMPATIBLE:
  1461. #endif
  1462. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1463. case ADC_LANHUB:
  1464. #endif
  1465. #if !defined(CONFIG_MUIC_SUPPORT_SMART_DOCK)
  1466. case ADC_SMARTDOCK:
  1467. #endif
  1468. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_HMT_DETECTION)
  1469. case ADC_HMT:
  1470. #endif
  1471. #if !defined(CONFIG_MUIC_SUPPORT_DESK_DOCK)
  1472. case ADC_DESKDOCK:
  1473. #endif
  1474. #if !defined(CONFIG_MUIC_SUPPORT_AUDIO_DOCK)
  1475. case ADC_AUDIODOCK:
  1476. #endif
  1477. #endif
  1478. if (vbvolt) {
  1479. new_state = BIT(EXTCON_TA);
  1480. info->cable_name = EXTCON_UNDEFINED_CHARGER;
  1481. }
  1482. break;
  1483. #endif
  1484. #if defined(CONFIG_USB_HOST_NOTIFY)
  1485. case ADC_GND:
  1486. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1487. if (!max77804k_muic_set_adcmode(info, ADC_ALWAYS))
  1488. msleep(50);
  1489. info->curr_state = ADC_GND;
  1490. info->lanhub_ta_status = false;
  1491. lanhub_otg_connected = true;
  1492. #endif
  1493. new_state = BIT(EXTCON_USB_HOST);
  1494. info->cable_name = EXTCON_USB_HOST;
  1495. #if defined CONFIG_ID_BYPASS_SBL
  1496. otg_attached = 1;
  1497. #endif
  1498. break;
  1499. #endif
  1500. #if defined(CONFIG_MUIC_SUPPORT_INCOMPATIBLE_CHARGER)
  1501. case ADC_VZW_INCOMPATIBLE:
  1502. new_state = BIT(EXTCON_INCOMPATIBLE);
  1503. info->cable_name = EXTCON_INCOMPATIBLE;
  1504. break;
  1505. #endif
  1506. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1507. case ADC_LANHUB:
  1508. /* Call Lan Hub handler here */
  1509. if (!max77804k_muic_set_adcmode(info, ADC_ALWAYS))
  1510. msleep(50);
  1511. pr_info("%s:LANHUB+TA Detected\n",__func__);
  1512. lanhub_otg_connected = true;
  1513. if(info->lanhub_ta_status == true)
  1514. {
  1515. new_state |= (BIT(EXTCON_USB_HOST) | BIT(EXTCON_LANHUB_TA));
  1516. goto __found_cable;
  1517. }
  1518. if ((info->pre_state == ADC_OPEN) && (!chgtyp)) {
  1519. pr_info("%s:LANHUB+TA Attached Directly\n",__func__);
  1520. sec_otg_notify(HNOTIFY_ID);
  1521. new_state = (BIT(EXTCON_USB_HOST) | BIT(EXTCON_LANHUB_TA));
  1522. } else if(info->pre_state == ADC_GND) {
  1523. new_state = BIT(EXTCON_LANHUB_TA);
  1524. pr_info("%s:LANHUB to LANHUB+TA Change\n",__func__);
  1525. }
  1526. info->curr_state = ADC_LANHUB;
  1527. info->lanhub_ta_status = true;
  1528. break;
  1529. #endif
  1530. #if defined(CONFIG_MUIC_SUPPORT_SMART_DOCK)
  1531. case ADC_SMARTDOCK:
  1532. #if defined(CONFIG_ADC_ONESHOT)
  1533. /* Set ADC Mode to Always */
  1534. if (!max77804k_muic_set_adcmode(info, ADC_ALWAYS))
  1535. msleep(50);
  1536. #endif
  1537. new_state = BIT(EXTCON_SMARTDOCK);
  1538. info->cable_name = EXTCON_SMARTDOCK;
  1539. if (chgtyp == CHGTYP_DEDICATED_CHGR) {
  1540. new_state |= BIT(EXTCON_SMARTDOCK_TA);
  1541. info->cable_name = EXTCON_SMARTDOCK_TA;
  1542. } else if (chgtyp == CHGTYP_USB) {
  1543. new_state |= BIT(EXTCON_SMARTDOCK_USB);
  1544. info->cable_name = EXTCON_SMARTDOCK_USB;
  1545. }
  1546. break;
  1547. #endif
  1548. #if defined(CONFIG_MUIC_MAX77804K_SUPPORT_HMT_DETECTION)
  1549. case ADC_HMT:
  1550. #if defined(CONFIG_ADC_ONESHOT)
  1551. /* Set ADC Mode to Always */
  1552. if (!max77804k_muic_set_adcmode(info, ADC_ALWAYS))
  1553. msleep(50);
  1554. #endif
  1555. new_state = BIT(EXTCON_HMT);
  1556. new_state |= BIT(EXTCON_USB_HOST);
  1557. info->cable_name = EXTCON_HMT;
  1558. break;
  1559. #endif
  1560. case ADC_JIG_UART_OFF:
  1561. new_state = BIT(EXTCON_JIG_UARTOFF);
  1562. info->cable_name = EXTCON_JIG_UARTOFF;
  1563. if (vbvolt) {
  1564. if (gInfo->otg_test) {/* add for DFMS */
  1565. new_state |= BIT(EXTCON_USB_HOST_5V);
  1566. info->cable_name = EXTCON_USB_HOST_5V;
  1567. } else {
  1568. new_state |= BIT(EXTCON_JIG_UARTOFF_VB);
  1569. info->cable_name = EXTCON_JIG_UARTOFF_VB;
  1570. }
  1571. }
  1572. break;
  1573. case ADC_CARDOCK: /* ADC_CARDOCK == ADC_JIG_UART_ON */
  1574. #if defined(CONFIG_SEC_FACTORY)
  1575. new_state = BIT(EXTCON_CARDOCK);
  1576. #else
  1577. new_state = BIT(EXTCON_JIG_UARTON);
  1578. #endif
  1579. info->cable_name = EXTCON_JIG_UARTON;
  1580. break;
  1581. case ADC_JIG_USB_OFF:
  1582. if (vbvolt) {
  1583. new_state = BIT(EXTCON_JIG_USBOFF);
  1584. info->cable_name = EXTCON_JIG_USBOFF;
  1585. }
  1586. break;
  1587. case ADC_JIG_USB_ON:
  1588. if (vbvolt) {
  1589. new_state = BIT(EXTCON_JIG_USBON);
  1590. info->cable_name = EXTCON_JIG_USBON;
  1591. }
  1592. break;
  1593. #if defined(CONFIG_MUIC_SUPPORT_DESK_DOCK)
  1594. case ADC_DESKDOCK:
  1595. new_state = BIT(EXTCON_DESKDOCK);
  1596. info->cable_name = EXTCON_DESKDOCK;
  1597. if (vbvolt) {
  1598. new_state |= BIT(EXTCON_DESKDOCK_VB);
  1599. info->cable_name = EXTCON_DESKDOCK_VB;
  1600. }
  1601. break;
  1602. #endif
  1603. #if defined(CONFIG_MUIC_SUPPORT_AUDIO_DOCK)
  1604. case ADC_AUDIODOCK:
  1605. new_state = BIT(EXTCON_AUDIODOCK);
  1606. info->cable_name = EXTCON_AUDIODOCK;
  1607. break;
  1608. #endif
  1609. #if defined(CONFIG_MUIC_DET_JACK)
  1610. case ADC_MHL_OR_SENDEND:
  1611. case ADC_DOCK_VOL_UP:
  1612. case ADC_DOCK_VOL_DN:
  1613. if ((info->cable_type == CABLE_TYPE_EARJACK_MUIC)) {
  1614. new_state = BIT(EXTCON_EARJACK);
  1615. info->earkeypressed = true;
  1616. max77804k_muic_earjack_key_detect(info, adc);
  1617. }
  1618. break;
  1619. case ADC_EARJACK:
  1620. #if defined(CONFIG_ADC_ONESHOT)
  1621. max77804k_muic_set_adcmode(info, ADC_ALWAYS);
  1622. msleep(50);
  1623. #endif
  1624. new_state = BIT(EXTCON_EARJACK);
  1625. info->cable_name = EXTCON_EARJACK;
  1626. if ((info->cable_type == CABLE_TYPE_EARJACK_MUIC) && (info->earkeypressed)) {
  1627. info->earkeypressed = false;
  1628. max77804k_muic_earjack_key_detect(info, adc);
  1629. } else {
  1630. max77804k_muic_earjack_detect(info, true);
  1631. info->cable_type = CABLE_TYPE_EARJACK_MUIC;
  1632. }
  1633. break;
  1634. #endif
  1635. case ADC_CHARGING_CABLE:
  1636. #if defined(CONFIG_USBID_STANDARD_VER_01)
  1637. if (vbvolt) {
  1638. new_state = BIT(EXTCON_TA);
  1639. info->cable_name = EXTCON_UNDEFINED_CHARGER;
  1640. }
  1641. #else
  1642. new_state = BIT(EXTCON_CHARGING_CABLE);
  1643. info->cable_name = EXTCON_CHARGING_CABLE;
  1644. #endif
  1645. break;
  1646. case ADC_CEA936ATYPE1_CHG:
  1647. case ADC_CEA936ATYPE2_CHG:
  1648. #if defined(CONFIG_SEC_FACTORY)
  1649. new_state = BIT(EXTCON_JIG_UARTOFF);
  1650. info->cable_name = EXTCON_JIG_UARTOFF;
  1651. if (vbvolt) {
  1652. if (gInfo->otg_test) { /*add for DFMS */
  1653. new_state |= BIT(EXTCON_USB_HOST_5V);
  1654. info->cable_name = EXTCON_USB_HOST_5V;
  1655. }
  1656. }
  1657. break;
  1658. #endif
  1659. case ADC_OPEN:
  1660. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1661. info->curr_state = ADC_OPEN;
  1662. #endif
  1663. info->is_adc_open_prev = true;
  1664. switch (chgtyp) {
  1665. case CHGTYP_USB:
  1666. if (adc == ADC_CEA936ATYPE2_CHG) {
  1667. new_state = BIT(EXTCON_CEA936_CHG);
  1668. info->cable_name = EXTCON_CEA936_CHG;
  1669. } else {
  1670. new_state = BIT(EXTCON_USB);
  1671. info->cable_name = EXTCON_USB;
  1672. }
  1673. break;
  1674. case CHGTYP_DOWNSTREAM_PORT:
  1675. new_state = BIT(EXTCON_CHARGE_DOWNSTREAM);
  1676. info->cable_name = EXTCON_CHARGE_DOWNSTREAM;
  1677. break;
  1678. case CHGTYP_DEDICATED_CHGR:
  1679. case CHGTYP_500MA:
  1680. case CHGTYP_1A:
  1681. case CHGTYP_SPECIAL_CHGR:
  1682. new_state = BIT(EXTCON_TA);
  1683. info->cable_name = EXTCON_TA;
  1684. break;
  1685. default:
  1686. break;
  1687. }
  1688. break;
  1689. default:
  1690. #if defined(CONFIG_MUIC_SUPPORT_INCOMPATIBLE_CHARGER)
  1691. if (vbvolt) {
  1692. dev_warn(info->dev, "Incompatible adc=0x%x\n", adc);
  1693. new_state = BIT(EXTCON_INCOMPATIBLE);
  1694. info->cable_name = EXTCON_INCOMPATIBLE;
  1695. } else
  1696. #endif
  1697. {
  1698. if (is_lpm_mode && vbvolt) {
  1699. dev_warn(info->dev, "Incompatible adc=0x%x\n", adc);
  1700. new_state = BIT(EXTCON_INCOMPATIBLE);
  1701. info->cable_name = EXTCON_INCOMPATIBLE;
  1702. } else {
  1703. dev_warn(info->dev, "unsupported adc=0x%x\n", adc);
  1704. }
  1705. }
  1706. break;
  1707. }
  1708. if (!new_state) {
  1709. dev_warn(info->dev, "Fail to get cable type (adc=0x%x)\n", adc);
  1710. info->cable_name = EXTCON_NONE;
  1711. _detected(info, 0);
  1712. return -1;
  1713. }
  1714. #if defined(CONFIG_MUIC_SUPPORT_MMD_CTL)
  1715. if(!(info->mmd_en) && (new_state & BIT(EXTCON_SMARTDOCK))) {
  1716. #if defined(CONFIG_MUIC_SUPPORT_INCOMPATIBLE_CHARGER)
  1717. if (vbvolt) {
  1718. dev_warn(info->dev, "Incompatible adc=0x%x\n", adc);
  1719. new_state = BIT(EXTCON_INCOMPATIBLE);
  1720. } else
  1721. #endif
  1722. {
  1723. return 0;
  1724. }
  1725. }
  1726. #endif
  1727. __found_cable:
  1728. _detected(info, new_state);
  1729. max77804k_update_jig_state(info);
  1730. return 0;
  1731. }
  1732. static int max77804k_muic_handle_detach(struct max77804k_muic_info *info)
  1733. {
  1734. u8 ctrl2_val;
  1735. info->cable_name = EXTCON_NONE;
  1736. /* Workaround: irq doesn't occur after detaching mHL cable */
  1737. max77804k_write_reg(info->muic, MAX77804K_MUIC_REG_CTRL1,
  1738. MAX77804K_MUIC_CTRL1_BIN_0_000);
  1739. /* Enable Factory Accessory Detection State Machine */
  1740. max77804k_update_reg(info->muic,
  1741. MAX77804K_MUIC_REG_CTRL2, CTRL2_CPEn0_LOWPWD1,
  1742. CTRL2_CPEn_MASK | CTRL2_LOWPWD_MASK);
  1743. max77804k_read_reg(info->muic, MAX77804K_MUIC_REG_CTRL2,
  1744. &ctrl2_val);
  1745. dev_info(info->dev, "%s: CNTL2(0x%02x)\n", __func__, ctrl2_val);
  1746. #if defined(CONFIG_MUIC_DET_JACK)
  1747. if(info->cable_type == CABLE_TYPE_EARJACK_MUIC) {
  1748. dev_info(info->dev, "%s: EARJACK\n", __func__);
  1749. _detected(info, 0);
  1750. info->earkeypressed = false;
  1751. max77804k_muic_earjack_key_detect(info, 0);
  1752. info->cable_type = CABLE_TYPE_NONE_MUIC;
  1753. max77804k_muic_earjack_detect(info, false);
  1754. max77804k_update_reg(info->muic,
  1755. MAX77804K_MUIC_REG_CTRL1, 0, MICEN_MASK);
  1756. #if defined(CONFIG_ADC_ONESHOT)
  1757. max77804k_muic_set_adcmode(info, ADC_ONESHOT);
  1758. msleep(50);
  1759. #endif
  1760. return 0;
  1761. }
  1762. #endif
  1763. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1764. lanhub_otg_connected = false;
  1765. if ((info->pre_state == ADC_OPEN)
  1766. && (info->curr_state == ADC_LANHUB)) {
  1767. pr_err("Detach event only for ID pull , not 16 \n");
  1768. info->curr_state = ADC_OPEN;
  1769. info->edev->state &= ~(BIT(EXTCON_LANHUB_TA));
  1770. sec_otg_notify(HNOTIFY_ID_PULL);
  1771. }
  1772. #endif
  1773. #if defined CONFIG_ID_BYPASS_SBL
  1774. otg_attached = 0;
  1775. #endif
  1776. _detected(info, 0);
  1777. return 0;
  1778. }
  1779. static void max77804k_muic_detect_dev(struct max77804k_muic_info *info, int irq)
  1780. {
  1781. struct i2c_client *client = info->muic;
  1782. u8 status[2];
  1783. int intr = INT_ATTACH;
  1784. int ret;
  1785. u8 cntl1_val;
  1786. ret = max77804k_read_reg(client, MAX77804K_MUIC_REG_CTRL1, &cntl1_val);
  1787. dev_info(info->dev, "func:%s CONTROL1:%x\n", __func__, cntl1_val);
  1788. ret = max77804k_bulk_read(client, MAX77804K_MUIC_REG_STATUS1, 2, status);
  1789. dev_info(info->dev, "func:%s irq:%d ret:%d\n", __func__, irq, ret);
  1790. if (ret) {
  1791. dev_err(info->dev, "%s: fail to read muic reg(%d)\n", __func__,
  1792. ret);
  1793. return;
  1794. }
  1795. dev_info(info->dev, "%s: STATUS1:0x%x, 2:0x%x\n", __func__,
  1796. status[0], status[1]);
  1797. wake_lock_timeout(&info->muic_wake_lock, HZ * 2);
  1798. info->adc = status[0] & STATUS1_ADC_MASK;
  1799. info->chgtyp = status[1] & STATUS2_CHGTYP_MASK;
  1800. info->vbvolt = status[1] & STATUS2_VBVOLT_MASK;
  1801. info->adc1k = status[0] & STATUS1_ADC1K_MASK;
  1802. dev_info(info->dev,
  1803. "adc:0x%x, adc1k:0x%x, chgtyp:0x%x, vbvolt:%d adc open prev:%d\n",
  1804. info->adc, info->adc1k, info->chgtyp, info->vbvolt, info->is_adc_open_prev);
  1805. if ((!info->is_adc_open_prev) && (info->adc == ADC_OPEN) && (info->vbvolt)) {
  1806. /* added a 100ms delay after detach accessory */
  1807. msleep(100);
  1808. dev_info(info->dev, "wait 100ms for update status register\n");
  1809. ret = max77804k_bulk_read(client, MAX77804K_MUIC_REG_STATUS1, 2, status);
  1810. dev_info(info->dev, "func:%s irq:%d ret:%d\n", __func__, irq, ret);
  1811. info->adc = status[0] & STATUS1_ADC_MASK;
  1812. info->chgtyp = status[1] & STATUS2_CHGTYP_MASK;
  1813. info->vbvolt = status[1] & STATUS2_VBVOLT_MASK;
  1814. info->adc1k = status[0] & STATUS1_ADC1K_MASK;
  1815. dev_info(info->dev,
  1816. "adc:0x%x, adc1k:0x%x, chgtyp:0x%x, vbvolt:%d\n",
  1817. info->adc, info->adc1k, info->chgtyp, info->vbvolt);
  1818. }
  1819. if ((info->adc == ADC_OPEN) && (info->chgtyp == CHGTYP_NO_VOLTAGE))
  1820. {
  1821. intr = INT_DETACH;
  1822. }
  1823. if (intr == INT_ATTACH) {
  1824. dev_info(info->dev, "%s: ATTACHED/CHANGED\n", __func__);
  1825. max77804k_muic_handle_attach(info, status[0], status[1]);
  1826. } else if (intr == INT_DETACH) {
  1827. dev_info(info->dev, "%s: DETACHED\n", __func__);
  1828. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  1829. info->pre_state = ADC_OPEN;
  1830. info->lanhub_ta_status = false;
  1831. #endif
  1832. info->is_adc_open_prev = true;
  1833. max77804k_muic_handle_detach(info);
  1834. } else {
  1835. pr_info("%s:%s device filtered, nothing affect.\n", DEV_NAME,
  1836. __func__);
  1837. }
  1838. return;
  1839. }
  1840. static irqreturn_t max77804k_muic_irq(int irq, void *data)
  1841. {
  1842. struct max77804k_muic_info *info = data;
  1843. dev_info(info->dev, "%s: irq:%d\n", __func__, irq);
  1844. #if defined(CONFIG_MUIC_DET_JACK)
  1845. if (!(info->init_detect_done == INIT_DONE))
  1846. return IRQ_HANDLED;
  1847. #endif
  1848. mutex_lock(&info->mutex);
  1849. max77804k_muic_detect_dev(info, irq);
  1850. mutex_unlock(&info->mutex);
  1851. return IRQ_HANDLED;
  1852. }
  1853. #define REQUEST_IRQ(_irq, _name) \
  1854. do { \
  1855. ret = request_threaded_irq(_irq, NULL, max77804k_muic_irq, \
  1856. 0, _name, info); \
  1857. if (ret < 0) \
  1858. dev_err(info->dev, "Failed to request IRQ #%d: %d\n", \
  1859. _irq, ret); \
  1860. } while (0)
  1861. static int max77804k_muic_irq_init(struct max77804k_muic_info *info)
  1862. {
  1863. int ret, count;
  1864. u8 val;
  1865. dev_info(info->dev, "func:%s\n", __func__);
  1866. /* dev_info(info->dev, "%s: system_rev=%x\n", __func__, system_rev); */
  1867. /* INTMASK1 3:ADC1K 2:ADCErr 1:ADCLow 0:ADC */
  1868. /* INTMASK2 0:Chgtype */
  1869. max77804k_write_reg(info->muic, MAX77804K_MUIC_REG_INTMASK1, 0x09);
  1870. max77804k_write_reg(info->muic, MAX77804K_MUIC_REG_INTMASK2, 0x11);
  1871. max77804k_write_reg(info->muic, MAX77804K_MUIC_REG_INTMASK3, 0x00);
  1872. REQUEST_IRQ(info->irq_adc, "muic-adc");
  1873. REQUEST_IRQ(info->irq_chgtype, "muic-chgtype");
  1874. REQUEST_IRQ(info->irq_vbvolt, "muic-vbvolt");
  1875. dev_info(info->dev, "adc:%d chgtype:%d vbvolt:%d",
  1876. info->irq_adc, info->irq_chgtype, info->irq_vbvolt);
  1877. for (count = 1; count < 9 ; count ++) {
  1878. max77804k_read_reg(info->muic, count, &val);
  1879. dev_info(info->dev, "%s: reg=%x, val=%x\n", __func__,
  1880. count, val);
  1881. }
  1882. return 0;
  1883. }
  1884. static void max77804k_muic_init_detect(struct work_struct *work)
  1885. {
  1886. struct max77804k_muic_info *info =
  1887. container_of(work, struct max77804k_muic_info, init_work.work);
  1888. dev_info(info->dev, "func:%s\n", __func__);
  1889. mutex_lock(&info->mutex);
  1890. #if defined(CONFIG_MUIC_DET_JACK)
  1891. info->init_detect_done = INIT_START;
  1892. #endif
  1893. info->edev->state = 0;
  1894. max77804k_muic_detect_dev(info, -1);
  1895. #if defined(CONFIG_MUIC_DET_JACK)
  1896. info->init_detect_done = INIT_DONE;
  1897. #endif
  1898. mutex_unlock(&info->mutex);
  1899. }
  1900. void max77804k_update_jig_state(struct max77804k_muic_info *info)
  1901. {
  1902. struct i2c_client *client = info->muic;
  1903. //struct max77804k_muic_data *mdata = info->muic_data;
  1904. u8 reg_data, adc;
  1905. int ret, jig_state;
  1906. ret = max77804k_read_reg(client, MAX77804K_MUIC_REG_STATUS1, &reg_data);
  1907. if (ret) {
  1908. dev_err(info->dev, "%s: fail to read muic reg(%d)\n",
  1909. __func__, ret);
  1910. return;
  1911. }
  1912. adc = reg_data & STATUS1_ADC_MASK;
  1913. switch (adc) {
  1914. case ADC_JIG_UART_OFF:
  1915. case ADC_JIG_USB_OFF:
  1916. case ADC_JIG_USB_ON:
  1917. jig_state = true;
  1918. break;
  1919. default:
  1920. jig_state = false;
  1921. break;
  1922. }
  1923. // mdata->jig_state(jig_state);
  1924. max77804k_muic_uart_uevent(jig_state);
  1925. }
  1926. #ifdef CONFIG_USB_HOST_NOTIFY
  1927. struct booster_data sec_booster = {
  1928. .name = "max77804k",
  1929. .boost = muic_otg_control,
  1930. };
  1931. #endif
  1932. static int __init get_lpm_mode(char *str)
  1933. {
  1934. if (!strncasecmp(str, "lpm", 3)) {
  1935. pr_info("%s:lpm mode\n", __func__);
  1936. is_lpm_mode = true;
  1937. } else {
  1938. pr_info("%s:not lpm mode\n", __func__);
  1939. is_lpm_mode = false;
  1940. }
  1941. return 0;
  1942. }
  1943. __setup("androidboot.baseband=", get_lpm_mode);
  1944. static int __devinit max77804k_muic_probe(struct platform_device *pdev)
  1945. {
  1946. struct max77804k_dev *max77804k = dev_get_drvdata(pdev->dev.parent);
  1947. struct max77804k_platform_data *pdata = dev_get_platdata(max77804k->dev);
  1948. struct max77804k_muic_info *info;
  1949. #if defined(CONFIG_MUIC_DET_JACK)
  1950. struct input_dev *input;
  1951. struct class *audio;
  1952. struct device *earjack;
  1953. #endif
  1954. int ret;
  1955. u8 cdetctrl1;
  1956. pr_info("func:%s\n", __func__);
  1957. info = kzalloc(sizeof(struct max77804k_muic_info), GFP_KERNEL);
  1958. if (!info) {
  1959. dev_err(&pdev->dev, "%s: failed to allocate info\n", __func__);
  1960. ret = -ENOMEM;
  1961. goto err_return;
  1962. }
  1963. #if defined(CONFIG_MUIC_DET_JACK)
  1964. input = input_allocate_device();
  1965. if (!input) {
  1966. dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
  1967. ret = -ENOMEM;
  1968. goto err_kfree;
  1969. }
  1970. #endif
  1971. info->dev = &pdev->dev;
  1972. info->max77804k = max77804k;
  1973. info->muic = max77804k->muic;
  1974. #if defined(CONFIG_MUIC_DET_JACK)
  1975. info->input = input;
  1976. #endif
  1977. info->irq_adc = max77804k->irq_base + MAX77804K_MUIC_IRQ_INT1_ADC;
  1978. info->irq_vbvolt = max77804k->irq_base + MAX77804K_MUIC_IRQ_INT2_VBVOLT;
  1979. info->irq_chgtype = max77804k->irq_base + MAX77804K_MUIC_IRQ_INT2_CHGTYP;
  1980. info->muic_data = pdata->muic_data;
  1981. info->muic_data->uart_sel = PATH_UART_AP;
  1982. info->is_adc_open_prev = true;
  1983. #if !defined(CONFIG_MUIC_MAX77804K_SUPPORT_CAR_DOCK)
  1984. info->is_factory_start = false;
  1985. #endif /* !CONFIG_MUIC_MZX77804k_SUPPORT_CAR_DOCK */
  1986. info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
  1987. GFP_KERNEL);
  1988. if (!info->edev) {
  1989. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  1990. ret = -ENOMEM;
  1991. goto err_input;
  1992. }
  1993. info->edev->name=EXTCON_DEV_NAME;
  1994. info->edev->supported_cable = extcon_cable_name;
  1995. ret = extcon_dev_register(info->edev, NULL);
  1996. if (ret) {
  1997. dev_err(&pdev->dev, "failed to register extcon device\n");
  1998. goto err_input;
  1999. }
  2000. wake_lock_init(&info->muic_wake_lock, WAKE_LOCK_SUSPEND,
  2001. "muic wake lock");
  2002. info->cable_name = EXTCON_NONE;
  2003. #if defined (CONFIG_MUIC_MAX77804K_SUPPORT_LANHUB)
  2004. info->lanhub_ta_status = false;
  2005. info->pre_state = ADC_OPEN;
  2006. info->curr_state = MAX77804K_MUIC_NONE;
  2007. #endif
  2008. info->muic_data->usb_sel = PATH_USB_AP;
  2009. #if defined(CONFIG_MUIC_DET_JACK)
  2010. info->eardetected = false;
  2011. info->earkeypressed = false;
  2012. info->init_detect_done = NOT_INIT;
  2013. info->old_keycode = 0;
  2014. info->keycode = 0;
  2015. #endif
  2016. gInfo = info;
  2017. platform_set_drvdata(pdev, info);
  2018. dev_info(info->dev, "adc:%d chgtype:%d\n",
  2019. info->irq_adc, info->irq_chgtype);
  2020. #if defined(CONFIG_MUIC_DET_JACK)
  2021. input->name = pdev->name;
  2022. input->phys = "deskdock-key/input0";
  2023. input->dev.parent = &pdev->dev;
  2024. input->id.bustype = BUS_HOST;
  2025. input->id.vendor = 0x0001;
  2026. input->id.product = 0x0001;
  2027. input->id.version = 0x0001;
  2028. /* Enable auto repeat feature of Linux input subsystem */
  2029. __set_bit(EV_REP, input->evbit);
  2030. dev_info(info->dev, "input->name:%s\n", input->name);
  2031. input_set_capability(input, EV_KEY, KEY_MEDIA);
  2032. input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
  2033. input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
  2034. input_set_capability(input, EV_KEY, KEY_PLAYPAUSE);
  2035. input_set_capability(input, EV_KEY, KEY_PREVIOUSSONG);
  2036. input_set_capability(input, EV_KEY, KEY_NEXTSONG);
  2037. ret = input_register_device(input);
  2038. if (ret) {
  2039. dev_err(info->dev, "%s: Unable to register input device, "
  2040. "error: %d\n", __func__, ret);
  2041. goto err_input;
  2042. }
  2043. #endif
  2044. #if defined(CONFIG_SWITCH_DUAL_MODEM) || defined(CONFIG_MUIC_RUSTPROOF_UART)
  2045. switch_sel &= 0xf;
  2046. if ((switch_sel & MAX77804K_SWITCH_SEL_1st_BIT_USB) == 0x1)
  2047. info->muic_data->usb_sel = PATH_USB_AP;
  2048. else
  2049. info->muic_data->usb_sel = PATH_USB_CP;
  2050. if ((switch_sel & MAX77804K_SWITCH_SEL_2nd_BIT_UART) == 0x1 << 1)
  2051. info->muic_data->uart_sel = PATH_UART_AP;
  2052. else
  2053. info->muic_data->uart_sel = PATH_UART_CP;
  2054. pr_err("%s: switch_sel: %x\n", __func__, switch_sel);
  2055. #endif
  2056. /* create sysfs group */
  2057. ret = sysfs_create_group(&switch_dev->kobj, &max77804k_muic_group);
  2058. pr_info("dev_set_drvdata:%s\n", __func__);
  2059. dev_set_drvdata(switch_dev, info);
  2060. if (ret) {
  2061. dev_err(&pdev->dev,
  2062. "failed to create max77804k muic attribute group\n");
  2063. goto err_input;
  2064. }
  2065. mutex_init(&info->mutex);
  2066. /* Set ADC debounce time: 25ms */
  2067. max77804k_muic_set_adcdbset(info, 2);
  2068. /* Set DCDTmr to 2sec */
  2069. max77804k_read_reg(info->max77804k->muic,
  2070. MAX77804K_MUIC_REG_CDETCTRL1, &cdetctrl1);
  2071. /* Get adcmode value */
  2072. max77804k_read_reg(info->max77804k->muic,
  2073. MAX77804K_MUIC_REG_CTRL4, &info->adc_mode);
  2074. info->adc_mode = info->adc_mode >> CTRL4_ADCMODE_SHIFT;
  2075. pr_info("%s: ADCMODE(0x%x)\n", __func__, info->adc_mode);
  2076. cdetctrl1 &= ~(1 << 5);
  2077. max77804k_write_reg(info->max77804k->muic,
  2078. MAX77804K_MUIC_REG_CDETCTRL1, cdetctrl1);
  2079. pr_info("%s: CDETCTRL1(0x%02x)\n", __func__, cdetctrl1);
  2080. #if defined(CONFIG_MUIC_DET_JACK)
  2081. ret = switch_dev_register(&switch_earjack);
  2082. if (ret < 0) {
  2083. pr_info("%s : Failed to register switch device\n", __func__);
  2084. goto err_switch_dev_register;
  2085. }
  2086. ret = switch_dev_register(&switch_earjackkey);
  2087. if (ret < 0) {
  2088. pr_info("%s : Failed to register switch device\n", __func__);
  2089. goto err_switch_dev_register;
  2090. }
  2091. audio = class_create(THIS_MODULE, "audio");
  2092. if (IS_ERR(audio)) {
  2093. pr_err("Failed to create class(audio)!\n");
  2094. goto err_switch_dev_register;
  2095. }
  2096. earjack = device_create(audio, NULL, 0, NULL, "earjack");
  2097. if (IS_ERR(earjack)) {
  2098. pr_err("Failed to create device(earjack)!\n");
  2099. goto err_switch_dev_register;
  2100. }
  2101. ret = device_create_file(earjack, &dev_attr_key_state);
  2102. if (ret) {
  2103. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  2104. dev_attr_key_state.attr.name);
  2105. goto err_switch_dev_register;
  2106. }
  2107. ret = device_create_file(earjack, &dev_attr_state);
  2108. if (ret) {
  2109. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  2110. dev_attr_state.attr.name);
  2111. goto err_switch_dev_register;
  2112. }
  2113. dev_set_drvdata(earjack, info);
  2114. #endif
  2115. ret = switch_dev_register(&switch_uart3);
  2116. if (ret < 0) {
  2117. pr_err("%s : Failed to register switch_uart3 device\n", __func__);
  2118. goto err_switch_uart3_dev_register;
  2119. }
  2120. ret = max77804k_muic_irq_init(info);
  2121. if (ret < 0) {
  2122. dev_err(&pdev->dev, "Failed to initialize MUIC irq:%d\n", ret);
  2123. goto fail;
  2124. }
  2125. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  2126. if ( (switch_sel&0x08) == 0x00 ) {
  2127. /* MAKE PATH OPEN */
  2128. max77804k_muic_set_path(info, PATH_OPEN);
  2129. pr_info("%s: uart en bit : %d\n", __func__, (switch_sel&0x08));
  2130. pr_info("%s: MUIC path is OPEN from now.\n", __func__);
  2131. } else {
  2132. pr_info("%s: uart enabled : %d\n", __func__, (switch_sel&0x08));
  2133. info->uart_en = true;
  2134. }
  2135. #endif
  2136. /* initial cable detection */
  2137. INIT_DELAYED_WORK(&info->init_work, max77804k_muic_init_detect);
  2138. schedule_delayed_work(&info->init_work, msecs_to_jiffies(3000));
  2139. #if defined(CONFIG_MUIC_DET_JACK)
  2140. INIT_WORK(&info->earjack_work, max77804k_muic_earjack_detect_workfunc);
  2141. INIT_WORK(&info->earjackkey_work, max77804k_muic_earjack_key_detect_workfunc);
  2142. #endif
  2143. /* init jig state */
  2144. max77804k_update_jig_state(info);
  2145. #ifdef CONFIG_USB_HOST_NOTIFY
  2146. sec_otg_register_booster(&sec_booster);
  2147. #endif
  2148. return 0;
  2149. fail:
  2150. if (info->irq_adc)
  2151. free_irq(info->irq_adc, NULL);
  2152. if (info->irq_chgtype)
  2153. free_irq(info->irq_chgtype, NULL);
  2154. if (info->irq_vbvolt)
  2155. free_irq(info->irq_vbvolt, NULL);
  2156. mutex_destroy(&info->mutex);
  2157. #if defined(CONFIG_MUIC_DET_JACK)
  2158. err_switch_dev_register:
  2159. switch_dev_unregister(&switch_earjack);
  2160. switch_dev_unregister(&switch_earjackkey);
  2161. #endif
  2162. err_switch_uart3_dev_register:
  2163. switch_dev_unregister(&switch_uart3);
  2164. err_input:
  2165. platform_set_drvdata(pdev, NULL);
  2166. wake_lock_destroy(&info->muic_wake_lock);
  2167. #if !defined(CONFIG_MUIC_DET_JACK)
  2168. kfree(info);
  2169. #else
  2170. err_kfree:
  2171. kfree(info);
  2172. #endif
  2173. err_return:
  2174. return ret;
  2175. }
  2176. #if !defined(CONFIG_SEC_FACTORY)
  2177. static int max77804k_suspend(struct device *dev)
  2178. {
  2179. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  2180. #if defined(CONFIG_CHARGER_SMB1357)
  2181. if (info) {
  2182. if (info->path == PATH_USB_CP)
  2183. return 0;
  2184. }
  2185. #endif
  2186. if (info) {
  2187. #if defined (CONFIG_MUIC_DET_JACK)
  2188. if (!info->eardetected)
  2189. #endif
  2190. max77804k_muic_set_path(info, PATH_OPEN);
  2191. } else {
  2192. pr_err("%s, dev_get_drvdata fail\n", __func__);
  2193. }
  2194. return 0;
  2195. }
  2196. static int max77804k_resume(struct device *dev)
  2197. {
  2198. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  2199. dev_info(info->dev, "func:%s\n", __func__);
  2200. #if defined(CONFIG_CHARGER_SMB1357)
  2201. if (info) {
  2202. if (info->path == PATH_USB_CP)
  2203. return 0;
  2204. }
  2205. #endif
  2206. if (info) {
  2207. if (info->edev->state)
  2208. max77804k_muic_set_path(info, info->path);
  2209. else
  2210. pr_info("%s, extcon cable none\n", __func__);
  2211. } else {
  2212. pr_err("%s, dev_get_drvdata fail\n", __func__);
  2213. }
  2214. return 0;
  2215. }
  2216. static const struct dev_pm_ops max77804k_dev_pm_ops = {
  2217. .suspend = max77804k_suspend,
  2218. .resume = max77804k_resume,
  2219. };
  2220. #endif
  2221. static int __devexit max77804k_muic_remove(struct platform_device *pdev)
  2222. {
  2223. struct max77804k_muic_info *info = platform_get_drvdata(pdev);
  2224. sysfs_remove_group(&switch_dev->kobj, &max77804k_muic_group);
  2225. if (info) {
  2226. dev_info(info->dev, "func:%s\n", __func__);
  2227. cancel_delayed_work(&info->init_work);
  2228. free_irq(info->irq_adc, info);
  2229. free_irq(info->irq_chgtype, info);
  2230. free_irq(info->irq_vbvolt, info);
  2231. wake_lock_destroy(&info->muic_wake_lock);
  2232. mutex_destroy(&info->mutex);
  2233. kfree(info);
  2234. }
  2235. return 0;
  2236. }
  2237. void max77804k_muic_shutdown(struct device *dev)
  2238. {
  2239. struct max77804k_muic_info *info = dev_get_drvdata(dev);
  2240. int ret;
  2241. u8 val;
  2242. dev_info(info->dev, "func:%s\n", __func__);
  2243. if (!info->muic) {
  2244. dev_err(info->dev, "%s: no muic i2c client\n", __func__);
  2245. return;
  2246. }
  2247. dev_info(info->dev, "%s: JIGSet: auto detection\n", __func__);
  2248. val = (0 << CTRL3_JIGSET_SHIFT) | (0 << CTRL3_BOOTSET_SHIFT);
  2249. ret = max77804k_update_reg(info->muic, MAX77804K_MUIC_REG_CTRL3, val,
  2250. CTRL3_JIGSET_MASK | CTRL3_BOOTSET_MASK);
  2251. if (ret < 0) {
  2252. dev_err(info->dev, "%s: fail to update reg\n", __func__);
  2253. return;
  2254. }
  2255. }
  2256. static struct platform_driver max77804k_muic_driver = {
  2257. .driver = {
  2258. .name = DEV_NAME,
  2259. .owner = THIS_MODULE,
  2260. #if !defined(CONFIG_SEC_FACTORY)
  2261. .pm = &max77804k_dev_pm_ops,
  2262. #endif
  2263. .shutdown = max77804k_muic_shutdown,
  2264. },
  2265. .probe = max77804k_muic_probe,
  2266. .remove = __devexit_p(max77804k_muic_remove),
  2267. };
  2268. static int __init max77804k_muic_init(void)
  2269. {
  2270. pr_info("func:%s\n", __func__);
  2271. return platform_driver_register(&max77804k_muic_driver);
  2272. }
  2273. module_init(max77804k_muic_init);
  2274. static void __exit max77804k_muic_exit(void)
  2275. {
  2276. pr_info("func:%s\n", __func__);
  2277. platform_driver_unregister(&max77804k_muic_driver);
  2278. }
  2279. module_exit(max77804k_muic_exit);
  2280. MODULE_DESCRIPTION("Maxim MAX77804K MUIC driver");
  2281. MODULE_AUTHOR("<sukdong.kim@samsung.com>");
  2282. MODULE_LICENSE("GPL");