sm5504.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /*
  2. * driver/misc/sm5504.c - SM5504 micro USB switch device driver
  3. *
  4. * Copyright (C) 2014 Samsung Electronics
  5. * Nitin Chaudhary <nc.chaudhary@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. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/err.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c/sm5504.h>
  27. #include <linux/irq.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/gpio.h>
  32. #include <linux/delay.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/mfd/pmic8058.h>
  35. #include <linux/input.h>
  36. #include <linux/switch.h>
  37. #if defined (CONFIG_OF)
  38. #include <linux/of_device.h>
  39. #include <linux/of_gpio.h>
  40. #endif
  41. #ifdef CONFIG_USB_HOST_NOTIFY
  42. #include <linux/host_notify.h>
  43. #endif
  44. /* spmi control */
  45. extern int spmi_ext_register_writel_extra(u8 sid, u16 ad, u8 *buf, int len);
  46. extern int spmi_ext_register_readl_extra(u8 sid, u16 ad, u8 *buf, int len);
  47. extern int system_rev;
  48. static int bcd_scan = 0; // L Cable check
  49. #define INT_MASK1 0xFC
  50. #define INT_MASK2 0xFC
  51. /* DEVICE ID */
  52. #define SM5504_DEV_ID 0x01
  53. #define SM5504_DEV_ID_REV 0x00
  54. /* SM5504 I2C registers */
  55. #define REG_DEVICE_ID 0x01
  56. #define REG_CONTROL 0x02
  57. #define REG_INT1 0x03
  58. #define REG_INT2 0x04
  59. #define REG_INT_MASK1 0x05
  60. #define REG_INT_MASK2 0x06
  61. #define REG_ADC 0x07
  62. #define REG_DEVICE_TYPE1 0x0a
  63. #define REG_DEVICE_TYPE2 0x0b
  64. #define REG_MANUAL_SW1 0x13
  65. #define REG_MANUAL_SW2 0x14
  66. #define REG_RESET 0x1B
  67. #define REG_RESERVED_1 0x20
  68. #define REG_CHG_TYPE 0x24
  69. #define DATA_NONE 0x00
  70. /* Control */
  71. #define CON_ADC_EN (1 << 7)
  72. #define CON_USBCHDEN (1 << 6)
  73. #define CON_CHGTYP (1 << 5)
  74. #define CON_SWITCH_OPEN (1 << 4)
  75. #define CON_MANUAL_SW (1 << 2)
  76. #define CON_INT_MASK (1 << 0)
  77. #define CON_MASK (CON_USBCHDEN | CON_CHGTYP | \
  78. CON_MANUAL_SW | CON_ADC_EN)
  79. #define CON_ADC_MASK (CON_USBCHDEN | CON_CHGTYP | \
  80. CON_MANUAL_SW)
  81. /* Device Type 1 */
  82. #define DEV_DEDICATED_CHG (1 << 6)
  83. #define DEV_USB_CHG (1 << 5)
  84. #define DEV_CAR_KIT (1 << 4)
  85. #define DEV_UART (1 << 3)
  86. #define DEV_USB (1 << 2)
  87. #define DEV_USB_OTG (1 << 0)
  88. #define DEV_T1_USB_MASK (DEV_USB_OTG | DEV_USB_CHG | DEV_USB)
  89. #define DEV_T1_UART_MASK (DEV_UART)
  90. #define DEV_T1_CHARGER_MASK (DEV_DEDICATED_CHG | DEV_CAR_KIT)
  91. #define MANSW1_OPEN_RUSTPROOF ((0x0 << 5)| (0x3 << 2) |(1 << 0))
  92. /* Charger Type Register */
  93. #define CHG_SDP_TIMEOUT (1 << 3)
  94. #define CHG_TYPE_SDP (1 << 2)
  95. #define CHG_TYPE_CDP (1 << 1)
  96. #define CHG_TYPE_DCP (1 << 0)
  97. #define DEV_T3_CHARGER_MASK (CHG_TYPE_SDP | CHG_TYPE_CDP | \
  98. CHG_TYPE_DCP)
  99. #define DEV_LCABLE_MASK (CHG_TYPE_DCP | CHG_TYPE_SDP)
  100. #define DEV_LANHUB (1 << 9)
  101. #define DEV_AUDIO_DOCK (1 << 8)
  102. /* Device Type 2 */
  103. #define DEV_JIG_UART_OFF (1 << 3)
  104. #define DEV_JIG_UART_ON (1 << 2)
  105. #define DEV_JIG_USB_OFF (1 << 1)
  106. #define DEV_JIG_USB_ON (1 << 0)
  107. #define DEV_T2_USB_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON)
  108. #define DEV_T2_UART_MASK (DEV_JIG_UART_OFF)
  109. #define DEV_T2_JIG_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
  110. DEV_JIG_UART_OFF)
  111. #define DEV_T2_JIG_ALL_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
  112. DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
  113. /*
  114. * Manual Switch 1
  115. * D- [7:5] / D+ [4:2]
  116. * 000: Open all / 001: USB / 011: UART
  117. */
  118. #define SW_UART ((3 << 5) | (3 << 2))
  119. #define SW_DHOST ((1 << 5) | (1 << 2) | (1 << 0))
  120. #define SW_AUTO ((0 << 5) | (0 << 2))
  121. #define SW_USB_OPEN (1 << 0)
  122. #define SW_ALL_OPEN (0)
  123. #define SW_ALL_OPEN_WITH_VBUS ((0 << 5) | (0 << 2) | (1 << 0))
  124. /* Interrupt 1 */
  125. #define INT_ADC_CHANGE (1 << 6)
  126. #define INT_CONNECT (1 << 5)
  127. #define INT_OVP (1 << 4)
  128. #define INT_DCD_OUT (1 << 3)
  129. #define INT_CHGDET (1 << 2)
  130. #define INT_DETACH (1 << 1)
  131. #define INT_ATTACH (1 << 0)
  132. /* Interrupt 2 */
  133. #define INT_OVP_OCP_EVENT (1 << 7)
  134. #define INT_OCP_EVENT (1 << 6)
  135. #define INT_VBUS_OCP (1 << 5)
  136. #define INT_VBUS_OVP (1 << 4)
  137. #define INT_POR (1 << 2)
  138. #define INT_VBUS_INVALID (1 << 1)
  139. #define INT_RID_CHARGER (1 << 0)
  140. /* ADC VALUE */
  141. #define ADC_OTG 0x00
  142. #define ADC_MHL 0x01
  143. #define ADC_VZW_DOCK 0x0E
  144. #define ADC_VZW_INCOMPATIBLE 0x0F
  145. #define ADC_SMART_DOCK 0x10
  146. #define ADC_HMT 0x11
  147. #define ADC_AUDIO_DOCK 0x12
  148. #define ADC_LANHUB 0x13
  149. #define ADC_CHARGING_CABLE 0x14
  150. #define ADC_MPOS 0x15
  151. #define ADC_UART 0x16
  152. #define ADC_LCABLE 0x17
  153. #define ADC_JIG_USB_OFF 0x18
  154. #define ADC_JIG_USB_ON 0x19
  155. #define ADC_DESKDOCK 0x1a
  156. #define ADC_CEA2 0x1b
  157. #define ADC_JIG_UART_OFF 0x1c
  158. #define ADC_JIG_UART_ON 0x1d
  159. #define ADC_CARDOCK 0x1d
  160. #define ADC_OPEN 0x1f
  161. int uart_sm5504_connecting;
  162. EXPORT_SYMBOL(uart_sm5504_connecting);
  163. int detached_sm5504_status;
  164. EXPORT_SYMBOL(detached_sm5504_status);
  165. static int jig_state;
  166. struct sm5504_usbsw {
  167. struct i2c_client *client;
  168. struct sm5504_platform_data *pdata;
  169. int dev1;
  170. int dev2;
  171. int dev3;
  172. int mansw;
  173. int vbus;
  174. int dock_attached;
  175. int dev_id;
  176. struct delayed_work init_work;
  177. struct mutex mutex;
  178. int adc;
  179. bool incomp_attached;
  180. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  181. unsigned int previous_dock;
  182. unsigned int lanhub_ta_status;
  183. #endif
  184. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  185. bool is_rustproof;
  186. #endif
  187. };
  188. static struct sm5504_usbsw *local_usbsw;
  189. static int sm5504_attach_dev(struct sm5504_usbsw *usbsw);
  190. static int sm5504_detach_dev(struct sm5504_usbsw *usbsw);
  191. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  192. bool lanhub_ta_case = false;
  193. /* RAW DATA Detection*/
  194. static void sm5504_enable_rawdataInterrupts(struct sm5504_usbsw *usbsw)
  195. {
  196. struct i2c_client *client = usbsw->client;
  197. u8 value, ret;
  198. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  199. value &= 0xF7; /*Control Register Bit 4 set to 0 to enable RAW Data INTR*/
  200. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, value);
  201. pr_info("%s:set CONTROL value to 0x%x\n", __func__, value);
  202. if (ret < 0)
  203. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  204. }
  205. static void sm5504_disable_rawdataInterrupts(struct sm5504_usbsw *usbsw)
  206. {
  207. struct i2c_client *client = local_usbsw->client;
  208. int value, ret;
  209. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  210. value |= 0x08; /*Control Register Bit 4 set to 0 to enable RAW Data INTR*/
  211. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, value);
  212. pr_info("%s:set CONTROL value to 0x%x \n", __func__, value);
  213. if (ret < 0)
  214. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  215. }
  216. #endif
  217. static int sm5504_write_reg(struct i2c_client *client, int reg, int val)
  218. {
  219. int ret;
  220. ret = i2c_smbus_write_byte_data(client, reg, val);
  221. if (ret < 0)
  222. {
  223. dev_err(&client->dev,
  224. "%s, i2c write error %d\n",__func__, ret);
  225. }
  226. return ret;
  227. }
  228. static int sm5504_read_reg(struct i2c_client *client, int reg)
  229. {
  230. int ret;
  231. ret = i2c_smbus_read_byte_data(client, reg);
  232. if (ret < 0)
  233. {
  234. dev_err(&client->dev,
  235. "%s, i2c read error %d\n",__func__, ret);
  236. }
  237. return ret;
  238. }
  239. #if defined(CONFIG_SEC_FACTORY) || defined(CONFIG_USB_HOST_NOTIFY)
  240. static void sm5504_dock_control(struct sm5504_usbsw *usbsw,
  241. int dock_type, int state, int path)
  242. {
  243. struct i2c_client *client = usbsw->client;
  244. struct sm5504_platform_data *pdata = usbsw->pdata;
  245. int ret;
  246. if (state) {
  247. usbsw->mansw = path;
  248. pdata->callback(dock_type, state);
  249. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, path);
  250. if (ret < 0)
  251. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  252. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  253. if (ret < 0)
  254. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  255. else {
  256. ret = i2c_smbus_write_byte_data(client,
  257. REG_CONTROL, ret & ~CON_MANUAL_SW);
  258. }
  259. if (ret < 0)
  260. dev_err(&client->dev, "%s: err %x\n", __func__, ret);
  261. } else {
  262. pdata->callback(dock_type, state);
  263. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  264. if (ret < 0)
  265. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  266. ret = i2c_smbus_write_byte_data(client, REG_CONTROL,
  267. ret | CON_MANUAL_SW);
  268. if (ret < 0)
  269. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  270. }
  271. }
  272. #endif
  273. static void sm5504_reg_init(struct sm5504_usbsw *usbsw)
  274. {
  275. struct i2c_client *client = usbsw->client;
  276. unsigned int ctrl = CON_MASK;
  277. int ret;
  278. pr_info("sm5504_reg_init is called\n");
  279. usbsw->dev_id = i2c_smbus_read_byte_data(client, REG_DEVICE_ID);
  280. local_usbsw->dev_id = usbsw->dev_id;
  281. if (usbsw->dev_id < 0)
  282. dev_err(&client->dev, "%s: err %d\n", __func__, usbsw->dev_id);
  283. dev_info(&client->dev, " sm5504_reg_init dev ID: 0x%x\n",
  284. usbsw->dev_id);
  285. ret = i2c_smbus_write_byte_data(client, REG_INT_MASK1, INT_MASK1);
  286. if (ret < 0)
  287. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  288. ret = i2c_smbus_write_byte_data(client, REG_INT_MASK2, INT_MASK2);
  289. if (ret < 0)
  290. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  291. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ctrl);
  292. if (ret < 0)
  293. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  294. /* Single scan for JIG Accessory */
  295. ret = i2c_smbus_write_byte_data(client, REG_RESERVED_1, 0x06);
  296. if (ret < 0)
  297. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  298. /*Manual SW2 bit2 : JIG_ON '1' */
  299. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x04);
  300. if (ret < 0)
  301. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  302. bcd_scan = 0;
  303. }
  304. static ssize_t sm5504_show_control(struct device *dev,
  305. struct device_attribute *attr,
  306. char *buf)
  307. {
  308. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  309. struct i2c_client *client = usbsw->client;
  310. int value;
  311. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  312. if (value < 0)
  313. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  314. return snprintf(buf, 13, "CONTROL: %02x\n", value);
  315. }
  316. static ssize_t sm5504_show_device_type(struct device *dev,
  317. struct device_attribute *attr,
  318. char *buf)
  319. {
  320. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  321. struct i2c_client *client = usbsw->client;
  322. int value;
  323. value = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  324. if (value < 0)
  325. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  326. return snprintf(buf, 11, "DEV_TYP %02x\n", value);
  327. }
  328. static ssize_t sm5504_show_manualsw(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  332. struct i2c_client *client = usbsw->client;
  333. int value;
  334. value = i2c_smbus_read_byte_data(client, REG_MANUAL_SW1);
  335. if (value < 0)
  336. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  337. if (value == SW_UART)
  338. return snprintf(buf, 5, "UART\n");
  339. else if (value == SW_DHOST)
  340. return snprintf(buf, 6, "DHOST\n");
  341. else if (value == SW_AUTO)
  342. return snprintf(buf, 5, "AUTO\n");
  343. else
  344. return snprintf(buf, 4, "%x", value);
  345. }
  346. static ssize_t sm5504_set_manualsw(struct device *dev,
  347. struct device_attribute *attr,
  348. const char *buf, size_t count)
  349. {
  350. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  351. struct i2c_client *client = usbsw->client;
  352. int value, ret;
  353. unsigned int path = 0;
  354. value = i2c_smbus_read_byte_data(client, REG_CONTROL);
  355. if (value < 0)
  356. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  357. if ((value & ~CON_MANUAL_SW) !=
  358. (CON_SWITCH_OPEN))
  359. return 0;
  360. if (!strncmp(buf, "UART", 4)) {
  361. path = SW_UART;
  362. value &= ~CON_MANUAL_SW;
  363. } else if (!strncmp(buf, "DHOST", 5)) {
  364. path = SW_DHOST;
  365. value &= ~CON_MANUAL_SW;
  366. } else if (!strncmp(buf, "AUTO", 4)) {
  367. path = SW_AUTO;
  368. value |= CON_MANUAL_SW;
  369. } else {
  370. dev_err(dev, "Wrong command\n");
  371. return 0;
  372. }
  373. usbsw->mansw = path;
  374. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, path);
  375. if (ret < 0)
  376. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  377. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, value);
  378. if (ret < 0)
  379. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  380. return count;
  381. }
  382. static ssize_t sm5504_show_usb_state(struct device *dev,
  383. struct device_attribute *attr,
  384. char *buf)
  385. {
  386. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  387. struct i2c_client *client = usbsw->client;
  388. int device_type1, device_type2;
  389. device_type1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  390. if (device_type1 < 0) {
  391. dev_err(&client->dev, "%s: err %d ", __func__, device_type1);
  392. return (ssize_t)device_type1;
  393. }
  394. device_type2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  395. if (device_type2 < 0) {
  396. dev_err(&client->dev, "%s: err %d ", __func__, device_type2);
  397. return (ssize_t)device_type2;
  398. }
  399. if (device_type1 & DEV_T1_USB_MASK || device_type2 & DEV_T2_USB_MASK)
  400. return snprintf(buf, 22, "USB_STATE_CONFIGURED\n");
  401. return snprintf(buf, 25, "USB_STATE_NOTCONFIGURED\n");
  402. }
  403. static ssize_t sm5504_show_adc(struct device *dev,
  404. struct device_attribute *attr,
  405. char *buf)
  406. {
  407. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  408. struct i2c_client *client = usbsw->client;
  409. int adc;
  410. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  411. if (adc < 0) {
  412. dev_err(&client->dev,
  413. "%s: err at read adc %d\n", __func__, adc);
  414. return snprintf(buf, 9, "UNKNOWN\n");
  415. }
  416. return snprintf(buf, 4, "%x\n", adc);
  417. }
  418. static ssize_t sm5504_reset(struct device *dev,
  419. struct device_attribute *attr,
  420. const char *buf, size_t count)
  421. {
  422. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  423. struct i2c_client *client = usbsw->client;
  424. if (!strncmp(buf, "1", 1)) {
  425. dev_info(&client->dev,
  426. "sm5504 reset after delay 1000 msec.\n");
  427. msleep(1000);
  428. sm5504_write_reg(client, REG_RESET, 0x01);
  429. dev_info(&client->dev, "sm5504_reset_control done!\n");
  430. } else {
  431. dev_info(&client->dev,
  432. "sm5504_reset_control, but not reset_value!\n");
  433. }
  434. #ifdef CONFIG_MUIC_SUPPORT_RUSTPROOF
  435. usbsw->is_rustproof = false;
  436. #endif
  437. sm5504_reg_init(usbsw);
  438. return count;
  439. }
  440. #ifdef CONFIG_MUIC_SUPPORT_RUSTPROOF
  441. static void muic_rustproof_feature(struct i2c_client *client, int state);
  442. /* Keystring "*#0*#" sysfs implementation */
  443. static ssize_t uart_en_show(struct device *dev,
  444. struct device_attribute *attr, char *buf)
  445. {
  446. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  447. /*is_rustproof is false then UART can be enabled*/
  448. return snprintf(buf, 4, "%d\n", !(usbsw->is_rustproof));
  449. }
  450. static ssize_t uart_en_store(struct device *dev,
  451. struct device_attribute *attr,
  452. const char *buf, size_t size)
  453. {
  454. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  455. struct i2c_client *client = usbsw->client;
  456. if (!strncmp(buf, "1", 1)) {
  457. dev_info(&client->dev,
  458. "[MUIC]Runtime enabling the UART.\n");
  459. usbsw->is_rustproof = false;
  460. muic_rustproof_feature(client,SM5504_DETACHED);
  461. } else {
  462. dev_info(&client->dev,
  463. "[MUIC]Runtime disabling the UART.\n");
  464. usbsw->is_rustproof = true;
  465. }
  466. /* reinvoke the attach detection function to set proper paths */
  467. sm5504_attach_dev(usbsw);
  468. return size;
  469. }
  470. static DEVICE_ATTR(uart_en, S_IRUGO | S_IWUSR ,
  471. uart_en_show, uart_en_store);
  472. static ssize_t uart_sel_show(struct device *dev,
  473. struct device_attribute *attr, char *buf)
  474. {
  475. /*for sm5504 paths are always switch to AP*/
  476. return snprintf(buf, 3, "AP\n");
  477. }
  478. static ssize_t uart_sel_store(struct device *dev,
  479. struct device_attribute *attr,
  480. const char *buf, size_t size)
  481. {
  482. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  483. struct i2c_client *client = usbsw->client;
  484. dev_info(&client->dev,"[MUIC]Enabling AP UART Path, dummy Call\n");
  485. return size;
  486. }
  487. static DEVICE_ATTR(uart_sel, S_IRUGO | S_IWUSR ,
  488. uart_sel_show, uart_sel_store);
  489. static ssize_t usbsel_show(struct device *dev,
  490. struct device_attribute *attr, char *buf)
  491. {
  492. return snprintf(buf, 4, "PDA\n");
  493. }
  494. static ssize_t usbsel_store(struct device *dev,
  495. struct device_attribute *attr,
  496. const char *buf, size_t size)
  497. {
  498. struct sm5504_usbsw *usbsw = dev_get_drvdata(dev);
  499. struct i2c_client *client = usbsw->client;
  500. dev_info(&client->dev,"[MUIC]Enabling AP UART Path, dummy Call\n");
  501. return size;
  502. }
  503. static DEVICE_ATTR(usb_sel, S_IRUGO | S_IWUSR ,
  504. usbsel_show, usbsel_store);
  505. #endif
  506. static DEVICE_ATTR(control, S_IRUGO, sm5504_show_control, NULL);
  507. static DEVICE_ATTR(device_type, S_IRUGO, sm5504_show_device_type, NULL);
  508. static DEVICE_ATTR(switch, S_IRUGO | S_IWUSR,
  509. sm5504_show_manualsw, sm5504_set_manualsw);
  510. static DEVICE_ATTR(usb_state, S_IRUGO, sm5504_show_usb_state, NULL);
  511. static DEVICE_ATTR(adc, S_IRUGO, sm5504_show_adc, NULL);
  512. static DEVICE_ATTR(reset_switch, S_IWUSR | S_IWGRP, NULL, sm5504_reset);
  513. static struct attribute *sm5504_attributes[] = {
  514. &dev_attr_control.attr,
  515. &dev_attr_device_type.attr,
  516. &dev_attr_switch.attr,
  517. NULL
  518. };
  519. static const struct attribute_group sm5504_group = {
  520. .attrs = sm5504_attributes,
  521. };
  522. #if defined(CONFIG_USB_HOST_NOTIFY)
  523. static void sm5504_set_otg(struct sm5504_usbsw *usbsw, int state)
  524. {
  525. int ret;
  526. struct i2c_client *client = usbsw->client;
  527. if (state == SM5504_ATTACHED) {
  528. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, 0x25);
  529. if (ret < 0)
  530. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  531. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00); //Disconnecting the MUIC_ID & ITBP Pins
  532. if (ret < 0)
  533. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  534. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  535. if (ret < 0)
  536. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  537. ret = ret & 0xFB; /*Manual Connection S/W enable*/
  538. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  539. if (ret < 0)
  540. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  541. } else {
  542. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00);
  543. if (ret < 0)
  544. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  545. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,
  546. SW_ALL_OPEN);
  547. if (ret < 0)
  548. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  549. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  550. if (ret < 0)
  551. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  552. ret = ret | 0x04; /*Manual Connection S/W Disable*/
  553. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  554. if (ret < 0)
  555. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  556. }
  557. }
  558. #endif
  559. #if defined(CONFIG_VIDEO_MHL_V2)
  560. int dock_det(void)
  561. {
  562. return local_usbsw->dock_attached;
  563. }
  564. EXPORT_SYMBOL(dock_det);
  565. #endif
  566. int check_sm5504_jig_state(void)
  567. {
  568. return jig_state;
  569. }
  570. EXPORT_SYMBOL(check_sm5504_jig_state);
  571. #if defined(CONFIG_TOUCHSCREEN_IST30XX)
  572. extern void charger_enable(int enable);
  573. #endif
  574. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  575. extern void tsp_charger_infom(bool en);
  576. #endif
  577. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  578. static void sm5504_set_lanhub(struct sm5504_usbsw *usbsw, int state)
  579. {
  580. int ret;
  581. struct i2c_client *client = usbsw->client;
  582. if (state == SM5504_ATTACHED) {
  583. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, 0x25);
  584. if (ret < 0)
  585. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  586. /*Disconnect the ITBP & MUIC_ID Pins*/
  587. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00);
  588. if (ret < 0)
  589. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  590. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  591. if (ret < 0)
  592. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  593. ret = ret & 0xFB; /*Manual Connection S/W enable*/
  594. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  595. if (ret < 0)
  596. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  597. } else {
  598. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00);
  599. if (ret < 0)
  600. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  601. ret = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,
  602. SW_ALL_OPEN);
  603. if (ret < 0)
  604. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  605. ret = i2c_smbus_read_byte_data(client, REG_CONTROL);
  606. if (ret < 0)
  607. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  608. ret = ret | 0x04; /*Manual Connection S/W enable*/
  609. ret = i2c_smbus_write_byte_data(client, REG_CONTROL, ret);
  610. if (ret < 0)
  611. dev_info(&client->dev, "%s: err %d\n", __func__, ret);
  612. }
  613. }
  614. static void sm5504_mask_vbus_detect(struct sm5504_usbsw *usbsw, int state){
  615. unsigned int value;
  616. struct i2c_client *client = usbsw->client;
  617. if(state == SM5504_ATTACHED) {
  618. pr_info("%s called, state: (%d)\n",__func__,state);
  619. /*Need to disable the vbus change interrupts*/
  620. value = i2c_smbus_read_byte_data(client,REG_INT_MASK2);
  621. if (value < 0)
  622. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  623. value |= 0x01;
  624. value = i2c_smbus_write_byte_data(client,REG_INT_MASK2,value);
  625. if (value < 0)
  626. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  627. }
  628. else {
  629. pr_info("%s called, state: (%d)\n",__func__,state);
  630. value = i2c_smbus_write_byte_data(client,REG_INT_MASK2,INT_MASK2);
  631. if (value < 0)
  632. dev_err(&client->dev, "%s: err %d\n", __func__, value);
  633. }
  634. }
  635. static int sm5504_detect_lanhub(struct sm5504_usbsw *usbsw) {
  636. unsigned int dev1, dev2, adc;
  637. struct sm5504_platform_data *pdata = usbsw->pdata;
  638. struct i2c_client *client = usbsw->client;
  639. pr_info("%s called\n",__func__);
  640. dev1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  641. if (dev1 < 0) {
  642. dev_err(&client->dev, "%s: err %d\n", __func__, dev1);
  643. return dev1;
  644. }
  645. dev2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  646. if (dev2 < 0) {
  647. dev_err(&client->dev, "%s: err %d\n", __func__, dev2);
  648. return dev2;
  649. }
  650. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  651. dev_info(&client->dev, "dev1: 0x%02x, dev2: 0x%02x, adc: 0x%02x\n",
  652. dev1, dev2, adc);
  653. /* Attached + Detached*/
  654. switch(adc){
  655. case ADC_OTG:
  656. lanhub_ta_case = false;
  657. usbsw->adc = adc;
  658. sm5504_set_otg(usbsw, SM5504_ATTACHED);
  659. if(usbsw->previous_dock == SM5504_NONE) {
  660. dev_info(&client->dev, "%s:LANHUB Connected\n", __func__);
  661. pdata->callback(CABLE_TYPE_OTG, SM5504_ATTACHED);
  662. sm5504_set_otg(usbsw,SM5504_ATTACHED);
  663. }
  664. else if(usbsw->previous_dock == ADC_LANHUB) {
  665. dev_info(&client->dev, "%s:Switch LANHUB+TA to LANHUB\n", __func__);
  666. usbsw->lanhub_ta_status=0;
  667. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5504_DETACHED, LANHUB_TA);
  668. }
  669. sm5504_mask_vbus_detect(usbsw,SM5504_DETACHED);
  670. usbsw->dock_attached = SM5504_ATTACHED;
  671. usbsw->previous_dock = ADC_OTG;
  672. break;
  673. case ADC_LANHUB:
  674. usbsw->adc = adc;
  675. lanhub_ta_case = true;
  676. usbsw->lanhub_ta_status = 1;
  677. sm5504_mask_vbus_detect(usbsw,SM5504_ATTACHED);
  678. usbsw->dock_attached = SM5504_ATTACHED;
  679. usbsw->mansw = SW_DHOST;
  680. if(usbsw->previous_dock == SM5504_NONE) {
  681. dev_info(&client->dev, "%s:LANHUB+TA Connected\n", __func__);
  682. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5504_ATTACHED, LANHUB);
  683. sm5504_set_lanhub(usbsw,SM5504_ATTACHED);
  684. } else if (usbsw->previous_dock == ADC_OTG) {
  685. dev_info(&client->dev, "%s:Switch LANHUB to LANHUB+TA\n", __func__);
  686. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5504_ATTACHED, LANHUB_TA);
  687. }
  688. usbsw->previous_dock = ADC_LANHUB;
  689. break;
  690. case ADC_OPEN:
  691. usbsw->adc = adc;
  692. dev_info(&client->dev, "%s:LANHUB + TA -> ADC_OPEN case\n", __func__);
  693. if (pdata->lanhub_cb && usbsw->lanhub_ta_status == 1) {
  694. if (usbsw->previous_dock == ADC_LANHUB)
  695. pdata->lanhub_cb(CABLE_TYPE_LANHUB,SM5504_DETACHED, LANHUB);
  696. sm5504_disable_rawdataInterrupts(usbsw);
  697. usbsw->lanhub_ta_status=0;
  698. sm5504_mask_vbus_detect(usbsw,SM5504_DETACHED);
  699. pdata->callback(CABLE_TYPE_OTG,SM5504_DETACHED);
  700. sm5504_set_otg(usbsw, SM5504_DETACHED);
  701. }
  702. else if (usbsw->previous_dock == ADC_OTG) {
  703. sm5504_disable_rawdataInterrupts(usbsw);
  704. pdata->callback(CABLE_TYPE_OTG,SM5504_DETACHED);
  705. sm5504_set_otg(usbsw, SM5504_DETACHED);
  706. }else {
  707. dev_info(&client->dev, "%s:ignore ADC_OPEN case\n", __func__);
  708. sm5504_mask_vbus_detect(usbsw,SM5504_DETACHED);
  709. }
  710. usbsw->previous_dock = SM5504_NONE;
  711. lanhub_ta_case = false;
  712. break;
  713. default:
  714. dev_info(&client->dev, "%s:Not reaching here(adc:0x%02x)\n",
  715. __func__, adc);
  716. lanhub_ta_case = false;
  717. break;
  718. }
  719. usbsw->dev1=dev1;
  720. usbsw->dev2=dev2;
  721. return adc;
  722. }
  723. #endif
  724. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  725. static void muic_rustproof_feature(struct i2c_client *client, int state)
  726. {
  727. int val;
  728. if(state) {
  729. val = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,
  730. SW_ALL_OPEN_WITH_VBUS);
  731. if(val < 0)
  732. dev_info(&client->dev, "%s:MANUAL SW1,err %d\n",__func__,val);
  733. val = i2c_smbus_read_byte_data(client,REG_CONTROL);
  734. if(val < 0)
  735. dev_info(&client->dev, "%s:CTRL REG,err %d\n",__func__,val);
  736. val &= 0xFB;
  737. val = i2c_smbus_write_byte_data(client,REG_CONTROL,val);
  738. if(val < 0)
  739. dev_info(&client->dev, "%s:CTRL REG,err %d\n",__func__,val);
  740. } else
  741. {
  742. val = i2c_smbus_write_byte_data(client, REG_MANUAL_SW2, 0x00);
  743. if (val < 0)
  744. dev_info(&client->dev, "%s: MANUAL SW2,err %d\n", __func__,val);
  745. val = i2c_smbus_write_byte_data(client, REG_MANUAL_SW1,SW_ALL_OPEN);
  746. if (val < 0)
  747. dev_info(&client->dev, "%s: MANUAL SW1,err %d\n", __func__,val);
  748. val = i2c_smbus_read_byte_data(client, REG_CONTROL);
  749. if (val < 0)
  750. dev_info(&client->dev, "%s: CTRL REG,err %d\n", __func__,val);
  751. val = val | 0x04; /*Automatic Connection S/W enable*/
  752. val = i2c_smbus_write_byte_data(client, REG_CONTROL, val);
  753. if (val < 0)
  754. dev_info(&client->dev, "%s: CTRL REG,err %d\n", __func__,val);
  755. }
  756. }
  757. #endif
  758. static int sm5504_attach_dev(struct sm5504_usbsw *usbsw)
  759. {
  760. int adc;
  761. int val1, val2, val3;
  762. struct sm5504_platform_data *pdata = usbsw->pdata;
  763. struct i2c_client *client = usbsw->client;
  764. int regCtrl1;
  765. #if defined(CONFIG_VIDEO_MHL_V2)
  766. /*u8 mhl_ret = 0;*/
  767. #endif
  768. #if defined(CONFIG_TOUCHSCREEN_MMS144) || defined(CONFIG_TOUCHSCREEN_IST30XX)
  769. int tsp_noti_ignore = 0;
  770. #endif
  771. val1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  772. if (val1 < 0) {
  773. dev_err(&client->dev, "%s: err %d\n", __func__, val1);
  774. return val1;
  775. }
  776. val2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  777. if (val2 < 0) {
  778. dev_err(&client->dev, "%s: err %d\n", __func__, val2);
  779. return val2;
  780. }
  781. jig_state = (val2 & DEV_T2_JIG_ALL_MASK) ? 1 : 0;
  782. val3 = i2c_smbus_read_byte_data(client, REG_CHG_TYPE);
  783. if (val3 < 0) {
  784. dev_err(&client->dev, "%s: err %d\n", __func__, val3);
  785. return val3;
  786. }
  787. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  788. switch (adc) {
  789. case (ADC_VZW_DOCK)...(ADC_MPOS):
  790. case ADC_DESKDOCK:
  791. pr_info("%s,[SM5504 MUIC] Unsupported Accessory!\n", __func__);
  792. goto attach_end;
  793. break;
  794. }
  795. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  796. else if (adc == ADC_LANHUB) {
  797. val2 = DEV_LANHUB;
  798. val1 = 0;
  799. }
  800. #endif
  801. #if defined(CONFIG_USB_HOST_NOTIFY)
  802. if (adc == 0x11 || adc == ADC_AUDIO_DOCK) {
  803. val2 = DEV_AUDIO_DOCK;
  804. val1 = 0;
  805. }
  806. #endif
  807. dev_err(&client->dev,
  808. "dev1: 0x%x,dev2: 0x%x,chg_typ: 0x%x,vbus %d,ADC: 0x%x,Jig: %s\n",
  809. val1, val2, val3, usbsw->vbus, adc,
  810. (check_sm5504_jig_state() ? "ON" : "OFF"));
  811. if (bcd_scan) {
  812. bcd_scan = 0;
  813. pr_info("BCD Scan end val3 = 0x%x\n", val3);
  814. if (val3 == CHG_TYPE_DCP)
  815. pdata->callback(CABLE_TYPE_AC, SM5504_ATTACHED);
  816. else
  817. pdata->callback(CABLE_TYPE_USB, SM5504_ATTACHED);
  818. } else {
  819. /* USB */
  820. if (val1 & DEV_USB || val2 & DEV_T2_USB_MASK ) {
  821. pr_info("[SM5504 MUIC] USB Connected\n");
  822. pdata->callback(CABLE_TYPE_USB, SM5504_ATTACHED);
  823. /* USB_CDP */
  824. } else if (val1 & DEV_USB_CHG) {
  825. pr_info("[MUIC] CDP Connected\n");
  826. pdata->callback(CABLE_TYPE_CDP, SM5504_ATTACHED);
  827. /* UART */
  828. } else if (val1 & DEV_T1_UART_MASK || val2 & DEV_T2_UART_MASK) {
  829. uart_sm5504_connecting = 1;
  830. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  831. if(usbsw->is_rustproof) {
  832. pr_info("[MUIC] RustProof mode, close UART Path\n");
  833. muic_rustproof_feature(client,SM5504_ATTACHED);
  834. } else
  835. #endif
  836. {
  837. pr_info("[MUIC] UART Connected\n");
  838. i2c_smbus_write_byte_data(client, REG_MANUAL_SW1, SW_UART);
  839. if(usbsw->vbus)
  840. pdata->callback(CABLE_TYPE_JIG_UART_OFF_VB, SM5504_ATTACHED);
  841. else
  842. pdata->callback(CABLE_TYPE_UARTOFF, SM5504_ATTACHED);
  843. i2c_smbus_write_byte_data(usbsw->client, REG_CONTROL, CON_ADC_MASK);
  844. }
  845. /* L Company Cable Detection Code */
  846. } else if (adc == ADC_LCABLE) {
  847. pr_info("[MUIC] 219K USB Cable/Charger Connected\n");
  848. // BCD scan start
  849. regCtrl1 = i2c_smbus_read_byte_data(client,0x02);
  850. if (regCtrl1 < 0) {
  851. pr_info("I2C read error regCtrl1 = 0x%02x\n",regCtrl1);
  852. return adc;
  853. }
  854. pr_info("reg_ctrl1 = 0x%x\n", regCtrl1);
  855. regCtrl1 &= (~CON_USBCHDEN);
  856. i2c_smbus_write_byte_data(client,0x02, regCtrl1);
  857. pr_info("reg_ctrl1 = 0x%x\n", regCtrl1);
  858. msleep(1);
  859. regCtrl1 |= (CON_USBCHDEN);
  860. i2c_smbus_write_byte_data(client,0x02, regCtrl1);
  861. pr_info("reg_ctrl1 = 0x%x\n", regCtrl1);
  862. bcd_scan = 1;
  863. pr_info("[MUIC] BCD Scan start \n");
  864. return adc;
  865. /* CHARGER */
  866. } else if ((val1 & DEV_T1_CHARGER_MASK) ||
  867. (val3 & DEV_T3_CHARGER_MASK)) {
  868. pr_info("[MUIC] Charger Connected\n");
  869. pdata->callback(CABLE_TYPE_AC, SM5504_ATTACHED);
  870. #if defined(CONFIG_USB_HOST_NOTIFY)
  871. /* for SAMSUNG OTG */
  872. } else if (val1 & DEV_USB_OTG && adc == ADC_OTG) {
  873. pr_info("[MUIC] OTG Connected\n");
  874. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  875. sm5504_enable_rawdataInterrupts(usbsw);
  876. usbsw->dock_attached = SM5504_ATTACHED;
  877. usbsw->previous_dock = ADC_OTG;
  878. #endif
  879. sm5504_set_otg(usbsw, SM5504_ATTACHED);
  880. pdata->callback(CABLE_TYPE_OTG, SM5504_ATTACHED);
  881. #endif
  882. /* JIG */
  883. } else if (val2 & DEV_T2_JIG_MASK) {
  884. pr_info("[MUIC] JIG Connected\n");
  885. pdata->callback(CABLE_TYPE_JIG, SM5504_ATTACHED);
  886. #if defined(CONFIG_VIDEO_MHL_V2)
  887. /* MHL */
  888. } else if (val3 & DEV_MHL) {
  889. pr_info("[MUIC] MHL Connected\n");
  890. if (!poweroff_charging)
  891. /*mhl_ret = mhl_onoff_ex(1); support from sii8240*/
  892. else
  893. pr_info("LPM mode, skip MHL sequence\n");
  894. #endif
  895. /* Car Dock */
  896. } else if (val2 & DEV_JIG_UART_ON) {
  897. pr_info("[MUIC] Cardock Connected\n");
  898. #if defined(CONFIG_SEC_FACTORY)
  899. local_usbsw->dock_attached = SM5504_ATTACHED;
  900. sm5504_dock_control(usbsw, CABLE_TYPE_CARDOCK,
  901. SM5504_ATTACHED, SW_UART);
  902. #elif defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  903. if(usbsw->is_rustproof) {
  904. pr_info("[MUIC] RustProof mode, close UART Path\n");
  905. muic_rustproof_feature(client,SM5504_ATTACHED);
  906. }
  907. #endif
  908. #if defined(CONFIG_USB_HOST_NOTIFY)
  909. /* Audio Dock */
  910. } else if (val2 & DEV_AUDIO_DOCK) {
  911. pr_info("[MUIC] Audiodock Connected\n");
  912. sm5504_dock_control(usbsw, CABLE_TYPE_AUDIO_DOCK,
  913. SM5504_ATTACHED, SW_DHOST);
  914. #endif
  915. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  916. /* LANHUB */
  917. } else if (val2 & DEV_LANHUB) {
  918. if(usbsw->previous_dock == ADC_LANHUB &&
  919. usbsw->lanhub_ta_status == 1 )
  920. /*Skip for the LANHUB+TA Case*/
  921. pr_info("[MUIC] Lanhub + TA is connected\n");
  922. else {
  923. /* Enable RAWDATA Interrupts */
  924. sm5504_enable_rawdataInterrupts(usbsw);
  925. sm5504_detect_lanhub(usbsw);
  926. }
  927. #endif
  928. /* Incompatible */
  929. } else if (usbsw->vbus) {
  930. pr_info("[MUIC] Incompatible Charger Connected\n");
  931. pdata->callback(CABLE_TYPE_INCOMPATIBLE,
  932. SM5504_ATTACHED);
  933. usbsw->incomp_attached = true;
  934. }
  935. #if defined(CONFIG_TOUCHSCREEN_MMS144) || defined(CONFIG_TOUCHSCREEN_IST30XX)
  936. else{
  937. tsp_noti_ignore = 1;
  938. printk("[TSP] detached, but don't noti \n");
  939. }
  940. if(!tsp_noti_ignore)
  941. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  942. tsp_charger_infom(1);
  943. #else
  944. charger_enable(1);
  945. #endif
  946. #endif
  947. }
  948. attach_end:
  949. usbsw->dev1 = val1;
  950. usbsw->dev2 = val2;
  951. usbsw->dev3 = val3;
  952. usbsw->adc = adc;
  953. return adc;
  954. }
  955. static int sm5504_detach_dev(struct sm5504_usbsw *usbsw)
  956. {
  957. struct sm5504_platform_data *pdata = usbsw->pdata;
  958. #if defined(CONFIG_TOUCHSCREEN_MMS144) || defined(CONFIG_TOUCHSCREEN_IST30XX)
  959. int tsp_noti_ignore = 0;
  960. #endif
  961. pr_err("%s\n", __func__);
  962. pr_err("dev1: 0x%x,dev2: 0x%x,chg_typ: 0x%x,vbus %d,ADC: 0x%x,Jig: %s\n",
  963. usbsw->dev1, usbsw->dev2, usbsw->dev3, usbsw->vbus, usbsw->adc,
  964. (check_sm5504_jig_state() ? "ON" : "OFF"));
  965. switch (usbsw->adc) {
  966. case (ADC_VZW_DOCK)...(ADC_MPOS):
  967. case ADC_DESKDOCK:
  968. pr_info("%s,[SM5504 MUIC] Unsupported Accessory!\n", __func__);
  969. goto detach_end;
  970. break;
  971. }
  972. /* USB */
  973. if (usbsw->dev1 & DEV_USB ||
  974. usbsw->dev2 & DEV_T2_USB_MASK) {
  975. pr_info("[MUIC] USB Disonnected\n");
  976. pdata->callback(CABLE_TYPE_USB, SM5504_DETACHED);
  977. } else if (usbsw->dev1 & DEV_USB_CHG) {
  978. pdata->callback(CABLE_TYPE_CDP, SM5504_DETACHED);
  979. /* UART */
  980. } else if (usbsw->dev1 & DEV_T1_UART_MASK ||
  981. usbsw->dev2 & DEV_T2_UART_MASK) {
  982. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  983. if(usbsw->is_rustproof) {
  984. pr_info("[MUIC] RustProof mode Disconnected Event\n");
  985. muic_rustproof_feature(usbsw->client,SM5504_DETACHED);
  986. } else
  987. #endif
  988. {
  989. pr_info("[MUIC] UART Disonnected\n");
  990. if(usbsw->vbus)
  991. pdata->callback(CABLE_TYPE_JIG_UART_OFF_VB, SM5504_DETACHED);
  992. else
  993. pdata->callback(CABLE_TYPE_UARTOFF, SM5504_DETACHED);
  994. uart_sm5504_connecting = 0;
  995. }
  996. /* L Company Cable Detection Code */
  997. } else if (usbsw->adc == ADC_LCABLE) {
  998. pr_info("[MUIC] 219K USB Cable/Charger Disconnected\n");
  999. if(usbsw->dev3 == CHG_TYPE_DCP)
  1000. pdata->callback(CABLE_TYPE_AC,SM5504_DETACHED);
  1001. else
  1002. pdata->callback(CABLE_TYPE_USB, SM5504_DETACHED);
  1003. /* CHARGER */
  1004. } else if ((usbsw->dev1 & DEV_T1_CHARGER_MASK) ||
  1005. (usbsw->dev3 & DEV_T3_CHARGER_MASK)) {
  1006. pr_info("[MUIC] Charger Disconnected\n");
  1007. pdata->callback(CABLE_TYPE_AC, SM5504_DETACHED);
  1008. #if defined(CONFIG_USB_HOST_NOTIFY)
  1009. /* for SAMSUNG OTG */
  1010. } else if (usbsw->dev1 & DEV_USB_OTG) {
  1011. pr_info("[MUIC] OTG Disconnected\n");
  1012. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  1013. sm5504_disable_rawdataInterrupts(usbsw);
  1014. pr_info("%s:lanhub_ta_status(%d)\n",
  1015. __func__, usbsw->lanhub_ta_status);
  1016. lanhub_ta_case = false;
  1017. if (usbsw->lanhub_ta_status == 0) {
  1018. pdata->callback(CABLE_TYPE_OTG,SM5504_DETACHED);
  1019. sm5504_set_otg(usbsw, SM5504_DETACHED);
  1020. }
  1021. else if (pdata->lanhub_cb && usbsw->lanhub_ta_status == 1)
  1022. pdata->lanhub_cb(CABLE_TYPE_LANHUB,SM5504_DETACHED, LANHUB);
  1023. usbsw->dock_attached = SM5504_DETACHED;
  1024. usbsw->lanhub_ta_status=0;
  1025. #else
  1026. sm5504_set_otg(usbsw, SM5504_DETACHED);
  1027. pdata->callback(CABLE_TYPE_OTG, SM5504_DETACHED);
  1028. #endif
  1029. #endif
  1030. /* JIG */
  1031. } else if (usbsw->dev2 & DEV_T2_JIG_MASK) {
  1032. pr_info("[MUIC] JIG Disonnected\n");
  1033. pdata->callback(CABLE_TYPE_JIG, SM5504_DETACHED);
  1034. #if defined(CONFIG_MHL_D3_SUPPORT)
  1035. /* MHL */
  1036. } else if (usbsw->dev3 & DEV_MHL) {
  1037. pr_info("[MUIC] MHL Disonnected\n");
  1038. //mhl_onoff_ex(false);
  1039. detached_sm5504_status = 1;
  1040. #endif
  1041. /* Car Dock */
  1042. } else if (usbsw->dev2 & DEV_JIG_UART_ON) {
  1043. pr_info("[MUIC] Cardock Disconnected\n");
  1044. #if defined(CONFIG_SEC_FACTORY)
  1045. local_usbsw->dock_attached = SM5504_DETACHED;
  1046. sm5504_dock_control(usbsw, CABLE_TYPE_CARDOCK,
  1047. SM5504_DETACHED, SW_ALL_OPEN);
  1048. #elif defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1049. if(usbsw->is_rustproof) {
  1050. pr_info("[MUIC] RustProof mode disconneted Event\n");
  1051. muic_rustproof_feature(usbsw->client,SM5504_DETACHED);
  1052. }
  1053. #endif
  1054. #if defined(CONFIG_USB_HOST_NOTIFY)
  1055. /* Audio Dock */
  1056. } else if (usbsw->dev2 == DEV_AUDIO_DOCK) {
  1057. pr_info("[MUIC] Audiodock Disconnected\n");
  1058. sm5504_dock_control(usbsw, CABLE_TYPE_AUDIO_DOCK,
  1059. SM5504_DETACHED, SW_ALL_OPEN);
  1060. #endif
  1061. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  1062. /* LANHUB */
  1063. } else if (usbsw->adc == ADC_LANHUB) {
  1064. pr_info("[MUIC] Lanhub disconnected\n");
  1065. /* Disable RAWDATA Interrupts */
  1066. sm5504_disable_rawdataInterrupts(usbsw);
  1067. lanhub_ta_case = false;
  1068. usbsw->dock_attached = SM5504_DETACHED;
  1069. sm5504_set_lanhub(usbsw,SM5504_DETACHED);
  1070. sm5504_mask_vbus_detect(usbsw, SM5504_DETACHED);
  1071. pr_info("%s:lanhub_ta_status(%d)\n",__func__, usbsw->lanhub_ta_status);
  1072. if (pdata->lanhub_cb && usbsw->lanhub_ta_status==1)
  1073. pdata->lanhub_cb(CABLE_TYPE_LANHUB, SM5504_DETACHED, LANHUB);
  1074. else if (usbsw->lanhub_ta_status == 0) {
  1075. pdata->callback(CABLE_TYPE_OTG, SM5504_DETACHED);
  1076. sm5504_set_otg(usbsw, SM5504_DETACHED);
  1077. }
  1078. usbsw->lanhub_ta_status=0;
  1079. usbsw->dock_attached = SM5504_DETACHED;
  1080. #endif
  1081. /* Incompatible */
  1082. } else if (usbsw->incomp_attached) {
  1083. pr_info("[MUIC] Incompatible Charger Disonnected\n");
  1084. pdata->callback(CABLE_TYPE_INCOMPATIBLE,
  1085. SM5504_DETACHED);
  1086. usbsw->incomp_attached = false;
  1087. }
  1088. #if defined(CONFIG_TOUCHSCREEN_MMS144) || defined(CONFIG_TOUCHSCREEN_IST30XX)
  1089. else{
  1090. tsp_noti_ignore = 1;
  1091. printk("[TSP] detached, but don't noti \n");
  1092. }
  1093. if(!tsp_noti_ignore)
  1094. #if defined(CONFIG_TOUCHSCREEN_MMS144)
  1095. tsp_charger_infom(0);
  1096. #else
  1097. charger_enable(0);
  1098. #endif
  1099. #endif
  1100. detach_end:
  1101. i2c_smbus_write_byte_data(usbsw->client, REG_CONTROL, CON_MASK);
  1102. usbsw->dev1 = 0;
  1103. usbsw->dev2 = 0;
  1104. usbsw->dev3 = 0;
  1105. usbsw->adc = 0;
  1106. bcd_scan = 0;
  1107. return 0;
  1108. }
  1109. static irqreturn_t sm5504_irq_thread(int irq, void *data)
  1110. {
  1111. struct sm5504_usbsw *usbsw = data;
  1112. struct i2c_client *client = usbsw->client;
  1113. int intr1, intr2;
  1114. int val1, adc;
  1115. /* SM5504 : Read interrupt -> Read Device */
  1116. pr_info("sm5504_irq_thread is called\n");
  1117. mutex_lock(&usbsw->mutex);
  1118. intr1 = i2c_smbus_read_byte_data(client, REG_INT1);
  1119. intr2 = i2c_smbus_read_byte_data(client, REG_INT2);
  1120. adc = i2c_smbus_read_byte_data(client, REG_ADC);
  1121. dev_info(&client->dev, "%s: intr1 : 0x%x,intr2 : 0x%x, adc : 0x%x\n",
  1122. __func__, intr1, intr2, adc);
  1123. /* MUIC OVP Check */
  1124. if (intr1 & INT_OVP_OCP_EVENT)
  1125. usbsw->pdata->oxp_callback(ENABLE);
  1126. else if (intr1 & (~INT_OVP_OCP_EVENT))
  1127. usbsw->pdata->oxp_callback(DISABLE);
  1128. if (!(intr2 & INT_VBUS_INVALID)) {
  1129. usbsw->vbus = 1;
  1130. } else {
  1131. usbsw->vbus = 0;
  1132. }
  1133. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  1134. if((intr1 == 0x00 && (intr2 & 0x04))
  1135. || ((intr1 & INT_DETACH) && (intr2 & 0x04)))
  1136. sm5504_detect_lanhub(usbsw);
  1137. else
  1138. #endif
  1139. /* device detection */
  1140. /* interrupt both attach and detach */
  1141. if (intr1 == (INT_ATTACH + INT_DETACH)) {
  1142. val1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  1143. if ((adc == ADC_OPEN) && (val1 == DATA_NONE) &&
  1144. (usbsw->vbus == 0x00))
  1145. sm5504_detach_dev(usbsw);
  1146. else
  1147. sm5504_attach_dev(usbsw);
  1148. }
  1149. /* interrupt attach */
  1150. else if ( (intr1 & INT_ATTACH) || (intr2 & INT_RID_CHARGER) )
  1151. sm5504_attach_dev(usbsw);
  1152. /* interrupt detach */
  1153. else if (intr1 & INT_DETACH)
  1154. sm5504_detach_dev(usbsw);
  1155. else if (!(intr2 & INT_VBUS_INVALID)) {
  1156. pr_info("sm5504: VBUSOUT_ON\n");
  1157. #ifdef CONFIG_USB_HOST_NOTIFY
  1158. sec_otg_notify(HNOTIFY_OTG_POWER_ON);
  1159. #endif
  1160. if ((adc != ADC_OPEN) &&
  1161. (sec_get_notification(HNOTIFY_MODE) != NOTIFY_TEST_MODE)) {
  1162. sm5504_attach_dev(usbsw);
  1163. } else {
  1164. goto irq_end;
  1165. }
  1166. }
  1167. else if (intr2 & INT_VBUS_INVALID) {
  1168. pr_info("sm5504: VBUSOUT_OFF\n");
  1169. #ifdef CONFIG_USB_HOST_NOTIFY
  1170. sec_otg_notify(HNOTIFY_OTG_POWER_OFF);
  1171. #endif
  1172. if (sec_get_notification(HNOTIFY_MODE) != NOTIFY_TEST_MODE) {
  1173. sm5504_detach_dev(usbsw);
  1174. } else {
  1175. goto irq_end;
  1176. }
  1177. }
  1178. irq_end:
  1179. mutex_unlock(&usbsw->mutex);
  1180. pr_info("sm5504_irq_thread,end\n");
  1181. return IRQ_HANDLED;
  1182. }
  1183. static int sm5504_irq_init(struct sm5504_usbsw *usbsw)
  1184. {
  1185. struct i2c_client *client = usbsw->client;
  1186. int ret;
  1187. if (client->irq) {
  1188. ret = request_threaded_irq(client->irq, NULL,
  1189. sm5504_irq_thread, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1190. "sm5504 micro USB", usbsw);
  1191. if (ret) {
  1192. dev_err(&client->dev, "failed to reqeust IRQ\n");
  1193. return ret;
  1194. }
  1195. enable_irq_wake(client->irq);
  1196. }
  1197. return 0;
  1198. }
  1199. static void sm5504_init_detect(struct work_struct *work)
  1200. {
  1201. struct sm5504_usbsw *usbsw = container_of(work,
  1202. struct sm5504_usbsw, init_work.work);
  1203. int ret;
  1204. int int_reg1, int_reg2;
  1205. dev_info(&usbsw->client->dev, "%s\n", __func__);
  1206. int_reg1 = sm5504_read_reg(usbsw->client, REG_INT1);
  1207. dev_info(&usbsw->client->dev, "%s: intr1 : 0x%x\n",
  1208. __func__, int_reg1);
  1209. int_reg2 = i2c_smbus_read_byte_data(usbsw->client, REG_INT2);
  1210. dev_info(&usbsw->client->dev, "%s: intr2 : 0x%x\n",
  1211. __func__, int_reg2);
  1212. if (!(int_reg2 & INT_VBUS_INVALID))
  1213. usbsw->vbus = 1;
  1214. else
  1215. usbsw->vbus = 0;
  1216. mutex_lock(&usbsw->mutex);
  1217. sm5504_attach_dev(usbsw);
  1218. mutex_unlock(&usbsw->mutex);
  1219. ret = sm5504_irq_init(usbsw);
  1220. if (ret)
  1221. dev_info(&usbsw->client->dev,
  1222. "failed to enable irq init %s\n", __func__);
  1223. }
  1224. #ifdef CONFIG_OF
  1225. static int sm5504_parse_dt(struct device *dev, struct sm5504_platform_data *pdata)
  1226. {
  1227. struct device_node *np = dev->of_node;
  1228. /*changes can be added later, when needed*/
  1229. #if 0
  1230. /* regulator info */
  1231. pdata->i2c_pull_up = of_property_read_bool(np, "sm5504,i2c-pull-up");
  1232. #endif
  1233. /* reset, irq gpio info */
  1234. pdata->gpio_scl = of_get_named_gpio_flags(np, "sm5504,gpio-scl",
  1235. 0, &pdata->scl_gpio_flags);
  1236. pdata->gpio_uart_on = of_get_named_gpio_flags(np, "sm5504,uarton-gpio",
  1237. 0, &pdata->uarton_gpio_flags);
  1238. pdata->gpio_sda = of_get_named_gpio_flags(np, "sm5504,gpio-sda",
  1239. 0, &pdata->sda_gpio_flags);
  1240. pdata->gpio_int = of_get_named_gpio_flags(np, "sm5504,irq-gpio",
  1241. 0, &pdata->irq_gpio_flags);
  1242. pr_info("%s: irq-gpio: %u \n", __func__, pdata->gpio_int);
  1243. return 0;
  1244. }
  1245. #endif
  1246. static int sm5504_probe(struct i2c_client *client,
  1247. const struct i2c_device_id *id)
  1248. {
  1249. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1250. struct sm5504_usbsw *usbsw;
  1251. int ret = 0;
  1252. struct device *switch_dev;
  1253. struct sm5504_platform_data *pdata;
  1254. dev_info(&client->dev,"%s:sm5504 probe called \n",__func__);
  1255. if(client->dev.of_node) {
  1256. pdata = devm_kzalloc(&client->dev,
  1257. sizeof(struct sm5504_platform_data), GFP_KERNEL);
  1258. if (!pdata) {
  1259. dev_err(&client->dev, "Failed to allocate memory \n");
  1260. return -ENOMEM;
  1261. }
  1262. ret = sm5504_parse_dt(&client->dev, pdata);
  1263. if (ret < 0) {
  1264. dev_err(&client->dev, "sm5502_parse_dt failed\n");
  1265. goto fail;
  1266. }
  1267. pdata->callback = sm5504_callback;
  1268. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  1269. pdata->lanhub_cb = sm5504_lanhub_callback;
  1270. #endif
  1271. pdata->dock_init = sm5504_dock_init;
  1272. pdata->oxp_callback = sm5504_oxp_callback;
  1273. pdata->mhl_sel = NULL;
  1274. #if defined(CONFIG_GPIO_MSM_V3)
  1275. gpio_tlmm_config(GPIO_CFG(pdata->gpio_int, 0, GPIO_CFG_INPUT,
  1276. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1277. gpio_tlmm_config(GPIO_CFG(pdata->gpio_scl, 0, GPIO_CFG_INPUT,
  1278. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1279. gpio_tlmm_config(GPIO_CFG(pdata->gpio_sda, 0, GPIO_CFG_INPUT,
  1280. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  1281. gpio_tlmm_config(GPIO_CFG(pdata->gpio_uart_on, 0, GPIO_CFG_INPUT,
  1282. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  1283. #endif
  1284. client->irq = gpio_to_irq(pdata->gpio_int);
  1285. } else {
  1286. pdata = client->dev.platform_data;
  1287. if (!pdata)
  1288. return -EINVAL;
  1289. }
  1290. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1291. dev_err(&client->dev, "i2c functionality check failed...!\n");
  1292. ret = -EIO;
  1293. goto fail;
  1294. }
  1295. usbsw = kzalloc(sizeof(struct sm5504_usbsw), GFP_KERNEL);
  1296. if (!usbsw) {
  1297. dev_err(&client->dev, "failed to allocate driver data\n");
  1298. ret = -ENOMEM;
  1299. goto fail;
  1300. }
  1301. usbsw->client = client;
  1302. if (client->dev.of_node)
  1303. usbsw->pdata = pdata;
  1304. else
  1305. usbsw->pdata = client->dev.platform_data;
  1306. if (!usbsw->pdata)
  1307. goto fail1;
  1308. i2c_set_clientdata(client, usbsw);
  1309. mutex_init(&usbsw->mutex);
  1310. usbsw->incomp_attached = false;
  1311. local_usbsw = usbsw;
  1312. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1313. ret = sm5504_read_reg(client,REG_MANUAL_SW1);
  1314. if(ret<0) {
  1315. dev_err(&client->dev, "failed to read MANUAL SW1 Reg, err:%d\n",ret);
  1316. }
  1317. /* Keep the feature disabled by default */
  1318. usbsw->is_rustproof = false;
  1319. dev_err(&client->dev, "MANUAL SW1 Reg :%d\n",ret);
  1320. /* RUSTPROOF: disable UART connection if MANSW1 from BL is OPEN_RUSTPROOF*/
  1321. if(ret == MANSW1_OPEN_RUSTPROOF)
  1322. usbsw->is_rustproof = true;
  1323. #endif
  1324. sm5504_reg_init(usbsw);
  1325. ret = sysfs_create_group(&client->dev.kobj, &sm5504_group);
  1326. if (ret) {
  1327. dev_err(&client->dev,
  1328. "failed to create sm5504 attribute group\n");
  1329. goto fail2;
  1330. }
  1331. /* make sysfs node /sys/class/sec/switch/ */
  1332. switch_dev = device_create(sec_class, NULL, 0, NULL, "switch");
  1333. if (IS_ERR(switch_dev)) {
  1334. pr_err("[SM5504] Failed to create device (switch_dev)!\n");
  1335. ret = PTR_ERR(switch_dev);
  1336. goto fail2;
  1337. }
  1338. ret = device_create_file(switch_dev, &dev_attr_usb_state);
  1339. if (ret < 0) {
  1340. pr_err("[SM5504] Failed to create file (usb_state)!\n");
  1341. goto err_create_file_state;
  1342. }
  1343. ret = device_create_file(switch_dev, &dev_attr_adc);
  1344. if (ret < 0) {
  1345. pr_err("[SM5504] Failed to create file (adc)!\n");
  1346. goto err_create_file_adc;
  1347. }
  1348. ret = device_create_file(switch_dev, &dev_attr_reset_switch);
  1349. if (ret < 0) {
  1350. pr_err("[SM5504] Failed to create file (reset_switch)!\n");
  1351. goto err_create_file_reset_switch;
  1352. }
  1353. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1354. ret = device_create_file(switch_dev, &dev_attr_uart_en);
  1355. if (ret < 0) {
  1356. pr_err("[SM5504] Failed to create file (uart_en)!\n");
  1357. goto err_create_file_uart_en;
  1358. }
  1359. ret = device_create_file(switch_dev, &dev_attr_uart_sel);
  1360. if (ret < 0) {
  1361. pr_err("[SM5504] Failed to create file (uart_sel)!\n");
  1362. goto err_create_file_uart_sel;
  1363. }
  1364. ret = device_create_file(switch_dev, &dev_attr_usb_sel);
  1365. if (ret < 0) {
  1366. pr_err("[SM5504] Failed to create file (usb_sel)!\n");
  1367. goto err_create_file_usb_sel;
  1368. }
  1369. #endif
  1370. #if defined(CONFIG_MUIC_SM5504_SUPPORT_LANHUB_TA)
  1371. local_usbsw->previous_dock = SM5504_NONE;
  1372. local_usbsw->lanhub_ta_status = 0;
  1373. #endif
  1374. dev_set_drvdata(switch_dev, usbsw);
  1375. /* sm5504 dock init*/
  1376. if (usbsw->pdata->dock_init)
  1377. usbsw->pdata->dock_init();
  1378. /* initial cable detection */
  1379. INIT_DELAYED_WORK(&usbsw->init_work, sm5504_init_detect);
  1380. schedule_delayed_work(&usbsw->init_work, msecs_to_jiffies(2700));
  1381. return 0;
  1382. #if defined(CONFIG_MUIC_SUPPORT_RUSTPROOF)
  1383. err_create_file_usb_sel:
  1384. device_remove_file(switch_dev, &dev_attr_usb_sel);
  1385. err_create_file_uart_sel:
  1386. device_remove_file(switch_dev, &dev_attr_uart_sel);
  1387. err_create_file_uart_en:
  1388. device_remove_file(switch_dev, &dev_attr_uart_en);
  1389. #endif
  1390. err_create_file_reset_switch:
  1391. device_remove_file(switch_dev, &dev_attr_reset_switch);
  1392. err_create_file_adc:
  1393. device_remove_file(switch_dev, &dev_attr_adc);
  1394. err_create_file_state:
  1395. device_remove_file(switch_dev, &dev_attr_usb_state);
  1396. fail2:
  1397. if (client->irq)
  1398. free_irq(client->irq, usbsw);
  1399. mutex_destroy(&usbsw->mutex);
  1400. i2c_set_clientdata(client, NULL);
  1401. fail1:
  1402. kfree(usbsw);
  1403. fail:
  1404. kfree(pdata);
  1405. return ret;
  1406. }
  1407. static int sm5504_remove(struct i2c_client *client)
  1408. {
  1409. struct sm5504_usbsw *usbsw = i2c_get_clientdata(client);
  1410. cancel_delayed_work(&usbsw->init_work);
  1411. if (client->irq) {
  1412. disable_irq_wake(client->irq);
  1413. free_irq(client->irq, usbsw);
  1414. }
  1415. mutex_destroy(&usbsw->mutex);
  1416. i2c_set_clientdata(client, NULL);
  1417. sysfs_remove_group(&client->dev.kobj, &sm5504_group);
  1418. kfree(usbsw);
  1419. return 0;
  1420. }
  1421. static int sm5504_resume(struct i2c_client *client)
  1422. {
  1423. struct sm5504_usbsw *usbsw = i2c_get_clientdata(client);
  1424. int ldev1, ldev2, intr;
  1425. pr_info("%s: resume \n",__func__);
  1426. ldev1 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE1);
  1427. if (ldev1 < 0)
  1428. {
  1429. pr_err("%s: Dev reg 1 read err! %d \n",__func__, ldev1);
  1430. goto safe_exit;
  1431. }
  1432. ldev2 = i2c_smbus_read_byte_data(client, REG_DEVICE_TYPE2);
  1433. if (ldev2 < 0)
  1434. {
  1435. pr_err("%s: Dev reg 2 read err! %d \n",__func__, ldev2);
  1436. goto safe_exit;
  1437. }
  1438. intr = i2c_smbus_read_byte_data(client, REG_INT1);
  1439. i2c_smbus_read_byte_data(client, REG_INT2);
  1440. if (intr & INT_DETACH) {
  1441. mutex_lock(&usbsw->mutex);
  1442. sm5504_detach_dev(usbsw);
  1443. mutex_unlock(&usbsw->mutex);
  1444. } else if (usbsw->dev1 != ldev1 || usbsw->dev2 != ldev2) {
  1445. /* device detection */
  1446. mutex_lock(&usbsw->mutex);
  1447. sm5504_attach_dev(usbsw);
  1448. mutex_unlock(&usbsw->mutex);
  1449. }
  1450. safe_exit:
  1451. return 0;
  1452. }
  1453. static const struct i2c_device_id sm5504_id[] = {
  1454. {"sm5504", 0},
  1455. {}
  1456. };
  1457. MODULE_DEVICE_TABLE(i2c, sm5504_id);
  1458. static struct of_device_id sm5504_i2c_match_table[] = {
  1459. { .compatible = "sm5504,i2c",},
  1460. {},
  1461. };
  1462. MODULE_DEVICE_TABLE(of, sm5504_i2c_match_table);
  1463. static struct i2c_driver sm5504_i2c_driver = {
  1464. .driver = {
  1465. .name = "sm5504",
  1466. .owner = THIS_MODULE,
  1467. .of_match_table = sm5504_i2c_match_table,
  1468. },
  1469. .probe = sm5504_probe,
  1470. .remove = sm5504_remove,
  1471. .resume = sm5504_resume,
  1472. .id_table = sm5504_id,
  1473. };
  1474. static int __init sm5504_init(void)
  1475. {
  1476. return i2c_add_driver(&sm5504_i2c_driver);
  1477. }
  1478. /*
  1479. * Late init call is required MUIC was accessing
  1480. * USB driver before USB initialization and watch dog reset
  1481. * was happening when booted with USB connected
  1482. */
  1483. late_initcall(sm5504_init);
  1484. static void __exit sm5504_exit(void)
  1485. {
  1486. i2c_del_driver(&sm5504_i2c_driver);
  1487. }
  1488. module_exit(sm5504_exit);
  1489. MODULE_AUTHOR("Nitin Chaudhary <nc.chaudhary@samsung.com>");
  1490. MODULE_DESCRIPTION("SM5504 Micro USB Switch driver");
  1491. MODULE_LICENSE("GPL");