sm5502.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. /*
  2. * driver/misc/sm5502.c - SM5502 micro USB switch device driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics
  5. * Jeongrae Kim <jryu.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. */
  22. /****************************** Revision History ******************************************
  23. *CH# Product author Description Date
  24. *-----------------------------------------------------------------------------------------
  25. *01 Millet & nc.chaudhary Modified the ISR for AT+OTGGTEST=0,0 30-Jan-2014
  26. * Matisse all AT Command failure issue during SMD
  27. * variants test.
  28. *02 Millet & Matise nc.chaudhary Enabling the support for Cardock in 02-Feb-2014
  29. * All Variants Factory Images only.
  30. *03 Millet & Matise nc.chaudhary Modified the ISR for the VBUS-UART 02-Feb-2014
  31. * All Variants cable not working properly.
  32. *04 MSM8x26 & MSM8926 nc.chaudhary Modified the ISR for OTG SMD Test fail 08-Feb-2014
  33. * All variants when checking OTG with UART AT Cmds.
  34. *05 MSM8x26 & MSM8926 nc.chaudhary Modified the cable type in the deskdock 12-Feb-2014
  35. * All variants callback depending on VBUS presence.
  36. *06 MSM8x26 & MSM8926 nc.chaudhary Modified ISR to report VBUS change 19-Feb-2014
  37. * All variants when desdock is connected.
  38. *07 MSM8x26 & MSM8926 nc.chaudhary Implemented the LANHUB+TA case 28-Feb-2014
  39. * All variants
  40. ******************************************************************************************
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/err.h>
  45. #include <linux/i2c.h>
  46. #include <linux/i2c/sm5502.h>
  47. #include <linux/irq.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/slab.h>
  51. #include <linux/gpio.h>
  52. #include <linux/delay.h>
  53. #include <linux/regulator/consumer.h>
  54. #include <linux/mfd/pmic8058.h>
  55. #include <linux/input.h>
  56. #include <linux/switch.h>
  57. #if defined (CONFIG_OF)
  58. #include <linux/of_device.h>
  59. #include <linux/of_gpio.h>
  60. #endif
  61. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  62. #include <linux/sec_param.h>
  63. #endif
  64. #ifdef CONFIG_USB_HOST_NOTIFY
  65. #include <linux/host_notify.h>
  66. #endif
  67. /* spmi control */
  68. extern int spmi_ext_register_writel_extra(u8 sid, u16 ad, u8 *buf, int len);
  69. extern int spmi_ext_register_readl_extra(u8 sid, u16 ad, u8 *buf, int len);
  70. extern int system_rev;
  71. #define INT_MASK1 0x5C
  72. #define INT_MASK2 0x20
  73. /* DEVICE ID */
  74. #define SM5502_DEV_ID 0x0A
  75. #define SM5502_DEV_ID_REV 0x12
  76. /* SM5502 I2C registers */
  77. #define REG_DEVICE_ID 0x01
  78. #define REG_CONTROL 0x02
  79. #define REG_INT1 0x03
  80. #define REG_INT2 0x04
  81. #define REG_INT_MASK1 0x05
  82. #define REG_INT_MASK2 0x06
  83. #define REG_ADC 0x07
  84. #define REG_TIMING_SET1 0x08
  85. #define REG_TIMING_SET2 0x09
  86. #define REG_DEVICE_TYPE1 0x0a
  87. #define REG_DEVICE_TYPE2 0x0b
  88. #define REG_BUTTON1 0x0c
  89. #define REG_BUTTON2 0x0d
  90. #define REG_MANUAL_SW1 0x13
  91. #define REG_MANUAL_SW2 0x14
  92. #define REG_DEVICE_TYPE3 0x15
  93. #define REG_RESET 0x1B
  94. #define REG_TIMER_SET 0x20
  95. #define REG_VBUSINVALID 0x1D
  96. #define REG_OCP_SET 0x22
  97. #define REG_CHGPUMP_SET 0x3A
  98. #define REG_CARKIT_STATUS 0x0E
  99. #define DATA_NONE 0x00
  100. /* Control */
  101. #define CON_SWITCH_OPEN (1 << 4)
  102. #define CON_RAW_DATA (1 << 3)
  103. #define CON_MANUAL_SW (1 << 2)
  104. #define CON_WAIT (1 << 1)
  105. #define CON_INT_MASK (1 << 0)
  106. #define CON_MASK (CON_SWITCH_OPEN | CON_RAW_DATA | \
  107. CON_MANUAL_SW | CON_WAIT)
  108. /* Device Type 1 */
  109. #define DEV_USB_OTG (1 << 7)
  110. #define DEV_DEDICATED_CHG (1 << 6)
  111. #define DEV_USB_CHG (1 << 5)
  112. #define DEV_CAR_KIT (1 << 4)
  113. #define DEV_UART (1 << 3)
  114. #define DEV_USB (1 << 2)
  115. #define DEV_AUDIO_2 (1 << 1)
  116. #define DEV_AUDIO_1 (1 << 0)
  117. #define DEV_T1_USB_MASK (DEV_USB_OTG | DEV_USB_CHG | DEV_USB)
  118. #define DEV_T1_UART_MASK (DEV_UART)
  119. #define DEV_T1_CHARGER_MASK (DEV_DEDICATED_CHG | DEV_CAR_KIT)
  120. #define DEV_CARKIT_CHARGER1_MASK (1 << 1)
  121. #define MANSW1_OPEN_RUSTPROOF ((0x0 << 5)| (0x3 << 2) |(1 << 0))
  122. /* Device Type 2 */
  123. #define DEV_LANHUB (1 << 9)
  124. #define DEV_AUDIO_DOCK (1 << 8)
  125. #define DEV_SMARTDOCK (1 << 7)
  126. #define DEV_AV (1 << 6)
  127. #define DEV_TTY (1 << 5)
  128. #define DEV_PPD (1 << 4)
  129. #define DEV_JIG_UART_OFF (1 << 3)
  130. #define DEV_JIG_UART_ON (1 << 2)
  131. #define DEV_JIG_USB_OFF (1 << 1)
  132. #define DEV_JIG_USB_ON (1 << 0)
  133. #define DEV_T2_USB_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON)
  134. #define DEV_T2_UART_MASK (DEV_JIG_UART_OFF)
  135. #define DEV_T2_JIG_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
  136. DEV_JIG_UART_OFF)
  137. #define DEV_T2_JIG_ALL_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
  138. DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
  139. /* Device Type 3 */
  140. #define DEV_MHL (1 << 0)
  141. #define DEV_VBUSIN_VALID (1 << 1)
  142. #define DEV_NON_STANDARD (1 << 2)
  143. #define DEV_AV_VBUS (1 << 4)
  144. #define DEV_U200_CHARGER (1 << 6)
  145. #define DEV_T3_CHARGER_MASK (DEV_U200_CHARGER)
  146. /*
  147. * Manual Switch
  148. * D- [7:5] / D+ [4:2]
  149. * 000: Open all / 001: USB / 010: AUDIO / 011: UART / 100: V_AUDIO
  150. */
  151. #define SW_VAUDIO ((4 << 5) | (4 << 2) | (1 << 1) | (1 << 0))
  152. #define SW_UART ((3 << 5) | (3 << 2))
  153. #define SW_AUDIO ((2 << 5) | (2 << 2) | (1 << 0))
  154. #define SW_DHOST ((1 << 5) | (1 << 2) | (1 << 0))
  155. #define SW_AUTO ((0 << 5) | (0 << 2))
  156. #define SW_USB_OPEN (1 << 0)
  157. #define SW_ALL_OPEN (0)
  158. #define SW_ALL_OPEN_WITH_VBUS ((0 << 5) | (0 << 2) | (1 << 0))
  159. /* Interrupt 1 */
  160. #define INT_OXP_DISABLE (1 << 7)
  161. #define INT_OCP_ENABLE (1 << 6)
  162. #define INT_OVP_ENABLE (1 << 5)
  163. #define INT_LONG_KEY_RELEASE (1 << 4)
  164. #define INT_LONG_KEY_PRESS (1 << 3)
  165. #define INT_KEY_PRESS (1 << 2)
  166. #define INT_DETACH (1 << 1)
  167. #define INT_ATTACH (1 << 0)
  168. /* Interrupt 2 */
  169. #define INT_VBUSOUT_ON (1 << 7)
  170. #define INT_OTP_ENABLE (1 << 6)
  171. #define INT_CONNECT (1 << 5)
  172. #define INT_STUCK_KEY_RCV (1 << 4)
  173. #define INT_STUCK_KEY (1 << 3)
  174. #define INT_ADC_CHANGE (1 << 2)
  175. #define INT_RESERVED_ATTACH (1 << 1)
  176. #define INT_VBUSOUT_OFF (1 << 0)
  177. /* ADC VALUE */
  178. #define ADC_OTG 0x00
  179. #define ADC_MHL 0x01
  180. #define ADC_SMART_DOCK 0x10
  181. #define ADC_AUDIO_DOCK 0x12
  182. #define ADC_JIG_USB_OFF 0x18
  183. #define ADC_JIG_USB_ON 0x19
  184. #define ADC_DESKDOCK 0x1a
  185. #define ADC_JIG_UART_OFF 0x1c
  186. #define ADC_JIG_UART_ON 0x1d
  187. #define ADC_CARDOCK 0x1d
  188. #define ADC_OPEN 0x1f
  189. #define ADC_LANHUB 0x13
  190. #define ADC_CHARGING_CABLE 0x14
  191. int uart_sm5502_connecting;
  192. EXPORT_SYMBOL(uart_sm5502_connecting);
  193. int detached_sm5502_status;
  194. EXPORT_SYMBOL(detached_sm5502_status);
  195. static int jig_state;
  196. //function added for enabling torch through AT command
  197. #if defined(CONFIG_TORCH_FIX)
  198. int factory_uart_connected(void);
  199. #endif
  200. struct sm5502_usbsw {
  201. struct i2c_client *client;
  202. struct sm5502_platform_data *pdata;
  203. int dev1;
  204. int dev2;
  205. int dev3;
  206. int mansw;
  207. int vbus;
  208. int dock_attached;
  209. int dev_id;
  210. int carkit_dev;
  211. struct delayed_work init_work;
  212. struct mutex mutex;
  213. int adc;
  214. /* muic current attached device */
  215. enum muic_attached_dev attached_dev;
  216. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  217. unsigned int previous_dock;
  218. unsigned int lanhub_ta_status;
  219. #endif
  220. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  221. bool is_rustproof;
  222. #endif
  223. };
  224. static struct sm5502_usbsw *local_usbsw;
  225. static int sm5502_attach_dev(struct sm5502_usbsw *usbsw);
  226. static int sm5502_detach_dev(struct sm5502_usbsw *usbsw);
  227. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  228. bool lanhub_ta_case = false;
  229. /* RAW DATA Detection*/
  230. static void sm5502_enable_rawdataInterrupts(struct sm5502_usbsw *usbsw)
  231. {
  232. struct i2c_client *client = usbsw->client;
  233. u8 value, ret;
  234. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  235. value &= 0xF7; /*Control Register Bit 4 set to 0 to enable RAW Data INTR*/
  236. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, value);
  237. pr_info("%s:set CONTROL value to 0x%x\n", __func__, value);
  238. if (ret < 0)
  239. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  240. }
  241. static void sm5502_disable_rawdataInterrupts(struct sm5502_usbsw *usbsw)
  242. {
  243. struct i2c_client *client = local_usbsw->client;
  244. int value, ret;
  245. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  246. value |= 0x08; /*Control Register Bit 4 set to 0 to enable RAW Data INTR*/
  247. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, value);
  248. pr_info("%s:set CONTROL value to 0x%x \n", __func__, value);
  249. if (ret < 0)
  250. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  251. }
  252. #endif
  253. static int sm5502_write_reg(struct i2c_client *client, int reg, int val)
  254. {
  255. int ret;
  256. ret = i2c_smbus_write_byte_data(client, reg, val);
  257. if (ret < 0)
  258. {
  259. dev_err(&client->dev,
  260. "%s, i2c write error %d\n",__func__, ret);
  261. }
  262. return ret;
  263. }
  264. static int sm5502_read_reg(struct i2c_client *client, int reg)
  265. {
  266. int ret;
  267. ret = i2c_smbus_read_byte_data(client, reg);
  268. if (ret < 0)
  269. {
  270. dev_err(&client->dev,
  271. "%s, i2c read error %d\n",__func__, ret);
  272. }
  273. return ret;
  274. }
  275. static void sm5502_dock_control(struct sm5502_usbsw *usbsw,
  276. int dock_type, int state, int path)
  277. {
  278. struct i2c_client *client = usbsw->client;
  279. struct sm5502_platform_data *pdata = usbsw->pdata;
  280. int ret;
  281. if (state) {
  282. usbsw->mansw = path;
  283. pdata->callback(dock_type, state);
  284. if (dock_type == CABLE_TYPE_DESK_DOCK_NO_VB)
  285. switch_set_state(&switch_dock, state);
  286. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, path);
  287. if (ret < 0)
  288. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  289. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  290. if (ret < 0)
  291. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  292. else {
  293. ret = i2c_smbus_write_byte_data(client,
  294. REG_CONTROL, ret & ~CON_MANUAL_SW);
  295. }
  296. if (ret < 0)
  297. dev_err(&client->dev, "%s: err %x\n", __func__, ret);
  298. } else {
  299. pdata->callback(dock_type, state);
  300. if (dock_type == CABLE_TYPE_DESK_DOCK_NO_VB)
  301. switch_set_state(&switch_dock, state);
  302. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  303. if (ret < 0)
  304. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  305. ret = i2c_smbus_write_byte_data(client, REG_CONTROL,
  306. ret | CON_MANUAL_SW | CON_RAW_DATA);
  307. if (ret < 0)
  308. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  309. }
  310. }
  311. static void sm5502_reg_init(struct sm5502_usbsw *usbsw)
  312. {
  313. struct i2c_client *client = usbsw->client;
  314. unsigned int ctrl = CON_MASK;
  315. int ret;
  316. pr_info("sm5502_reg_init is called\n");
  317. usbsw->dev_id = i2c_smbus_read_byte_data(client, REG_DEVICE_ID);
  318. local_usbsw->dev_id = usbsw->dev_id;
  319. if (usbsw->dev_id < 0)
  320. dev_err(&client->dev, "%s: err %d\n", __func__, usbsw->dev_id);
  321. dev_info(&client->dev, " sm5502_reg_init dev ID: 0x%x\n",
  322. usbsw->dev_id);
  323. ret = i2c_smbus_write_byte_data(client, REG_INT_MASK1, INT_MASK1);
  324. if (ret < 0)
  325. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  326. ret = i2c_smbus_write_byte_data(client, REG_INT_MASK2, INT_MASK2);
  327. if (ret < 0)
  328. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  329. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ctrl);
  330. if (ret < 0)
  331. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  332. /*set timing1 to 300ms */
  333. ret = i2c_smbus_write_byte_data(client, REG_TIMING_SET1, 0x04);
  334. if (ret < 0)
  335. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  336. /*Manual SW2 bit2 : JIG_ON '1' */
  337. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x04);
  338. if (ret < 0)
  339. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  340. /*Disabling the DCD Timer to ensure a high cable detection rate*/
  341. /*reset value is 0x0E, clearing the bit 2 to disable DCD timeout*/
  342. ret = i2c_smbus_write_byte_data(client, REG_TIMER_SET, 0x0A);
  343. if (ret < 0)
  344. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  345. }
  346. static ssize_t sm5502_muic_show_attached_dev(struct device *dev,
  347. struct device_attribute *attr,
  348. char *buf)
  349. {
  350. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  351. pr_info("[MUIC] SM5502:%s attached_dev:%d\n",
  352. __func__,usbsw->attached_dev);
  353. switch(usbsw->attached_dev) {
  354. case ATTACHED_DEV_NONE_MUIC:
  355. return sprintf(buf, "No VPS\n");
  356. case ATTACHED_DEV_USB_MUIC:
  357. return sprintf(buf, "USB\n");
  358. case ATTACHED_DEV_CDP_MUIC:
  359. return sprintf(buf, "CDP\n");
  360. case ATTACHED_DEV_OTG_MUIC:
  361. return sprintf(buf, "OTG\n");
  362. case ATTACHED_DEV_TA_MUIC:
  363. return sprintf(buf, "TA\n");
  364. case ATTACHED_DEV_JIG_UART_OFF_MUIC:
  365. return sprintf(buf, "JIG UART OFF\n");
  366. case ATTACHED_DEV_JIG_UART_OFF_VB_MUIC:
  367. return sprintf(buf, "JIG UART OFF/VB\n");
  368. case ATTACHED_DEV_JIG_UART_ON_MUIC:
  369. return sprintf(buf, "JIG UART ON\n");
  370. case ATTACHED_DEV_JIG_USB_OFF_MUIC:
  371. return sprintf(buf, "JIG USB OFF\n");
  372. case ATTACHED_DEV_JIG_USB_ON_MUIC:
  373. return sprintf(buf, "JIG USB ON\n");
  374. case ATTACHED_DEV_DESKDOCK_MUIC:
  375. return sprintf(buf, "DESKDOCK\n");
  376. case ATTACHED_DEV_AUDIODOCK_MUIC:
  377. return sprintf(buf, "AUDIODOCK\n");
  378. default:
  379. break;
  380. }
  381. return sprintf(buf, "UNKNOWN\n");
  382. }
  383. static ssize_t sm5502_show_control(struct device *dev,
  384. struct device_attribute *attr,
  385. char *buf)
  386. {
  387. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  388. struct i2c_client *client = usbsw->client;
  389. int value;
  390. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  391. if (value < 0)
  392. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  393. return snprintf(buf, 13, "CONTROL: %02x\n", value);
  394. }
  395. static ssize_t sm5502_show_device_type(struct device *dev,
  396. struct device_attribute *attr,
  397. char *buf)
  398. {
  399. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  400. struct i2c_client *client = usbsw->client;
  401. int value;
  402. value = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  403. if (value < 0)
  404. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  405. return snprintf(buf, 11, "DEV_TYP %02x\n", value);
  406. }
  407. static ssize_t sm5502_show_manualsw(struct device *dev,
  408. struct device_attribute *attr, char *buf)
  409. {
  410. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  411. struct i2c_client *client = usbsw->client;
  412. int value;
  413. value = i2c_smbus_read_byte_data(client, REG_MANUAL_SW1);
  414. if (value < 0)
  415. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  416. if (value == SW_VAUDIO)
  417. return snprintf(buf, 7, "VAUDIO\n");
  418. else if (value == SW_UART)
  419. return snprintf(buf, 5, "UART\n");
  420. else if (value == SW_AUDIO)
  421. return snprintf(buf, 6, "AUDIO\n");
  422. else if (value == SW_DHOST)
  423. return snprintf(buf, 6, "DHOST\n");
  424. else if (value == SW_AUTO)
  425. return snprintf(buf, 5, "AUTO\n");
  426. else
  427. return snprintf(buf, 4, "%x", value);
  428. }
  429. static ssize_t sm5502_set_manualsw(struct device *dev,
  430. struct device_attribute *attr,
  431. const char *buf, size_t count)
  432. {
  433. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  434. struct i2c_client *client = usbsw->client;
  435. int value, ret;
  436. unsigned int path = 0;
  437. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  438. if (value < 0)
  439. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  440. if ((value & ~CON_MANUAL_SW) !=
  441. (CON_SWITCH_OPEN | CON_RAW_DATA | CON_WAIT))
  442. return 0;
  443. if (!strncmp(buf, "VAUDIO", 6)) {
  444. path = SW_VAUDIO;
  445. value &= ~CON_MANUAL_SW;
  446. } else if (!strncmp(buf, "UART", 4)) {
  447. path = SW_UART;
  448. value &= ~CON_MANUAL_SW;
  449. } else if (!strncmp(buf, "AUDIO", 5)) {
  450. path = SW_AUDIO;
  451. value &= ~CON_MANUAL_SW;
  452. } else if (!strncmp(buf, "DHOST", 5)) {
  453. path = SW_DHOST;
  454. value &= ~CON_MANUAL_SW;
  455. } else if (!strncmp(buf, "AUTO", 4)) {
  456. path = SW_AUTO;
  457. value |= CON_MANUAL_SW;
  458. } else {
  459. dev_err(dev, "Wrong command\n");
  460. return 0;
  461. }
  462. usbsw->mansw = path;
  463. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, path);
  464. if (ret < 0)
  465. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  466. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, value);
  467. if (ret < 0)
  468. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  469. return count;
  470. }
  471. static ssize_t sm5502_show_usb_state(struct device *dev,
  472. struct device_attribute *attr,
  473. char *buf)
  474. {
  475. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  476. struct i2c_client *client = usbsw->client;
  477. int device_type1, device_type2;
  478. device_type1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  479. if (device_type1 < 0) {
  480. dev_err(&client->dev, "%s: err %d ", __func__, device_type1);
  481. return (ssize_t)device_type1;
  482. }
  483. device_type2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  484. if (device_type2 < 0) {
  485. dev_err(&client->dev, "%s: err %d ", __func__, device_type2);
  486. return (ssize_t)device_type2;
  487. }
  488. if (device_type1 & DEV_T1_USB_MASK || device_type2 & DEV_T2_USB_MASK)
  489. return snprintf(buf, 22, "USB_STATE_CONFIGURED\n");
  490. return snprintf(buf, 25, "USB_STATE_NOTCONFIGURED\n");
  491. }
  492. static ssize_t sm5502_show_adc(struct device *dev,
  493. struct device_attribute *attr,
  494. char *buf)
  495. {
  496. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  497. struct i2c_client *client = usbsw->client;
  498. int adc;
  499. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  500. if (adc < 0) {
  501. dev_err(&client->dev,
  502. "%s: err at read adc %d\n", __func__, adc);
  503. return snprintf(buf, 9, "UNKNOWN\n");
  504. }
  505. return snprintf(buf, 4, "%x\n", adc);
  506. }
  507. static ssize_t sm5502_reset(struct device *dev,
  508. struct device_attribute *attr,
  509. const char *buf, size_t count)
  510. {
  511. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  512. struct i2c_client *client = usbsw->client;
  513. if (!strncmp(buf, "1", 1)) {
  514. dev_info(&client->dev,
  515. "sm5502 reset after delay 1000 msec.\n");
  516. msleep(1000);
  517. sm5502_write_reg(client, REG_RESET, 0x01);
  518. dev_info(&client->dev, "sm5502_reset_control done!\n");
  519. } else {
  520. dev_info(&client->dev,
  521. "sm5502_reset_control, but not reset_value!\n");
  522. }
  523. #ifdef CONFIG_MUIC_SUPPORT_RUSTPROOF
  524. usbsw->is_rustproof = false;
  525. #endif
  526. usbsw->attached_dev = ATTACHED_DEV_NONE_MUIC;
  527. sm5502_reg_init(usbsw);
  528. return count;
  529. }
  530. #ifdef CONFIG_MUIC_SUPPORT_RUSTPROOF
  531. static void muic_rustproof_feature(struct i2c_client *client, int state);
  532. /* Keystring "*#0*#" sysfs implementation */
  533. static ssize_t uart_en_show(struct device *dev,
  534. struct device_attribute *attr, char *buf)
  535. {
  536. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  537. /*is_rustproof is false then UART can be enabled*/
  538. return snprintf(buf, 4, "%d\n", !(usbsw->is_rustproof));
  539. }
  540. static ssize_t uart_en_store(struct device *dev,
  541. struct device_attribute *attr,
  542. const char *buf, size_t size)
  543. {
  544. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  545. struct i2c_client *client = usbsw->client;
  546. if (!strncmp(buf, "1", 1)) {
  547. dev_info(&client->dev,
  548. "[MUIC]Runtime enabling the UART.\n");
  549. usbsw->is_rustproof = false;
  550. muic_rustproof_feature(client,SM5502_DETACHED);
  551. } else {
  552. dev_info(&client->dev,
  553. "[MUIC]Runtime disabling the UART.\n");
  554. usbsw->is_rustproof = true;
  555. }
  556. /* reinvoke the attach detection function to set proper paths */
  557. sm5502_attach_dev(usbsw);
  558. return size;
  559. }
  560. static DEVICE_ATTR(uart_en, S_IRUGO | S_IWUSR ,
  561. uart_en_show, uart_en_store);
  562. static ssize_t uart_sel_show(struct device *dev,
  563. struct device_attribute *attr, char *buf)
  564. {
  565. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  566. /*for sm5502 paths are always switch to AP*/
  567. if(usbsw->attached_dev != ATTACHED_DEV_NONE_MUIC)
  568. return snprintf(buf, 4, "AP\n");
  569. else
  570. return snprintf(buf, 9, "UNKNOWN\n");
  571. }
  572. static ssize_t uart_sel_store(struct device *dev,
  573. struct device_attribute *attr,
  574. const char *buf, size_t size)
  575. {
  576. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  577. struct i2c_client *client = usbsw->client;
  578. dev_info(&client->dev,"[MUIC]Enabling AP UART Path, dummy Call\n");
  579. return size;
  580. }
  581. static DEVICE_ATTR(uart_sel, S_IRUGO | S_IWUSR ,
  582. uart_sel_show, uart_sel_store);
  583. static ssize_t usbsel_show(struct device *dev,
  584. struct device_attribute *attr, char *buf)
  585. {
  586. return snprintf(buf, 4, "PDA\n");
  587. }
  588. static ssize_t usbsel_store(struct device *dev,
  589. struct device_attribute *attr,
  590. const char *buf, size_t size)
  591. {
  592. struct sm5502_usbsw *usbsw = dev_get_drvdata(dev);
  593. struct i2c_client *client = usbsw->client;
  594. dev_info(&client->dev,"[MUIC]Enabling AP UART Path, dummy Call\n");
  595. return size;
  596. }
  597. static DEVICE_ATTR(usb_sel, S_IRUGO | S_IWUSR ,
  598. usbsel_show, usbsel_store);
  599. #endif
  600. static DEVICE_ATTR(control, S_IRUGO, sm5502_show_control, NULL);
  601. static DEVICE_ATTR(device_type, S_IRUGO, sm5502_show_device_type, NULL);
  602. static DEVICE_ATTR(switch, S_IRUGO | S_IWUSR,
  603. sm5502_show_manualsw, sm5502_set_manualsw);
  604. static DEVICE_ATTR(usb_state, S_IRUGO, sm5502_show_usb_state, NULL);
  605. static DEVICE_ATTR(adc, S_IRUGO, sm5502_show_adc, NULL);
  606. static DEVICE_ATTR(reset_switch, S_IWUSR | S_IWGRP, NULL, sm5502_reset);
  607. static DEVICE_ATTR(attached_dev, S_IRUGO, sm5502_muic_show_attached_dev, NULL);
  608. static struct attribute *sm5502_attributes[] = {
  609. &dev_attr_control.attr,
  610. &dev_attr_device_type.attr,
  611. &dev_attr_switch.attr,
  612. NULL
  613. };
  614. static const struct attribute_group sm5502_group = {
  615. .attrs = sm5502_attributes,
  616. };
  617. #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
  618. #define PMIC_SLAVE_ID 0x0
  619. #define PMIC_SMBBP_BAT_IF_BPD_CTRL 0x1248
  620. /* control flash function without BAT_ID */
  621. static void flash_control(bool en) {
  622. u8 sid = PMIC_SLAVE_ID;
  623. u8 buf[16];
  624. int addr = PMIC_SMBBP_BAT_IF_BPD_CTRL;
  625. spmi_ext_register_readl_extra(sid, addr, buf, 1);
  626. if (en)
  627. buf[0] &= ~BIT(0);
  628. else
  629. buf[0] |= BIT(0);
  630. spmi_ext_register_writel_extra(sid, addr, buf, 1);
  631. }
  632. #endif
  633. #if defined(CONFIG_USB_HOST_NOTIFY)
  634. static void sm5502_set_otg(struct sm5502_usbsw *usbsw, int state)
  635. {
  636. int ret;
  637. struct i2c_client *client = usbsw->client;
  638. if (state == SM5502_ATTACHED) {
  639. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, 0x25);
  640. if (ret < 0)
  641. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  642. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00); //Disconnecting the MUIC_ID & ITBP Pins
  643. if (ret < 0)
  644. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  645. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  646. if (ret < 0)
  647. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  648. ret = ret & 0xFB; /*Manual Connection S/W enable*/
  649. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  650. if (ret < 0)
  651. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  652. } else {
  653. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00);
  654. if (ret < 0)
  655. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  656. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,
  657. SW_ALL_OPEN);
  658. if (ret < 0)
  659. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  660. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  661. if (ret < 0)
  662. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  663. ret = ret | 0x04; /*Manual Connection S/W Disable*/
  664. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  665. if (ret < 0)
  666. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  667. }
  668. }
  669. #endif
  670. #ifndef CONFIG_USB_HOST_NOTIFY
  671. /* Dummy callback Function to handle the OTG Test case*/
  672. int sec_get_notification(int mode)
  673. {
  674. return 1;
  675. }
  676. #endif
  677. #if defined(CONFIG_VIDEO_MHL_V2)
  678. int dock_det(void)
  679. {
  680. return local_usbsw->dock_attached;
  681. }
  682. EXPORT_SYMBOL(dock_det);
  683. #endif
  684. int check_sm5502_jig_state(void)
  685. {
  686. return jig_state;
  687. }
  688. EXPORT_SYMBOL(check_sm5502_jig_state);
  689. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  690. extern void tsp_charger_infom(bool en);
  691. #endif
  692. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  693. static void sm5502_set_lanhub(struct sm5502_usbsw *usbsw, int state)
  694. {
  695. int ret;
  696. struct i2c_client *client = usbsw->client;
  697. if (state == SM5502_ATTACHED) {
  698. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, 0x25);
  699. if (ret < 0)
  700. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  701. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00); //Disconnect the ITBP & MUIC_ID Pins
  702. if (ret < 0)
  703. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  704. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  705. if (ret < 0)
  706. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  707. ret = ret & 0xFB; /*Manual Connection S/W enable*/
  708. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  709. if (ret < 0)
  710. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  711. } else {
  712. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00);
  713. if (ret < 0)
  714. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  715. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,
  716. SW_ALL_OPEN);
  717. if (ret < 0)
  718. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  719. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  720. if (ret < 0)
  721. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  722. ret = ret | 0x04; /*Manual Connection S/W enable*/
  723. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  724. if (ret < 0)
  725. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  726. }
  727. }
  728. static void sm5502_mask_vbus_detect(struct sm5502_usbsw *usbsw, int state){
  729. unsigned int value;
  730. struct i2c_client *client = usbsw->client;
  731. if(state == SM5502_ATTACHED) {
  732. pr_info("%s called, state: (%d)\n",__func__,state);
  733. /*Need to disable the vbus change interrupts*/
  734. value = i2c_smbus_read_byte_data(client,REG_INT_MASK2);
  735. if (value < 0)
  736. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  737. value |= 0x01;
  738. value = i2c_smbus_write_byte_data(client,REG_INT_MASK2,value);
  739. if (value < 0)
  740. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  741. }
  742. else {
  743. pr_info("%s called, state: (%d)\n",__func__,state);
  744. value = i2c_smbus_write_byte_data(client,REG_INT_MASK2,INT_MASK2);
  745. if (value < 0)
  746. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  747. }
  748. }
  749. static int sm5502_detect_lanhub(struct sm5502_usbsw *usbsw) {
  750. unsigned int dev1, dev2, adc;
  751. struct sm5502_platform_data *pdata = usbsw->pdata;
  752. struct i2c_client *client = usbsw->client;
  753. pr_info("%s called\n",__func__);
  754. dev1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  755. if (dev1 < 0) {
  756. dev_err(&client->dev, "%s: err %d\n", __func__, dev1);
  757. return dev1;
  758. }
  759. dev2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  760. if (dev2 < 0) {
  761. dev_err(&client->dev, "%s: err %d\n", __func__, dev2);
  762. return dev2;
  763. }
  764. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  765. dev_info(&client->dev, "dev1: 0x%02x, dev2: 0x%02x, adc: 0x%02x\n",
  766. dev1, dev2, adc);
  767. /* Attached + Detached*/
  768. switch(adc){
  769. case ADC_OTG:
  770. lanhub_ta_case = false;
  771. usbsw->adc = adc;
  772. sm5502_set_otg(usbsw, SM5502_ATTACHED);
  773. if(usbsw->previous_dock == SM5502_NONE) {
  774. dev_info(&client->dev, "%s:LANHUB Connected\n", __func__);
  775. pdata->callback(CABLE_TYPE_OTG, SM5502_ATTACHED);
  776. sm5502_set_otg(usbsw,SM5502_ATTACHED);
  777. }
  778. else if(usbsw->previous_dock == ADC_LANHUB) {
  779. dev_info(&client->dev, "%s:Switch LANHUB+TA to LANHUB\n", __func__);
  780. usbsw->lanhub_ta_status=0;
  781. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5502_DETACHED, LANHUB_TA);
  782. }
  783. sm5502_mask_vbus_detect(usbsw,SM5502_DETACHED);
  784. usbsw->dock_attached = SM5502_ATTACHED;
  785. usbsw->previous_dock = ADC_OTG;
  786. break;
  787. case ADC_LANHUB:
  788. usbsw->adc = adc;
  789. lanhub_ta_case = true;
  790. usbsw->lanhub_ta_status = 1;
  791. sm5502_mask_vbus_detect(usbsw,SM5502_ATTACHED);
  792. usbsw->dock_attached = SM5502_ATTACHED;
  793. usbsw->mansw = SW_DHOST;
  794. if(usbsw->previous_dock == SM5502_NONE) {
  795. dev_info(&client->dev, "%s:LANHUB+TA Connected\n", __func__);
  796. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5502_ATTACHED, LANHUB);
  797. sm5502_set_lanhub(usbsw,SM5502_ATTACHED);
  798. } else if (usbsw->previous_dock == ADC_OTG) {
  799. dev_info(&client->dev, "%s:Switch LANHUB to LANHUB+TA\n", __func__);
  800. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5502_ATTACHED, LANHUB_TA);
  801. }
  802. usbsw->previous_dock = ADC_LANHUB;
  803. break;
  804. case ADC_OPEN:
  805. usbsw->adc = adc;
  806. dev_info(&client->dev, "%s:LANHUB + TA -> ADC_OPEN case\n", __func__);
  807. if (pdata->lanhub_cb && usbsw->lanhub_ta_status == 1) {
  808. if (usbsw->previous_dock == ADC_LANHUB)
  809. pdata->lanhub_cb(CABLE_TYPE_LANHUB,SM5502_DETACHED, LANHUB);
  810. sm5502_disable_rawdataInterrupts(usbsw);
  811. usbsw->lanhub_ta_status=0;
  812. sm5502_mask_vbus_detect(usbsw,SM5502_DETACHED);
  813. pdata->callback(CABLE_TYPE_OTG,SM5502_DETACHED);
  814. sm5502_set_otg(usbsw, SM5502_DETACHED);
  815. }
  816. else if (usbsw->previous_dock == ADC_OTG) {
  817. sm5502_disable_rawdataInterrupts(usbsw);
  818. pdata->callback(CABLE_TYPE_OTG,SM5502_DETACHED);
  819. sm5502_set_otg(usbsw, SM5502_DETACHED);
  820. }else {
  821. dev_info(&client->dev, "%s:ignore ADC_OPEN case\n", __func__);
  822. sm5502_mask_vbus_detect(usbsw,SM5502_DETACHED);
  823. }
  824. usbsw->previous_dock = SM5502_NONE;
  825. lanhub_ta_case = false;
  826. break;
  827. default:
  828. dev_info(&client->dev, "%s:Not reaching here(adc:0x%02x)\n",
  829. __func__, adc);
  830. lanhub_ta_case = false;
  831. break;
  832. }
  833. usbsw->dev1=dev1;
  834. usbsw->dev2=dev2;
  835. return adc;
  836. }
  837. #endif
  838. // For Torch enable
  839. #if defined(CONFIG_TORCH_FIX)
  840. int factory_uart_connected()
  841. {
  842. int val2 = 0, jig_state_factory = 0;
  843. struct i2c_client *client = local_usbsw->client;
  844. val2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  845. jig_state_factory = (val2 & DEV_T2_JIG_ALL_MASK) ? 1 : 0;
  846. printk(KERN_INFO "%s: jig_state_factory = %d\n", __func__, jig_state_factory);
  847. return jig_state_factory;
  848. }
  849. EXPORT_SYMBOL(factory_uart_connected);
  850. #endif
  851. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  852. static void muic_rustproof_feature(struct i2c_client *client, int state)
  853. {
  854. int val;
  855. if(state) {
  856. val = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,
  857. SW_ALL_OPEN_WITH_VBUS);
  858. if(val < 0)
  859. dev_info(&client->dev, "%s:MANUAL SW1,err %d\n",__func__,val);
  860. val = i2c_smbus_read_byte_data(client,REG_CONTROL);
  861. if(val < 0)
  862. dev_info(&client->dev, "%s:CTRL REG,err %d\n",__func__,val);
  863. val &= 0xFB;
  864. val = i2c_smbus_write_byte_data(client,REG_CONTROL,val);
  865. if(val < 0)
  866. dev_info(&client->dev, "%s:CTRL REG,err %d\n",__func__,val);
  867. } else
  868. {
  869. val = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00);
  870. if (val < 0)
  871. dev_info(&client->dev, "%s: MANUAL SW2,err %d\n", __func__,val);
  872. val = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,SW_ALL_OPEN);
  873. if (val < 0)
  874. dev_info(&client->dev, "%s: MANUAL SW1,err %d\n", __func__,val);
  875. val = i2c_smbus_read_byte_data(client, REG_CONTROL);
  876. if (val < 0)
  877. dev_info(&client->dev, "%s: CTRL REG,err %d\n", __func__,val);
  878. val = val | 0x04; /*Automatic Connection S/W enable*/
  879. val = i2c_smbus_write_byte_data(client, REG_CONTROL, val);
  880. if (val < 0)
  881. dev_info(&client->dev, "%s: CTRL REG,err %d\n", __func__,val);
  882. }
  883. }
  884. #endif
  885. static void muic_update_jig_state(struct sm5502_usbsw *usbsw, int dev_type2, int vbus)
  886. {
  887. if(dev_type2 & DEV_JIG_UART_OFF && !vbus)
  888. usbsw->attached_dev = ATTACHED_DEV_JIG_UART_OFF_MUIC;
  889. else if(dev_type2 & DEV_JIG_UART_OFF && vbus)
  890. usbsw->attached_dev = ATTACHED_DEV_JIG_UART_OFF_VB_MUIC;
  891. else if(dev_type2 & DEV_JIG_UART_ON)
  892. usbsw->attached_dev = ATTACHED_DEV_JIG_UART_ON_MUIC;
  893. else if(dev_type2 & DEV_JIG_USB_OFF)
  894. usbsw->attached_dev = ATTACHED_DEV_JIG_USB_OFF_MUIC;
  895. else if(dev_type2 & DEV_JIG_USB_ON)
  896. usbsw->attached_dev = ATTACHED_DEV_JIG_USB_ON_MUIC;
  897. }
  898. #if defined CONFIG_ID_BYPASS_SBL
  899. int otg_attached = 0;
  900. #endif
  901. static int sm5502_attach_dev(struct sm5502_usbsw *usbsw)
  902. {
  903. int adc;
  904. int val1, val2, val3, val4, vbus;
  905. struct sm5502_platform_data *pdata = usbsw->pdata;
  906. struct i2c_client *client = usbsw->client;
  907. #if defined(CONFIG_VIDEO_MHL_V2)
  908. /*u8 mhl_ret = 0;*/
  909. #endif
  910. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  911. int tsp_noti_ignore = 0;
  912. #endif
  913. val1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  914. if (val1 < 0) {
  915. dev_err(&client->dev, "%s: err %d\n", __func__, val1);
  916. return val1;
  917. }
  918. val2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  919. if (val2 < 0) {
  920. dev_err(&client->dev, "%s: err %d\n", __func__, val2);
  921. return val2;
  922. }
  923. jig_state = (val2 & DEV_T2_JIG_ALL_MASK) ? 1 : 0;
  924. val3 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE3);
  925. if (val3 < 0) {
  926. dev_err(&client->dev, "%s: err %d\n", __func__, val3);
  927. return val3;
  928. }
  929. val4 = i2c_smbus_read_byte_data(client, REG_CARKIT_STATUS);
  930. if (val4 < 0) {
  931. dev_err(&client->dev, "%s: err %d\n", __func__, val4);
  932. return val4;
  933. }
  934. // add start
  935. vbus = i2c_smbus_read_byte_data(client, REG_VBUSINVALID);
  936. if (vbus < 0) {
  937. dev_err(&client->dev, "%s: err %d\n", __func__, vbus);
  938. return vbus;
  939. }
  940. // add end
  941. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  942. if (adc == ADC_SMART_DOCK) {
  943. val2 = DEV_SMARTDOCK;
  944. val1 = 0;
  945. }
  946. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  947. else if (adc == ADC_LANHUB) {
  948. val2 = DEV_LANHUB;
  949. val1 = 0;
  950. }
  951. #endif
  952. #if defined(CONFIG_USB_HOST_NOTIFY)
  953. if (adc == 0x11 || adc == ADC_AUDIO_DOCK) {
  954. val2 = DEV_AUDIO_DOCK;
  955. val1 = 0;
  956. }
  957. #endif
  958. dev_err(&client->dev,
  959. "dev1: 0x%x,dev2: 0x%x,dev3: 0x%x,Carkit: 0x%x,ADC: 0x%x,Jig: %s\n",
  960. val1, val2, val3, val4, adc,
  961. (check_sm5502_jig_state() ? "ON" : "OFF"));
  962. /* USB */
  963. if (val1 & DEV_USB) {
  964. pr_info("[MUIC] USB Connected\n");
  965. pdata->callback(CABLE_TYPE_USB, SM5502_ATTACHED);
  966. usbsw->attached_dev = ATTACHED_DEV_USB_MUIC;
  967. /* D+,D-open */
  968. } else if (val3 & DEV_NON_STANDARD) {
  969. pr_info("[MUIC] D+,D-open Connected\n");
  970. pdata->callback(CABLE_TYPE_USB, SM5502_ATTACHED);
  971. /* USB_CDP */
  972. } else if (val1 & DEV_USB_CHG) {
  973. pr_info("[MUIC] CDP Connected\n");
  974. pdata->callback(CABLE_TYPE_CDP, SM5502_ATTACHED);
  975. usbsw->attached_dev = ATTACHED_DEV_CDP_MUIC;
  976. /* UART */
  977. } else if (val1 & DEV_T1_UART_MASK || val2 & DEV_T2_UART_MASK) {
  978. uart_sm5502_connecting = 1;
  979. muic_update_jig_state(usbsw,val2,vbus);
  980. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  981. if(usbsw->is_rustproof) {
  982. pr_info("[MUIC] RustProof mode, close UART Path\n");
  983. muic_rustproof_feature(client,SM5502_ATTACHED);
  984. } else
  985. #endif
  986. {
  987. pr_info("[MUIC] UART Connected\n");
  988. i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, SW_UART);
  989. if(vbus & DEV_VBUSIN_VALID)
  990. pdata->callback(CABLE_TYPE_JIG_UART_OFF_VB, SM5502_ATTACHED);
  991. else
  992. pdata->callback(CABLE_TYPE_UARTOFF, SM5502_ATTACHED);
  993. #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
  994. flash_control(true);
  995. #endif
  996. }
  997. #ifndef CONFIG_ATT_CHARGER_SUPPORT
  998. /* L Company Cable Detection Code */
  999. } else if (adc == 0x17 &&
  1000. (val4 & DEV_CARKIT_CHARGER1_MASK)) {
  1001. pr_info("[MUIC] 219K USB Cable/Charger Connected\n");
  1002. pdata->callback(CABLE_TYPE_219KUSB,SM5502_ATTACHED);
  1003. usbsw->attached_dev = ATTACHED_DEV_USB_MUIC;
  1004. #endif
  1005. /* CHARGER */
  1006. } else if ((val1 & DEV_T1_CHARGER_MASK) ||
  1007. (val3 & DEV_T3_CHARGER_MASK)) {
  1008. pr_info("[MUIC] Charger Connected\n");
  1009. usbsw->attached_dev = ATTACHED_DEV_TA_MUIC;
  1010. pdata->callback(CABLE_TYPE_AC, SM5502_ATTACHED);
  1011. #if defined(CONFIG_USB_HOST_NOTIFY)
  1012. /* for SAMSUNG OTG */
  1013. } else if (val1 & DEV_USB_OTG && adc == ADC_OTG) {
  1014. pr_info("[MUIC] OTG Connected\n");
  1015. usbsw->attached_dev = ATTACHED_DEV_OTG_MUIC;
  1016. #if defined CONFIG_ID_BYPASS_SBL
  1017. otg_attached = 1;
  1018. #endif
  1019. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  1020. sm5502_enable_rawdataInterrupts(usbsw);
  1021. usbsw->dock_attached = SM5502_ATTACHED;
  1022. usbsw->previous_dock = ADC_OTG;
  1023. #endif
  1024. sm5502_set_otg(usbsw, SM5502_ATTACHED);
  1025. pdata->callback(CABLE_TYPE_OTG, SM5502_ATTACHED);
  1026. #endif
  1027. /* JIG */
  1028. } else if (val2 & DEV_T2_USB_MASK) {
  1029. pr_info("[MUIC] JIG USB Connected\n");
  1030. muic_update_jig_state(usbsw,val2,vbus);
  1031. if(vbus & DEV_VBUSIN_VALID)
  1032. pdata->callback(CABLE_TYPE_USB, SM5502_ATTACHED);
  1033. else
  1034. pdata->callback(CABLE_TYPE_JIG, SM5502_ATTACHED);
  1035. #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
  1036. flash_control(true);
  1037. #endif
  1038. /* Desk Dock */
  1039. } else if ((val2 & DEV_AV) || (val3 & DEV_AV_VBUS)) {
  1040. pr_info("[MUIC] Deskdock Connected\n");
  1041. usbsw->attached_dev = ATTACHED_DEV_DESKDOCK_MUIC;
  1042. local_usbsw->dock_attached = SM5502_ATTACHED;
  1043. if(vbus & DEV_VBUSIN_VALID)
  1044. sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK,
  1045. SM5502_ATTACHED, SW_AUDIO);
  1046. else
  1047. sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK_NO_VB,
  1048. SM5502_ATTACHED, SW_AUDIO);
  1049. #if defined(CONFIG_VIDEO_MHL_V2)
  1050. /* MHL */
  1051. } else if (val3 & DEV_MHL) {
  1052. pr_info("[MUIC] MHL Connected\n");
  1053. if (!poweroff_charging)
  1054. /*mhl_ret = mhl_onoff_ex(1); support from sii8240*/
  1055. else
  1056. pr_info("LPM mode, skip MHL sequence\n");
  1057. #endif
  1058. /* Car Dock */
  1059. } else if (val2 & DEV_JIG_UART_ON) {
  1060. pr_info("[MUIC] Cardock Connected\n");
  1061. muic_update_jig_state(usbsw,val2,vbus);
  1062. #if defined(CONFIG_SEC_FACTORY)
  1063. local_usbsw->dock_attached = SM5502_ATTACHED;
  1064. sm5502_dock_control(usbsw, CABLE_TYPE_CARDOCK,
  1065. SM5502_ATTACHED, SW_AUDIO);
  1066. #elif defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1067. if(usbsw->is_rustproof) {
  1068. pr_info("[MUIC] RustProof mode, close UART Path\n");
  1069. muic_rustproof_feature(client,SM5502_ATTACHED);
  1070. }
  1071. #endif
  1072. /* SmartDock */
  1073. } else if (val2 & DEV_SMARTDOCK) {
  1074. pr_info("[MUIC] Smartdock Connected\n");
  1075. sm5502_dock_control(usbsw, CABLE_TYPE_SMART_DOCK,
  1076. SM5502_ATTACHED, SW_DHOST);
  1077. #if defined(CONFIG_VIDEO_MHL_V2)
  1078. /*mhl_onoff_ex(1); support can be added once mhl is up*/
  1079. #endif
  1080. #if defined(CONFIG_USB_HOST_NOTIFY)
  1081. /* Audio Dock */
  1082. } else if (val2 & DEV_AUDIO_DOCK) {
  1083. pr_info("[MUIC] Audiodock Connected\n");
  1084. usbsw->attached_dev = ATTACHED_DEV_AUDIODOCK_MUIC;
  1085. sm5502_dock_control(usbsw, CABLE_TYPE_AUDIO_DOCK,
  1086. SM5502_ATTACHED, SW_DHOST);
  1087. #endif
  1088. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  1089. /* LANHUB */
  1090. } else if (val2 & DEV_LANHUB) {
  1091. if(usbsw->previous_dock == ADC_LANHUB &&
  1092. usbsw->lanhub_ta_status == 1 )
  1093. /*Skip for the LANHUB+TA Case*/
  1094. pr_info("[MUIC] Lanhub + TA is connected\n");
  1095. else {
  1096. /* Enable RAWDATA Interrupts */
  1097. sm5502_enable_rawdataInterrupts(usbsw);
  1098. sm5502_detect_lanhub(usbsw);
  1099. }
  1100. #endif
  1101. /* Charging Cable */
  1102. } else if ((val2 & DEV_PPD) && (adc == ADC_CHARGING_CABLE)) {
  1103. pr_info("[MUIC] Phone Charging cable Connected\n");
  1104. pdata->callback(CABLE_TYPE_CHARGING_CABLE,
  1105. SM5502_ATTACHED);
  1106. /* Incompatible */
  1107. } else if (vbus & DEV_VBUSIN_VALID) {
  1108. pr_info("[MUIC] Incompatible Charger Connected\n");
  1109. usbsw->attached_dev = ATTACHED_DEV_UNKNOWN_MUIC;
  1110. pdata->callback(CABLE_TYPE_INCOMPATIBLE,
  1111. SM5502_ATTACHED);
  1112. }
  1113. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  1114. else{
  1115. tsp_noti_ignore = 1;
  1116. printk("[TSP] attached, but don't noti \n");
  1117. }
  1118. if(!tsp_noti_ignore)
  1119. tsp_charger_infom(1);
  1120. #endif
  1121. usbsw->dev1 = val1;
  1122. usbsw->dev2 = val2;
  1123. usbsw->dev3 = val3;
  1124. usbsw->adc = adc;
  1125. usbsw->vbus = vbus;
  1126. usbsw->carkit_dev = val4;
  1127. return adc;
  1128. }
  1129. static int sm5502_detach_dev(struct sm5502_usbsw *usbsw)
  1130. {
  1131. struct sm5502_platform_data *pdata = usbsw->pdata;
  1132. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  1133. int tsp_noti_ignore = 0;
  1134. #endif
  1135. /* USB */
  1136. if (usbsw->dev1 & DEV_USB) {
  1137. pr_info("[MUIC] USB Disconnected\n");
  1138. pdata->callback(CABLE_TYPE_USB, SM5502_DETACHED);
  1139. } else if (usbsw->dev1 & DEV_USB_CHG) {
  1140. pdata->callback(CABLE_TYPE_CDP, SM5502_DETACHED);
  1141. /* D+,D-open */
  1142. } else if (usbsw->dev3 & DEV_NON_STANDARD) {
  1143. pr_info("[MUIC] D+,D-open Disconnected\n");
  1144. pdata->callback(CABLE_TYPE_USB, SM5502_DETACHED);
  1145. /* UART */
  1146. } else if (usbsw->dev1 & DEV_T1_UART_MASK ||
  1147. usbsw->dev2 & DEV_T2_UART_MASK) {
  1148. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1149. if(usbsw->is_rustproof) {
  1150. pr_info("[MUIC] RustProof mode Disconnected Event\n");
  1151. muic_rustproof_feature(usbsw->client,SM5502_DETACHED);
  1152. } else
  1153. #endif
  1154. {
  1155. pr_info("[MUIC] UART Disconnected\n");
  1156. if(usbsw->vbus & DEV_VBUSIN_VALID)
  1157. pdata->callback(CABLE_TYPE_JIG_UART_OFF_VB, SM5502_DETACHED);
  1158. else
  1159. pdata->callback(CABLE_TYPE_UARTOFF, SM5502_DETACHED);
  1160. uart_sm5502_connecting = 0;
  1161. #if (defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_CHN_CMCC_3G) || defined(CONFIG_MACH_MS01_CHN_CU_3G))
  1162. flash_control(false);
  1163. #endif
  1164. }
  1165. #ifndef CONFIG_ATT_CHARGER_SUPPORT
  1166. /* L Company Cable Detection Code */
  1167. } else if (usbsw->adc == 0x17 &&
  1168. (usbsw->carkit_dev & DEV_CARKIT_CHARGER1_MASK)) {
  1169. pr_info("[MUIC] 219K USB Cable/Charger Disconnected\n");
  1170. pdata->callback(CABLE_TYPE_219KUSB,SM5502_DETACHED);
  1171. #endif
  1172. /* CHARGER */
  1173. } else if ((usbsw->dev1 & DEV_T1_CHARGER_MASK) ||
  1174. (usbsw->dev3 & DEV_T3_CHARGER_MASK)) {
  1175. pr_info("[MUIC] Charger Disconnected\n");
  1176. pdata->callback(CABLE_TYPE_AC, SM5502_DETACHED);
  1177. #if defined(CONFIG_USB_HOST_NOTIFY)
  1178. /* for SAMSUNG OTG */
  1179. } else if (usbsw->dev1 & DEV_USB_OTG) {
  1180. pr_info("[MUIC] OTG Disconnected\n");
  1181. #if defined CONFIG_ID_BYPASS_SBL
  1182. otg_attached = 0;
  1183. #endif
  1184. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  1185. sm5502_disable_rawdataInterrupts(usbsw);
  1186. pr_info("%s:lanhub_ta_status(%d)\n",
  1187. __func__, usbsw->lanhub_ta_status);
  1188. lanhub_ta_case = false;
  1189. if (usbsw->lanhub_ta_status == 0) {
  1190. pdata->callback(CABLE_TYPE_OTG,SM5502_DETACHED);
  1191. sm5502_set_otg(usbsw, SM5502_DETACHED);
  1192. }
  1193. else if (pdata->lanhub_cb && usbsw->lanhub_ta_status == 1)
  1194. pdata->lanhub_cb(CABLE_TYPE_LANHUB,SM5502_DETACHED, LANHUB);
  1195. usbsw->dock_attached = SM5502_DETACHED;
  1196. usbsw->lanhub_ta_status=0;
  1197. #else
  1198. sm5502_set_otg(usbsw, SM5502_DETACHED);
  1199. pdata->callback(CABLE_TYPE_OTG, SM5502_DETACHED);
  1200. #endif
  1201. #endif
  1202. /* JIG */
  1203. } else if (usbsw->dev2 & DEV_T2_USB_MASK) {
  1204. pr_info("[MUIC] JIG USB Disconnected\n");
  1205. if(usbsw->vbus & DEV_VBUSIN_VALID)
  1206. pdata->callback(CABLE_TYPE_USB, SM5502_DETACHED);
  1207. else
  1208. pdata->callback(CABLE_TYPE_JIG, SM5502_DETACHED);
  1209. #if (defined(CONFIG_MACH_MS01_EUR_3G) || defined(CONFIG_MACH_MS01_CHN_CMCC_3G) || defined(CONFIG_MACH_MS01_CHN_CU_3G))
  1210. flash_control(false);
  1211. #endif
  1212. /* Desk Dock */
  1213. } else if ((usbsw->dev2 & DEV_AV) ||
  1214. (usbsw->dev3 & DEV_AV_VBUS)) {
  1215. pr_info("[MUIC] Deskdock Disconnected\n");
  1216. local_usbsw->dock_attached = SM5502_DETACHED;
  1217. if(usbsw->vbus & DEV_VBUSIN_VALID)
  1218. sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK,
  1219. SM5502_DETACHED, SW_ALL_OPEN);
  1220. else
  1221. sm5502_dock_control(usbsw, CABLE_TYPE_DESK_DOCK_NO_VB,
  1222. SM5502_DETACHED, SW_ALL_OPEN);
  1223. #if defined(CONFIG_MHL_D3_SUPPORT)
  1224. /* MHL */
  1225. } else if (usbsw->dev3 & DEV_MHL) {
  1226. pr_info("[MUIC] MHL Disconnected\n");
  1227. //mhl_onoff_ex(false);
  1228. detached_sm5502_status = 1;
  1229. #endif
  1230. /* Car Dock */
  1231. } else if (usbsw->dev2 & DEV_JIG_UART_ON) {
  1232. pr_info("[MUIC] Cardock Disconnected\n");
  1233. #if defined(CONFIG_SEC_FACTORY)
  1234. local_usbsw->dock_attached = SM5502_DETACHED;
  1235. sm5502_dock_control(usbsw, CABLE_TYPE_CARDOCK,
  1236. SM5502_DETACHED, SW_ALL_OPEN);
  1237. #elif defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1238. if(usbsw->is_rustproof) {
  1239. pr_info("[MUIC] RustProof mode disconneted Event\n");
  1240. muic_rustproof_feature(usbsw->client,SM5502_DETACHED);
  1241. }
  1242. #endif
  1243. /* Smart Dock */
  1244. } else if (usbsw->dev2 == DEV_SMARTDOCK) {
  1245. pr_info("[MUIC] Smartdock Disconnected\n");
  1246. sm5502_dock_control(usbsw, CABLE_TYPE_SMART_DOCK,
  1247. SM5502_DETACHED, SW_ALL_OPEN);
  1248. #if defined(CONFIG_VIDEO_MHL_V2)
  1249. //mhl_onoff_ex(false);
  1250. #endif
  1251. #if defined(CONFIG_USB_HOST_NOTIFY)
  1252. /* Audio Dock */
  1253. } else if (usbsw->dev2 == DEV_AUDIO_DOCK) {
  1254. pr_info("[MUIC] Audiodock Disconnected\n");
  1255. sm5502_dock_control(usbsw, CABLE_TYPE_AUDIO_DOCK,
  1256. SM5502_DETACHED, SW_ALL_OPEN);
  1257. #endif
  1258. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  1259. /* LANHUB */
  1260. } else if (usbsw->adc == ADC_LANHUB) {
  1261. pr_info("[MUIC] Lanhub disconnected\n");
  1262. /* Disable RAWDATA Interrupts */
  1263. sm5502_disable_rawdataInterrupts(usbsw);
  1264. lanhub_ta_case = false;
  1265. usbsw->dock_attached = SM5502_DETACHED;
  1266. sm5502_set_lanhub(usbsw,SM5502_DETACHED);
  1267. sm5502_mask_vbus_detect(usbsw, SM5502_DETACHED);
  1268. pr_info("%s:lanhub_ta_status(%d)\n",__func__, usbsw->lanhub_ta_status);
  1269. if (pdata->lanhub_cb && usbsw->lanhub_ta_status==1)
  1270. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5502_DETACHED, LANHUB);
  1271. else if (usbsw->lanhub_ta_status == 0) {
  1272. pdata->callback(CABLE_TYPE_OTG, SM5502_DETACHED);
  1273. sm5502_set_otg(usbsw, SM5502_DETACHED);
  1274. }
  1275. usbsw->lanhub_ta_status=0;
  1276. usbsw->dock_attached = SM5502_DETACHED;
  1277. #endif
  1278. /* Charging Cable */
  1279. } else if ((usbsw->dev2 & DEV_PPD) &&
  1280. (usbsw->adc == ADC_CHARGING_CABLE)) {
  1281. pr_info("[MUIC] Phone Charging cable Disconnected\n");
  1282. pdata->callback(CABLE_TYPE_CHARGING_CABLE,
  1283. SM5502_DETACHED);
  1284. /* Incompatible */
  1285. } else if (usbsw->vbus & DEV_VBUSIN_VALID) {
  1286. pr_info("[MUIC] Incompatible Charger Disconnected\n");
  1287. pdata->callback(CABLE_TYPE_INCOMPATIBLE,
  1288. SM5502_DETACHED);
  1289. }
  1290. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  1291. else{
  1292. tsp_noti_ignore = 1;
  1293. printk("[TSP] detached, but don't noti \n");
  1294. }
  1295. if(!tsp_noti_ignore)
  1296. tsp_charger_infom(0);
  1297. #endif
  1298. i2c_smbus_write_byte_data(usbsw->client, REG_CONTROL, CON_MASK);
  1299. usbsw->dev1 = 0;
  1300. usbsw->dev2 = 0;
  1301. usbsw->dev3 = 0;
  1302. usbsw->adc = 0;
  1303. usbsw->vbus = 0;
  1304. usbsw->carkit_dev = 0;
  1305. usbsw->attached_dev = ATTACHED_DEV_NONE_MUIC;
  1306. return 0;
  1307. }
  1308. static irqreturn_t sm5502_irq_thread(int irq, void *data)
  1309. {
  1310. struct sm5502_usbsw *usbsw = data;
  1311. struct i2c_client *client = usbsw->client;
  1312. int intr1, intr2;
  1313. int val1, val3, adc , vbus;
  1314. /* SM5502 : Read interrupt -> Read Device */
  1315. pr_info("sm5502_irq_thread is called\n");
  1316. mutex_lock(&usbsw->mutex);
  1317. intr1 = i2c_smbus_read_byte_data(client, REG_INT1);
  1318. intr2 = i2c_smbus_read_byte_data(client, REG_INT2);
  1319. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  1320. dev_info(&client->dev, "%s: intr1 : 0x%x,intr2 : 0x%x, adc : 0x%x\n",
  1321. __func__, intr1, intr2,adc);
  1322. /* MUIC OVP Check */
  1323. if (intr1 & INT_OVP_ENABLE)
  1324. usbsw->pdata->oxp_callback(ENABLE);
  1325. else if (intr1 & INT_OXP_DISABLE)
  1326. usbsw->pdata->oxp_callback(DISABLE);
  1327. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  1328. if((intr1 == 0x00 && (intr2 & 0x04))
  1329. || ((intr1 & INT_DETACH) && (intr2 & 0x04)))
  1330. sm5502_detect_lanhub(usbsw);
  1331. else
  1332. #endif
  1333. /* device detection */
  1334. /* interrupt both attach and detach */
  1335. if (intr1 == (INT_ATTACH + INT_DETACH)) {
  1336. val1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  1337. val3 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE3);
  1338. vbus = i2c_smbus_read_byte_data(client, REG_VBUSINVALID);
  1339. if ((adc == ADC_OPEN) && (val1 == DATA_NONE) &&
  1340. ((val3 == DATA_NONE) || (vbus == 0x00)))
  1341. sm5502_detach_dev(usbsw);
  1342. else
  1343. sm5502_attach_dev(usbsw);
  1344. }
  1345. /* interrupt attach */
  1346. else if ( (intr1 & INT_ATTACH) || (intr2 & INT_RESERVED_ATTACH ) )
  1347. sm5502_attach_dev(usbsw);
  1348. /* interrupt detach */
  1349. else if (intr1 & INT_DETACH)
  1350. sm5502_detach_dev(usbsw);
  1351. else if ((intr2 == INT_VBUSOUT_ON)) {
  1352. pr_info("sm5502: VBUSOUT_ON\n");
  1353. #ifdef CONFIG_USB_HOST_NOTIFY
  1354. sec_otg_notify(HNOTIFY_OTG_POWER_ON);
  1355. #endif
  1356. if(sec_get_notification(HNOTIFY_MODE) != NOTIFY_TEST_MODE){
  1357. /*JIG UART OFF VBUS Change or Sosche Charger*/
  1358. if((adc == ADC_JIG_UART_OFF) || (adc == ADC_OPEN))
  1359. sm5502_attach_dev(usbsw);
  1360. /*Deskdock VBUS Change scenario*/
  1361. else if(adc == ADC_DESKDOCK)
  1362. usbsw->pdata->callback(CABLE_TYPE_DESK_DOCK,SM5502_ATTACHED);
  1363. }
  1364. }
  1365. else if (intr2 == INT_VBUSOUT_OFF) {
  1366. pr_info("sm5502: VBUSOUT_OFF\n");
  1367. #ifdef CONFIG_USB_HOST_NOTIFY
  1368. sec_otg_notify(HNOTIFY_OTG_POWER_OFF);
  1369. #endif
  1370. if(sec_get_notification(HNOTIFY_MODE) != NOTIFY_TEST_MODE){
  1371. /*JIG UART OFF VBUS Change or Sosche Charger*/
  1372. if((adc == ADC_JIG_UART_OFF) || (adc == ADC_OPEN))
  1373. sm5502_detach_dev(usbsw);
  1374. /*Deskdock VBUS Change scenario*/
  1375. else if(adc == ADC_DESKDOCK)
  1376. usbsw->pdata->callback(CABLE_TYPE_DESK_DOCK_NO_VB,SM5502_DETACHED);
  1377. }
  1378. }
  1379. mutex_unlock(&usbsw->mutex);
  1380. pr_info("sm5502_irq_thread,end\n");
  1381. return IRQ_HANDLED;
  1382. }
  1383. static int sm5502_irq_init(struct sm5502_usbsw *usbsw)
  1384. {
  1385. struct i2c_client *client = usbsw->client;
  1386. int ret;
  1387. if (client->irq) {
  1388. ret = request_threaded_irq(client->irq, NULL,
  1389. sm5502_irq_thread, IRQF_TRIGGER_FALLING,
  1390. "sm5502 micro USB", usbsw);
  1391. if (ret) {
  1392. dev_err(&client->dev, "failed to reqeust IRQ\n");
  1393. return ret;
  1394. }
  1395. enable_irq_wake(client->irq);
  1396. }
  1397. return 0;
  1398. }
  1399. static void sm5502_init_detect(struct work_struct *work)
  1400. {
  1401. struct sm5502_usbsw *usbsw = container_of(work,
  1402. struct sm5502_usbsw, init_work.work);
  1403. int ret;
  1404. int int_reg1, int_reg2;
  1405. dev_info(&usbsw->client->dev, "%s\n", __func__);
  1406. mutex_lock(&usbsw->mutex);
  1407. sm5502_attach_dev(usbsw);
  1408. mutex_unlock(&usbsw->mutex);
  1409. ret = sm5502_irq_init(usbsw);
  1410. if (ret)
  1411. dev_info(&usbsw->client->dev,
  1412. "failed to enable irq init %s\n", __func__);
  1413. int_reg1 = sm5502_read_reg(usbsw->client, REG_INT1);
  1414. dev_info(&usbsw->client->dev, "%s: intr1 : 0x%x\n",
  1415. __func__, int_reg1);
  1416. int_reg2 = i2c_smbus_read_byte_data(usbsw->client, REG_INT2);
  1417. dev_info(&usbsw->client->dev, "%s: intr2 : 0x%x\n",
  1418. __func__, int_reg2);
  1419. }
  1420. #ifdef CONFIG_OF
  1421. static int sm5502_parse_dt(struct device *dev, struct sm5502_platform_data *pdata)
  1422. {
  1423. struct device_node *np = dev->of_node;
  1424. /*changes can be added later, when needed*/
  1425. #if 0
  1426. /* regulator info */
  1427. pdata->i2c_pull_up = of_property_read_bool(np, "sm5502,i2c-pull-up");
  1428. #endif
  1429. /* reset, irq gpio info */
  1430. pdata->gpio_scl = of_get_named_gpio_flags(np, "sm5502,gpio-scl",
  1431. 0, &pdata->scl_gpio_flags);
  1432. pdata->gpio_uart_on = of_get_named_gpio_flags(np, "sm5502,uarton-gpio",
  1433. 0, &pdata->uarton_gpio_flags);
  1434. pdata->gpio_sda = of_get_named_gpio_flags(np, "sm5502,gpio-sda",
  1435. 0, &pdata->sda_gpio_flags);
  1436. pdata->gpio_int = of_get_named_gpio_flags(np, "sm5502,irq-gpio",
  1437. 0, &pdata->irq_gpio_flags);
  1438. pr_info("%s: irq-gpio: %u \n", __func__, pdata->gpio_int);
  1439. return 0;
  1440. }
  1441. #endif
  1442. static int __devinit sm5502_probe(struct i2c_client *client,
  1443. const struct i2c_device_id *id)
  1444. {
  1445. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1446. struct sm5502_usbsw *usbsw;
  1447. int ret = 0;
  1448. struct device *switch_dev;
  1449. struct sm5502_platform_data *pdata;
  1450. dev_info(&client->dev,"%s:sm5502 probe called \n",__func__);
  1451. if(client->dev.of_node) {
  1452. pdata = devm_kzalloc(&client->dev,
  1453. sizeof(struct sm5502_platform_data), GFP_KERNEL);
  1454. if (!pdata) {
  1455. dev_err(&client->dev, "Failed to allocate memory \n");
  1456. return -ENOMEM;
  1457. }
  1458. ret = sm5502_parse_dt(&client->dev, pdata);
  1459. if (ret < 0) {
  1460. dev_err(&client->dev, "sm5502_parse_dt failed\n");
  1461. goto fail;
  1462. }
  1463. pdata->callback = sm5502_callback;
  1464. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  1465. pdata->lanhub_cb = sm5502_lanhub_callback;
  1466. #endif
  1467. pdata->dock_init = sm5502_dock_init;
  1468. pdata->oxp_callback = sm5502_oxp_callback;
  1469. pdata->mhl_sel = NULL;
  1470. #if defined(CONFIG_MACH_S3VE3G_EUR) || defined(CONFIG_MACH_VICTOR3GDSDTV_LTN)
  1471. gpio_tlmm_config(GPIO_CFG(pdata->gpio_int, 0, GPIO_CFG_INPUT,
  1472. GPIO_CFG_PULL_UP, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1473. #else
  1474. gpio_tlmm_config(GPIO_CFG(pdata->gpio_int, 0, GPIO_CFG_INPUT,
  1475. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1476. #endif
  1477. gpio_tlmm_config(GPIO_CFG(pdata->gpio_scl, 0, GPIO_CFG_INPUT,
  1478. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1479. gpio_tlmm_config(GPIO_CFG(pdata->gpio_sda, 0, GPIO_CFG_INPUT,
  1480. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1481. #if defined(CONFIG_MACH_S3VE3G_EUR) || defined(CONFIG_MACH_VICTOR3GDSDTV_LTN) || defined(CONFIG_MACH_ATLANTIC3GEUR_OPEN)
  1482. gpio_tlmm_config(GPIO_CFG(pdata->gpio_uart_on, 0, GPIO_CFG_INPUT,
  1483. GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  1484. #else
  1485. gpio_tlmm_config(GPIO_CFG(pdata->gpio_uart_on, 0, GPIO_CFG_INPUT,
  1486. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  1487. #endif
  1488. client->irq = gpio_to_irq(pdata->gpio_int);
  1489. } else {
  1490. pdata = client->dev.platform_data;
  1491. if (!pdata)
  1492. return -EINVAL;
  1493. }
  1494. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1495. dev_err(&client->dev, "i2c functionality check failed...!\n");
  1496. ret = -EIO;
  1497. goto fail;
  1498. }
  1499. usbsw = kzalloc(sizeof(struct sm5502_usbsw), GFP_KERNEL);
  1500. if (!usbsw) {
  1501. dev_err(&client->dev, "failed to allocate driver data\n");
  1502. ret = -ENOMEM;
  1503. goto fail;
  1504. }
  1505. usbsw->client = client;
  1506. if (client->dev.of_node)
  1507. usbsw->pdata = pdata;
  1508. else
  1509. usbsw->pdata = client->dev.platform_data;
  1510. if (!usbsw->pdata)
  1511. goto fail1;
  1512. i2c_set_clientdata(client, usbsw);
  1513. mutex_init(&usbsw->mutex);
  1514. local_usbsw = usbsw;
  1515. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1516. ret = sm5502_read_reg(client,REG_MANUAL_SW1);
  1517. if(ret<0) {
  1518. dev_err(&client->dev, "failed to read MANUAL SW1 Reg, err:%d\n",ret);
  1519. }
  1520. /* Keep the feature disabled by default */
  1521. usbsw->is_rustproof = false;
  1522. /* RUSTPROOF: disable UART connection if MANSW1 from BL is OPEN_RUSTPROOF*/
  1523. if(ret == MANSW1_OPEN_RUSTPROOF)
  1524. usbsw->is_rustproof = true;
  1525. #endif
  1526. /* SM5502 muic soft reset */
  1527. ret = sm5502_write_reg(client, REG_RESET, 0x01);
  1528. if(ret < 0) {
  1529. dev_err(&client->dev,"failed to Soft reset MUIC, err:%d\n",ret);
  1530. }
  1531. sm5502_reg_init(usbsw);
  1532. ret = sysfs_create_group(&client->dev.kobj, &sm5502_group);
  1533. if (ret) {
  1534. dev_err(&client->dev,
  1535. "failed to create sm5502 attribute group\n");
  1536. goto fail2;
  1537. }
  1538. /* make sysfs node /sys/class/sec/switch/ */
  1539. switch_dev = device_create(sec_class, NULL, 0, NULL, "switch");
  1540. if (IS_ERR(switch_dev)) {
  1541. pr_err("[SM5502] Failed to create device (switch_dev)!\n");
  1542. ret = PTR_ERR(switch_dev);
  1543. goto fail2;
  1544. }
  1545. ret = device_create_file(switch_dev, &dev_attr_usb_state);
  1546. if (ret < 0) {
  1547. pr_err("[SM5502] Failed to create file (usb_state)!\n");
  1548. goto err_create_file_state;
  1549. }
  1550. ret = device_create_file(switch_dev, &dev_attr_adc);
  1551. if (ret < 0) {
  1552. pr_err("[SM5502] Failed to create file (adc)!\n");
  1553. goto err_create_file_adc;
  1554. }
  1555. ret = device_create_file(switch_dev, &dev_attr_reset_switch);
  1556. if (ret < 0) {
  1557. pr_err("[SM5502] Failed to create file (reset_switch)!\n");
  1558. goto err_create_file_reset_switch;
  1559. }
  1560. ret = device_create_file(switch_dev, &dev_attr_attached_dev);
  1561. if (ret < 0) {
  1562. pr_err("[SM5502] Failed to create file (attached_dev)!\n");
  1563. goto err_create_file_attached_dev;
  1564. }
  1565. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1566. ret = device_create_file(switch_dev, &dev_attr_uart_en);
  1567. if (ret < 0) {
  1568. pr_err("[SM5502] Failed to create file (uart_en)!\n");
  1569. goto err_create_file_uart_en;
  1570. }
  1571. ret = device_create_file(switch_dev, &dev_attr_uart_sel);
  1572. if (ret < 0) {
  1573. pr_err("[SM5502] Failed to create file (uart_sel)!\n");
  1574. goto err_create_file_uart_sel;
  1575. }
  1576. ret = device_create_file(switch_dev, &dev_attr_usb_sel);
  1577. if (ret < 0) {
  1578. pr_err("[SM5502] Failed to create file (usb_sel)!\n");
  1579. goto err_create_file_usb_sel;
  1580. }
  1581. #endif
  1582. local_usbsw->attached_dev = ATTACHED_DEV_NONE_MUIC;
  1583. #if defined(CONFIG_MUIC_SM5502_SUPPORT_LANHUB_TA)
  1584. local_usbsw->previous_dock = SM5502_NONE;
  1585. local_usbsw->lanhub_ta_status = 0;
  1586. #endif
  1587. dev_set_drvdata(switch_dev, usbsw);
  1588. /* sm5502 dock init*/
  1589. if (usbsw->pdata->dock_init)
  1590. usbsw->pdata->dock_init();
  1591. /* initial cable detection */
  1592. INIT_DELAYED_WORK(&usbsw->init_work, sm5502_init_detect);
  1593. schedule_delayed_work(&usbsw->init_work, msecs_to_jiffies(2700));
  1594. return 0;
  1595. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1596. err_create_file_usb_sel:
  1597. device_remove_file(switch_dev, &dev_attr_usb_sel);
  1598. err_create_file_uart_sel:
  1599. device_remove_file(switch_dev, &dev_attr_uart_sel);
  1600. err_create_file_uart_en:
  1601. device_remove_file(switch_dev, &dev_attr_uart_en);
  1602. #endif
  1603. err_create_file_attached_dev:
  1604. device_remove_file(switch_dev, &dev_attr_attached_dev);
  1605. err_create_file_reset_switch:
  1606. device_remove_file(switch_dev, &dev_attr_reset_switch);
  1607. err_create_file_adc:
  1608. device_remove_file(switch_dev, &dev_attr_adc);
  1609. err_create_file_state:
  1610. device_remove_file(switch_dev, &dev_attr_usb_state);
  1611. fail2:
  1612. if (client->irq)
  1613. free_irq(client->irq, usbsw);
  1614. mutex_destroy(&usbsw->mutex);
  1615. i2c_set_clientdata(client, NULL);
  1616. fail1:
  1617. kfree(usbsw);
  1618. fail:
  1619. kfree(pdata);
  1620. return ret;
  1621. }
  1622. static int __devexit sm5502_remove(struct i2c_client *client)
  1623. {
  1624. struct sm5502_usbsw *usbsw = i2c_get_clientdata(client);
  1625. cancel_delayed_work(&usbsw->init_work);
  1626. if (client->irq) {
  1627. disable_irq_wake(client->irq);
  1628. free_irq(client->irq, usbsw);
  1629. }
  1630. mutex_destroy(&usbsw->mutex);
  1631. i2c_set_clientdata(client, NULL);
  1632. sysfs_remove_group(&client->dev.kobj, &sm5502_group);
  1633. kfree(usbsw);
  1634. return 0;
  1635. }
  1636. static int sm5502_resume(struct i2c_client *client)
  1637. {
  1638. struct sm5502_usbsw *usbsw = i2c_get_clientdata(client);
  1639. int ldev1,ldev2,ldev3;
  1640. pr_info("%s: resume \n",__func__);
  1641. ldev1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  1642. if (ldev1 < 0)
  1643. {
  1644. pr_err("%s: Dev reg 1 read err! %d \n",__func__, ldev1);
  1645. goto safe_exit;
  1646. }
  1647. ldev2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  1648. if (ldev2 < 0)
  1649. {
  1650. pr_err("%s: Dev reg 2 read err! %d \n",__func__, ldev2);
  1651. goto safe_exit;
  1652. }
  1653. ldev3 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE3);
  1654. if (ldev3 < 0)
  1655. {
  1656. pr_err("%s: Dev reg 3 read err! %d \n",__func__, ldev3);
  1657. goto safe_exit;
  1658. }
  1659. i2c_smbus_read_byte_data(client, REG_INT1);
  1660. i2c_smbus_read_byte_data(client, REG_INT2);
  1661. if (usbsw->dev1 != ldev1 || usbsw->dev2 != ldev2 || usbsw->dev3 != ldev3)
  1662. {
  1663. /* device detection */
  1664. mutex_lock(&usbsw->mutex);
  1665. sm5502_attach_dev(usbsw);
  1666. mutex_unlock(&usbsw->mutex);
  1667. }
  1668. safe_exit:
  1669. return 0;
  1670. }
  1671. static const struct i2c_device_id sm5502_id[] = {
  1672. {"sm5502", 0},
  1673. {}
  1674. };
  1675. MODULE_DEVICE_TABLE(i2c, sm5502_id);
  1676. static struct of_device_id sm5502_i2c_match_table[] = {
  1677. { .compatible = "sm5502,i2c",},
  1678. {},
  1679. };
  1680. MODULE_DEVICE_TABLE(of, sm5502_i2c_match_table);
  1681. static struct i2c_driver sm5502_i2c_driver = {
  1682. .driver = {
  1683. .name = "sm5502",
  1684. .owner = THIS_MODULE,
  1685. .of_match_table = sm5502_i2c_match_table,
  1686. },
  1687. .probe = sm5502_probe,
  1688. .remove = __devexit_p(sm5502_remove),
  1689. .resume = sm5502_resume,
  1690. .id_table = sm5502_id,
  1691. };
  1692. static int __init sm5502_init(void)
  1693. {
  1694. #if defined(CONFIG_MACH_S3VE_CHN_OPEN) || defined(CONFIG_MACH_S3VE_CHN_CMCC)
  1695. if (system_rev < 3)
  1696. return 0;
  1697. #endif
  1698. return i2c_add_driver(&sm5502_i2c_driver);
  1699. }
  1700. /*
  1701. Late init call is required MUIC was accessing
  1702. USB driver before USB initialization and watch dog reset
  1703. was happening when booted with USB connected*/
  1704. #if defined(CONFIG_TORCH_FIX)
  1705. module_init(sm5502_init);
  1706. #else
  1707. late_initcall(sm5502_init);
  1708. #endif
  1709. static void __exit sm5502_exit(void)
  1710. {
  1711. i2c_del_driver(&sm5502_i2c_driver);
  1712. }
  1713. module_exit(sm5502_exit);
  1714. MODULE_AUTHOR("Jeongrae Kim <Jryu.kim@samsung.com>");
  1715. MODULE_DESCRIPTION("SM5502 Micro USB Switch driver");
  1716. MODULE_LICENSE("GPL");