fsa9480.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * fsa9480.c - FSA9480 micro USB switch device driver
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. * Minkyu Kang <mk7.kang@samsung.com>
  6. * Wonguk Jeong <wonguk.jeong@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/platform_data/fsa9480.h>
  17. #include <linux/irq.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/pm_runtime.h>
  23. /* FSA9480 I2C registers */
  24. #define FSA9480_REG_DEVID 0x01
  25. #define FSA9480_REG_CTRL 0x02
  26. #define FSA9480_REG_INT1 0x03
  27. #define FSA9480_REG_INT2 0x04
  28. #define FSA9480_REG_INT1_MASK 0x05
  29. #define FSA9480_REG_INT2_MASK 0x06
  30. #define FSA9480_REG_ADC 0x07
  31. #define FSA9480_REG_TIMING1 0x08
  32. #define FSA9480_REG_TIMING2 0x09
  33. #define FSA9480_REG_DEV_T1 0x0a
  34. #define FSA9480_REG_DEV_T2 0x0b
  35. #define FSA9480_REG_BTN1 0x0c
  36. #define FSA9480_REG_BTN2 0x0d
  37. #define FSA9480_REG_CK 0x0e
  38. #define FSA9480_REG_CK_INT1 0x0f
  39. #define FSA9480_REG_CK_INT2 0x10
  40. #define FSA9480_REG_CK_INTMASK1 0x11
  41. #define FSA9480_REG_CK_INTMASK2 0x12
  42. #define FSA9480_REG_MANSW1 0x13
  43. #define FSA9480_REG_MANSW2 0x14
  44. /* Control */
  45. #define CON_SWITCH_OPEN (1 << 4)
  46. #define CON_RAW_DATA (1 << 3)
  47. #define CON_MANUAL_SW (1 << 2)
  48. #define CON_WAIT (1 << 1)
  49. #define CON_INT_MASK (1 << 0)
  50. #define CON_MASK (CON_SWITCH_OPEN | CON_RAW_DATA | \
  51. CON_MANUAL_SW | CON_WAIT)
  52. /* Device Type 1 */
  53. #define DEV_USB_OTG (1 << 7)
  54. #define DEV_DEDICATED_CHG (1 << 6)
  55. #define DEV_USB_CHG (1 << 5)
  56. #define DEV_CAR_KIT (1 << 4)
  57. #define DEV_UART (1 << 3)
  58. #define DEV_USB (1 << 2)
  59. #define DEV_AUDIO_2 (1 << 1)
  60. #define DEV_AUDIO_1 (1 << 0)
  61. #define DEV_T1_USB_MASK (DEV_USB_OTG | DEV_USB)
  62. #define DEV_T1_UART_MASK (DEV_UART)
  63. #define DEV_T1_CHARGER_MASK (DEV_DEDICATED_CHG | DEV_USB_CHG)
  64. /* Device Type 2 */
  65. #define DEV_AV (1 << 6)
  66. #define DEV_TTY (1 << 5)
  67. #define DEV_PPD (1 << 4)
  68. #define DEV_JIG_UART_OFF (1 << 3)
  69. #define DEV_JIG_UART_ON (1 << 2)
  70. #define DEV_JIG_USB_OFF (1 << 1)
  71. #define DEV_JIG_USB_ON (1 << 0)
  72. #define DEV_T2_USB_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON)
  73. #define DEV_T2_UART_MASK (DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
  74. #define DEV_T2_JIG_MASK (DEV_JIG_USB_OFF | DEV_JIG_USB_ON | \
  75. DEV_JIG_UART_OFF | DEV_JIG_UART_ON)
  76. /*
  77. * Manual Switch
  78. * D- [7:5] / D+ [4:2]
  79. * 000: Open all / 001: USB / 010: AUDIO / 011: UART / 100: V_AUDIO
  80. */
  81. #define SW_VAUDIO ((4 << 5) | (4 << 2))
  82. #define SW_UART ((3 << 5) | (3 << 2))
  83. #define SW_AUDIO ((2 << 5) | (2 << 2))
  84. #define SW_DHOST ((1 << 5) | (1 << 2))
  85. #define SW_AUTO ((0 << 5) | (0 << 2))
  86. /* Interrupt 1 */
  87. #define INT_DETACH (1 << 1)
  88. #define INT_ATTACH (1 << 0)
  89. struct fsa9480_usbsw {
  90. struct i2c_client *client;
  91. struct fsa9480_platform_data *pdata;
  92. int dev1;
  93. int dev2;
  94. int mansw;
  95. };
  96. static struct fsa9480_usbsw *chip;
  97. static int fsa9480_write_reg(struct i2c_client *client,
  98. int reg, int value)
  99. {
  100. int ret;
  101. ret = i2c_smbus_write_byte_data(client, reg, value);
  102. if (ret < 0)
  103. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  104. return ret;
  105. }
  106. static int fsa9480_read_reg(struct i2c_client *client, int reg)
  107. {
  108. int ret;
  109. ret = i2c_smbus_read_byte_data(client, reg);
  110. if (ret < 0)
  111. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  112. return ret;
  113. }
  114. static int fsa9480_read_irq(struct i2c_client *client, int *value)
  115. {
  116. int ret;
  117. ret = i2c_smbus_read_i2c_block_data(client,
  118. FSA9480_REG_INT1, 2, (u8 *)value);
  119. *value &= 0xffff;
  120. if (ret < 0)
  121. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  122. return ret;
  123. }
  124. static void fsa9480_set_switch(const char *buf)
  125. {
  126. struct fsa9480_usbsw *usbsw = chip;
  127. struct i2c_client *client = usbsw->client;
  128. unsigned int value;
  129. unsigned int path = 0;
  130. value = fsa9480_read_reg(client, FSA9480_REG_CTRL);
  131. if (!strncmp(buf, "VAUDIO", 6)) {
  132. path = SW_VAUDIO;
  133. value &= ~CON_MANUAL_SW;
  134. } else if (!strncmp(buf, "UART", 4)) {
  135. path = SW_UART;
  136. value &= ~CON_MANUAL_SW;
  137. } else if (!strncmp(buf, "AUDIO", 5)) {
  138. path = SW_AUDIO;
  139. value &= ~CON_MANUAL_SW;
  140. } else if (!strncmp(buf, "DHOST", 5)) {
  141. path = SW_DHOST;
  142. value &= ~CON_MANUAL_SW;
  143. } else if (!strncmp(buf, "AUTO", 4)) {
  144. path = SW_AUTO;
  145. value |= CON_MANUAL_SW;
  146. } else {
  147. printk(KERN_ERR "Wrong command\n");
  148. return;
  149. }
  150. usbsw->mansw = path;
  151. fsa9480_write_reg(client, FSA9480_REG_MANSW1, path);
  152. fsa9480_write_reg(client, FSA9480_REG_CTRL, value);
  153. }
  154. static ssize_t fsa9480_get_switch(char *buf)
  155. {
  156. struct fsa9480_usbsw *usbsw = chip;
  157. struct i2c_client *client = usbsw->client;
  158. unsigned int value;
  159. value = fsa9480_read_reg(client, FSA9480_REG_MANSW1);
  160. if (value == SW_VAUDIO)
  161. return sprintf(buf, "VAUDIO\n");
  162. else if (value == SW_UART)
  163. return sprintf(buf, "UART\n");
  164. else if (value == SW_AUDIO)
  165. return sprintf(buf, "AUDIO\n");
  166. else if (value == SW_DHOST)
  167. return sprintf(buf, "DHOST\n");
  168. else if (value == SW_AUTO)
  169. return sprintf(buf, "AUTO\n");
  170. else
  171. return sprintf(buf, "%x", value);
  172. }
  173. static ssize_t fsa9480_show_device(struct device *dev,
  174. struct device_attribute *attr,
  175. char *buf)
  176. {
  177. struct fsa9480_usbsw *usbsw = dev_get_drvdata(dev);
  178. struct i2c_client *client = usbsw->client;
  179. int dev1, dev2;
  180. dev1 = fsa9480_read_reg(client, FSA9480_REG_DEV_T1);
  181. dev2 = fsa9480_read_reg(client, FSA9480_REG_DEV_T2);
  182. if (!dev1 && !dev2)
  183. return sprintf(buf, "NONE\n");
  184. /* USB */
  185. if (dev1 & DEV_T1_USB_MASK || dev2 & DEV_T2_USB_MASK)
  186. return sprintf(buf, "USB\n");
  187. /* UART */
  188. if (dev1 & DEV_T1_UART_MASK || dev2 & DEV_T2_UART_MASK)
  189. return sprintf(buf, "UART\n");
  190. /* CHARGER */
  191. if (dev1 & DEV_T1_CHARGER_MASK)
  192. return sprintf(buf, "CHARGER\n");
  193. /* JIG */
  194. if (dev2 & DEV_T2_JIG_MASK)
  195. return sprintf(buf, "JIG\n");
  196. return sprintf(buf, "UNKNOWN\n");
  197. }
  198. static ssize_t fsa9480_show_manualsw(struct device *dev,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. return fsa9480_get_switch(buf);
  202. }
  203. static ssize_t fsa9480_set_manualsw(struct device *dev,
  204. struct device_attribute *attr,
  205. const char *buf, size_t count)
  206. {
  207. fsa9480_set_switch(buf);
  208. return count;
  209. }
  210. static DEVICE_ATTR(device, S_IRUGO, fsa9480_show_device, NULL);
  211. static DEVICE_ATTR(switch, S_IRUGO | S_IWUSR,
  212. fsa9480_show_manualsw, fsa9480_set_manualsw);
  213. static struct attribute *fsa9480_attributes[] = {
  214. &dev_attr_device.attr,
  215. &dev_attr_switch.attr,
  216. NULL
  217. };
  218. static const struct attribute_group fsa9480_group = {
  219. .attrs = fsa9480_attributes,
  220. };
  221. static void fsa9480_detect_dev(struct fsa9480_usbsw *usbsw, int intr)
  222. {
  223. int val1, val2, ctrl;
  224. struct fsa9480_platform_data *pdata = usbsw->pdata;
  225. struct i2c_client *client = usbsw->client;
  226. val1 = fsa9480_read_reg(client, FSA9480_REG_DEV_T1);
  227. val2 = fsa9480_read_reg(client, FSA9480_REG_DEV_T2);
  228. ctrl = fsa9480_read_reg(client, FSA9480_REG_CTRL);
  229. dev_info(&client->dev, "intr: 0x%x, dev1: 0x%x, dev2: 0x%x\n",
  230. intr, val1, val2);
  231. if (!intr)
  232. goto out;
  233. if (intr & INT_ATTACH) { /* Attached */
  234. /* USB */
  235. if (val1 & DEV_T1_USB_MASK || val2 & DEV_T2_USB_MASK) {
  236. if (pdata->usb_cb)
  237. pdata->usb_cb(FSA9480_ATTACHED);
  238. if (usbsw->mansw) {
  239. fsa9480_write_reg(client,
  240. FSA9480_REG_MANSW1, usbsw->mansw);
  241. }
  242. }
  243. /* UART */
  244. if (val1 & DEV_T1_UART_MASK || val2 & DEV_T2_UART_MASK) {
  245. if (pdata->uart_cb)
  246. pdata->uart_cb(FSA9480_ATTACHED);
  247. if (!(ctrl & CON_MANUAL_SW)) {
  248. fsa9480_write_reg(client,
  249. FSA9480_REG_MANSW1, SW_UART);
  250. }
  251. }
  252. /* CHARGER */
  253. if (val1 & DEV_T1_CHARGER_MASK) {
  254. if (pdata->charger_cb)
  255. pdata->charger_cb(FSA9480_ATTACHED);
  256. }
  257. /* JIG */
  258. if (val2 & DEV_T2_JIG_MASK) {
  259. if (pdata->jig_cb)
  260. pdata->jig_cb(FSA9480_ATTACHED);
  261. }
  262. } else if (intr & INT_DETACH) { /* Detached */
  263. /* USB */
  264. if (usbsw->dev1 & DEV_T1_USB_MASK ||
  265. usbsw->dev2 & DEV_T2_USB_MASK) {
  266. if (pdata->usb_cb)
  267. pdata->usb_cb(FSA9480_DETACHED);
  268. }
  269. /* UART */
  270. if (usbsw->dev1 & DEV_T1_UART_MASK ||
  271. usbsw->dev2 & DEV_T2_UART_MASK) {
  272. if (pdata->uart_cb)
  273. pdata->uart_cb(FSA9480_DETACHED);
  274. }
  275. /* CHARGER */
  276. if (usbsw->dev1 & DEV_T1_CHARGER_MASK) {
  277. if (pdata->charger_cb)
  278. pdata->charger_cb(FSA9480_DETACHED);
  279. }
  280. /* JIG */
  281. if (usbsw->dev2 & DEV_T2_JIG_MASK) {
  282. if (pdata->jig_cb)
  283. pdata->jig_cb(FSA9480_DETACHED);
  284. }
  285. }
  286. usbsw->dev1 = val1;
  287. usbsw->dev2 = val2;
  288. out:
  289. ctrl &= ~CON_INT_MASK;
  290. fsa9480_write_reg(client, FSA9480_REG_CTRL, ctrl);
  291. }
  292. static irqreturn_t fsa9480_irq_handler(int irq, void *data)
  293. {
  294. struct fsa9480_usbsw *usbsw = data;
  295. struct i2c_client *client = usbsw->client;
  296. int intr;
  297. /* clear interrupt */
  298. fsa9480_read_irq(client, &intr);
  299. /* device detection */
  300. fsa9480_detect_dev(usbsw, intr);
  301. return IRQ_HANDLED;
  302. }
  303. static int fsa9480_irq_init(struct fsa9480_usbsw *usbsw)
  304. {
  305. struct fsa9480_platform_data *pdata = usbsw->pdata;
  306. struct i2c_client *client = usbsw->client;
  307. int ret;
  308. int intr;
  309. unsigned int ctrl = CON_MASK;
  310. /* clear interrupt */
  311. fsa9480_read_irq(client, &intr);
  312. /* unmask interrupt (attach/detach only) */
  313. fsa9480_write_reg(client, FSA9480_REG_INT1_MASK, 0xfc);
  314. fsa9480_write_reg(client, FSA9480_REG_INT2_MASK, 0x1f);
  315. usbsw->mansw = fsa9480_read_reg(client, FSA9480_REG_MANSW1);
  316. if (usbsw->mansw)
  317. ctrl &= ~CON_MANUAL_SW; /* Manual Switching Mode */
  318. fsa9480_write_reg(client, FSA9480_REG_CTRL, ctrl);
  319. if (pdata && pdata->cfg_gpio)
  320. pdata->cfg_gpio();
  321. if (client->irq) {
  322. ret = request_threaded_irq(client->irq, NULL,
  323. fsa9480_irq_handler,
  324. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  325. "fsa9480 micro USB", usbsw);
  326. if (ret) {
  327. dev_err(&client->dev, "failed to request IRQ\n");
  328. return ret;
  329. }
  330. if (pdata)
  331. device_init_wakeup(&client->dev, pdata->wakeup);
  332. }
  333. return 0;
  334. }
  335. static int fsa9480_probe(struct i2c_client *client,
  336. const struct i2c_device_id *id)
  337. {
  338. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  339. struct fsa9480_usbsw *usbsw;
  340. int ret = 0;
  341. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  342. return -EIO;
  343. usbsw = kzalloc(sizeof(struct fsa9480_usbsw), GFP_KERNEL);
  344. if (!usbsw) {
  345. dev_err(&client->dev, "failed to allocate driver data\n");
  346. return -ENOMEM;
  347. }
  348. usbsw->client = client;
  349. usbsw->pdata = client->dev.platform_data;
  350. chip = usbsw;
  351. i2c_set_clientdata(client, usbsw);
  352. ret = fsa9480_irq_init(usbsw);
  353. if (ret)
  354. goto fail1;
  355. ret = sysfs_create_group(&client->dev.kobj, &fsa9480_group);
  356. if (ret) {
  357. dev_err(&client->dev,
  358. "failed to create fsa9480 attribute group\n");
  359. goto fail2;
  360. }
  361. /* ADC Detect Time: 500ms */
  362. fsa9480_write_reg(client, FSA9480_REG_TIMING1, 0x6);
  363. if (chip->pdata->reset_cb)
  364. chip->pdata->reset_cb();
  365. /* device detection */
  366. fsa9480_detect_dev(usbsw, INT_ATTACH);
  367. pm_runtime_set_active(&client->dev);
  368. return 0;
  369. fail2:
  370. if (client->irq)
  371. free_irq(client->irq, usbsw);
  372. fail1:
  373. kfree(usbsw);
  374. return ret;
  375. }
  376. static int fsa9480_remove(struct i2c_client *client)
  377. {
  378. struct fsa9480_usbsw *usbsw = i2c_get_clientdata(client);
  379. if (client->irq)
  380. free_irq(client->irq, usbsw);
  381. sysfs_remove_group(&client->dev.kobj, &fsa9480_group);
  382. device_init_wakeup(&client->dev, 0);
  383. kfree(usbsw);
  384. return 0;
  385. }
  386. #ifdef CONFIG_PM_SLEEP
  387. static int fsa9480_suspend(struct device *dev)
  388. {
  389. struct i2c_client *client = to_i2c_client(dev);
  390. struct fsa9480_usbsw *usbsw = i2c_get_clientdata(client);
  391. struct fsa9480_platform_data *pdata = usbsw->pdata;
  392. if (device_may_wakeup(&client->dev) && client->irq)
  393. enable_irq_wake(client->irq);
  394. if (pdata->usb_power)
  395. pdata->usb_power(0);
  396. return 0;
  397. }
  398. static int fsa9480_resume(struct device *dev)
  399. {
  400. struct i2c_client *client = to_i2c_client(dev);
  401. struct fsa9480_usbsw *usbsw = i2c_get_clientdata(client);
  402. int dev1, dev2;
  403. if (device_may_wakeup(&client->dev) && client->irq)
  404. disable_irq_wake(client->irq);
  405. /*
  406. * Clear Pending interrupt. Note that detect_dev does what
  407. * the interrupt handler does. So, we don't miss pending and
  408. * we reenable interrupt if there is one.
  409. */
  410. fsa9480_read_reg(client, FSA9480_REG_INT1);
  411. fsa9480_read_reg(client, FSA9480_REG_INT2);
  412. dev1 = fsa9480_read_reg(client, FSA9480_REG_DEV_T1);
  413. dev2 = fsa9480_read_reg(client, FSA9480_REG_DEV_T2);
  414. /* device detection */
  415. fsa9480_detect_dev(usbsw, (dev1 || dev2) ? INT_ATTACH : INT_DETACH);
  416. return 0;
  417. }
  418. static SIMPLE_DEV_PM_OPS(fsa9480_pm_ops, fsa9480_suspend, fsa9480_resume);
  419. #define FSA9480_PM_OPS (&fsa9480_pm_ops)
  420. #else
  421. #define FSA9480_PM_OPS NULL
  422. #endif /* CONFIG_PM_SLEEP */
  423. static const struct i2c_device_id fsa9480_id[] = {
  424. {"fsa9480", 0},
  425. {}
  426. };
  427. MODULE_DEVICE_TABLE(i2c, fsa9480_id);
  428. static struct i2c_driver fsa9480_i2c_driver = {
  429. .driver = {
  430. .name = "fsa9480",
  431. .pm = FSA9480_PM_OPS,
  432. },
  433. .probe = fsa9480_probe,
  434. .remove = fsa9480_remove,
  435. .id_table = fsa9480_id,
  436. };
  437. module_i2c_driver(fsa9480_i2c_driver);
  438. MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
  439. MODULE_DESCRIPTION("FSA9480 USB Switch driver");
  440. MODULE_LICENSE("GPL");