extcon-max8997.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
  3. *
  4. * Copyright (C) 2012 Samsung Electrnoics
  5. * Donggeun Kim <dg77.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. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/err.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/kobject.h>
  25. #include <linux/mfd/max8997.h>
  26. #include <linux/mfd/max8997-private.h>
  27. #include <linux/extcon.h>
  28. #include <linux/irqdomain.h>
  29. #define DEV_NAME "max8997-muic"
  30. /* MAX8997-MUIC STATUS1 register */
  31. #define STATUS1_ADC_SHIFT 0
  32. #define STATUS1_ADCLOW_SHIFT 5
  33. #define STATUS1_ADCERR_SHIFT 6
  34. #define STATUS1_ADC_MASK (0x1f << STATUS1_ADC_SHIFT)
  35. #define STATUS1_ADCLOW_MASK (0x1 << STATUS1_ADCLOW_SHIFT)
  36. #define STATUS1_ADCERR_MASK (0x1 << STATUS1_ADCERR_SHIFT)
  37. /* MAX8997-MUIC STATUS2 register */
  38. #define STATUS2_CHGTYP_SHIFT 0
  39. #define STATUS2_CHGDETRUN_SHIFT 3
  40. #define STATUS2_DCDTMR_SHIFT 4
  41. #define STATUS2_DBCHG_SHIFT 5
  42. #define STATUS2_VBVOLT_SHIFT 6
  43. #define STATUS2_CHGTYP_MASK (0x7 << STATUS2_CHGTYP_SHIFT)
  44. #define STATUS2_CHGDETRUN_MASK (0x1 << STATUS2_CHGDETRUN_SHIFT)
  45. #define STATUS2_DCDTMR_MASK (0x1 << STATUS2_DCDTMR_SHIFT)
  46. #define STATUS2_DBCHG_MASK (0x1 << STATUS2_DBCHG_SHIFT)
  47. #define STATUS2_VBVOLT_MASK (0x1 << STATUS2_VBVOLT_SHIFT)
  48. /* MAX8997-MUIC STATUS3 register */
  49. #define STATUS3_OVP_SHIFT 2
  50. #define STATUS3_OVP_MASK (0x1 << STATUS3_OVP_SHIFT)
  51. /* MAX8997-MUIC CONTROL1 register */
  52. #define COMN1SW_SHIFT 0
  53. #define COMP2SW_SHIFT 3
  54. #define COMN1SW_MASK (0x7 << COMN1SW_SHIFT)
  55. #define COMP2SW_MASK (0x7 << COMP2SW_SHIFT)
  56. #define SW_MASK (COMP2SW_MASK | COMN1SW_MASK)
  57. #define MAX8997_SW_USB ((1 << COMP2SW_SHIFT) | (1 << COMN1SW_SHIFT))
  58. #define MAX8997_SW_AUDIO ((2 << COMP2SW_SHIFT) | (2 << COMN1SW_SHIFT))
  59. #define MAX8997_SW_UART ((3 << COMP2SW_SHIFT) | (3 << COMN1SW_SHIFT))
  60. #define MAX8997_SW_OPEN ((0 << COMP2SW_SHIFT) | (0 << COMN1SW_SHIFT))
  61. #define MAX8997_ADC_GROUND 0x00
  62. #define MAX8997_ADC_MHL 0x01
  63. #define MAX8997_ADC_JIG_USB_1 0x18
  64. #define MAX8997_ADC_JIG_USB_2 0x19
  65. #define MAX8997_ADC_DESKDOCK 0x1a
  66. #define MAX8997_ADC_JIG_UART 0x1c
  67. #define MAX8997_ADC_CARDOCK 0x1d
  68. #define MAX8997_ADC_OPEN 0x1f
  69. struct max8997_muic_irq {
  70. unsigned int irq;
  71. const char *name;
  72. unsigned int virq;
  73. };
  74. static struct max8997_muic_irq muic_irqs[] = {
  75. { MAX8997_MUICIRQ_ADCError, "muic-ADC_error" },
  76. { MAX8997_MUICIRQ_ADCLow, "muic-ADC_low" },
  77. { MAX8997_MUICIRQ_ADC, "muic-ADC" },
  78. { MAX8997_MUICIRQ_VBVolt, "muic-VB_voltage" },
  79. { MAX8997_MUICIRQ_DBChg, "muic-DB_charger" },
  80. { MAX8997_MUICIRQ_DCDTmr, "muic-DCD_timer" },
  81. { MAX8997_MUICIRQ_ChgDetRun, "muic-CDR_status" },
  82. { MAX8997_MUICIRQ_ChgTyp, "muic-charger_type" },
  83. { MAX8997_MUICIRQ_OVP, "muic-over_voltage" },
  84. };
  85. struct max8997_muic_info {
  86. struct device *dev;
  87. struct i2c_client *muic;
  88. struct max8997_muic_platform_data *muic_pdata;
  89. int irq;
  90. struct work_struct irq_work;
  91. enum max8997_muic_charger_type pre_charger_type;
  92. int pre_adc;
  93. struct mutex mutex;
  94. struct extcon_dev *edev;
  95. };
  96. const char *max8997_extcon_cable[] = {
  97. [0] = "USB",
  98. [1] = "USB-Host",
  99. [2] = "TA",
  100. [3] = "Fast-charger",
  101. [4] = "Slow-charger",
  102. [5] = "Charge-downstream",
  103. [6] = "MHL",
  104. [7] = "Dock-desk",
  105. [8] = "Dock-card",
  106. [9] = "JIG",
  107. NULL,
  108. };
  109. static int max8997_muic_handle_usb(struct max8997_muic_info *info,
  110. enum max8997_muic_usb_type usb_type, bool attached)
  111. {
  112. int ret = 0;
  113. if (usb_type == MAX8997_USB_HOST) {
  114. /* switch to USB */
  115. ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
  116. attached ? MAX8997_SW_USB : MAX8997_SW_OPEN,
  117. SW_MASK);
  118. if (ret) {
  119. dev_err(info->dev, "failed to update muic register\n");
  120. goto out;
  121. }
  122. }
  123. switch (usb_type) {
  124. case MAX8997_USB_HOST:
  125. extcon_set_cable_state(info->edev, "USB-Host", attached);
  126. break;
  127. case MAX8997_USB_DEVICE:
  128. extcon_set_cable_state(info->edev, "USB", attached);
  129. break;
  130. default:
  131. ret = -EINVAL;
  132. break;
  133. }
  134. out:
  135. return ret;
  136. }
  137. static int max8997_muic_handle_dock(struct max8997_muic_info *info,
  138. int adc, bool attached)
  139. {
  140. int ret = 0;
  141. /* switch to AUDIO */
  142. ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
  143. attached ? MAX8997_SW_AUDIO : MAX8997_SW_OPEN,
  144. SW_MASK);
  145. if (ret) {
  146. dev_err(info->dev, "failed to update muic register\n");
  147. goto out;
  148. }
  149. switch (adc) {
  150. case MAX8997_ADC_DESKDOCK:
  151. extcon_set_cable_state(info->edev, "Dock-desk", attached);
  152. break;
  153. case MAX8997_ADC_CARDOCK:
  154. extcon_set_cable_state(info->edev, "Dock-card", attached);
  155. break;
  156. default:
  157. ret = -EINVAL;
  158. break;
  159. }
  160. out:
  161. return ret;
  162. }
  163. static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
  164. bool attached)
  165. {
  166. int ret = 0;
  167. /* switch to UART */
  168. ret = max8997_update_reg(info->muic, MAX8997_MUIC_REG_CONTROL1,
  169. attached ? MAX8997_SW_UART : MAX8997_SW_OPEN,
  170. SW_MASK);
  171. if (ret) {
  172. dev_err(info->dev, "failed to update muic register\n");
  173. goto out;
  174. }
  175. extcon_set_cable_state(info->edev, "JIG", attached);
  176. out:
  177. return ret;
  178. }
  179. static int max8997_muic_handle_adc_detach(struct max8997_muic_info *info)
  180. {
  181. int ret = 0;
  182. switch (info->pre_adc) {
  183. case MAX8997_ADC_GROUND:
  184. ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, false);
  185. break;
  186. case MAX8997_ADC_MHL:
  187. extcon_set_cable_state(info->edev, "MHL", false);
  188. break;
  189. case MAX8997_ADC_JIG_USB_1:
  190. case MAX8997_ADC_JIG_USB_2:
  191. ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, false);
  192. break;
  193. case MAX8997_ADC_DESKDOCK:
  194. case MAX8997_ADC_CARDOCK:
  195. ret = max8997_muic_handle_dock(info, info->pre_adc, false);
  196. break;
  197. case MAX8997_ADC_JIG_UART:
  198. ret = max8997_muic_handle_jig_uart(info, false);
  199. break;
  200. default:
  201. break;
  202. }
  203. return ret;
  204. }
  205. static int max8997_muic_handle_adc(struct max8997_muic_info *info, int adc)
  206. {
  207. int ret = 0;
  208. switch (adc) {
  209. case MAX8997_ADC_GROUND:
  210. ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, true);
  211. break;
  212. case MAX8997_ADC_MHL:
  213. extcon_set_cable_state(info->edev, "MHL", true);
  214. break;
  215. case MAX8997_ADC_JIG_USB_1:
  216. case MAX8997_ADC_JIG_USB_2:
  217. ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, true);
  218. break;
  219. case MAX8997_ADC_DESKDOCK:
  220. case MAX8997_ADC_CARDOCK:
  221. ret = max8997_muic_handle_dock(info, adc, true);
  222. break;
  223. case MAX8997_ADC_JIG_UART:
  224. ret = max8997_muic_handle_jig_uart(info, true);
  225. break;
  226. case MAX8997_ADC_OPEN:
  227. ret = max8997_muic_handle_adc_detach(info);
  228. break;
  229. default:
  230. ret = -EINVAL;
  231. goto out;
  232. }
  233. info->pre_adc = adc;
  234. out:
  235. return ret;
  236. }
  237. static int max8997_muic_handle_charger_type_detach(
  238. struct max8997_muic_info *info)
  239. {
  240. int ret = 0;
  241. switch (info->pre_charger_type) {
  242. case MAX8997_CHARGER_TYPE_USB:
  243. extcon_set_cable_state(info->edev, "USB", false);
  244. break;
  245. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  246. extcon_set_cable_state(info->edev, "Charge-downstream", false);
  247. break;
  248. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  249. extcon_set_cable_state(info->edev, "TA", false);
  250. break;
  251. case MAX8997_CHARGER_TYPE_500MA:
  252. extcon_set_cable_state(info->edev, "Slow-charger", false);
  253. break;
  254. case MAX8997_CHARGER_TYPE_1A:
  255. extcon_set_cable_state(info->edev, "Fast-charger", false);
  256. break;
  257. default:
  258. ret = -EINVAL;
  259. break;
  260. }
  261. return ret;
  262. }
  263. static int max8997_muic_handle_charger_type(struct max8997_muic_info *info,
  264. enum max8997_muic_charger_type charger_type)
  265. {
  266. u8 adc;
  267. int ret;
  268. ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_STATUS1, &adc);
  269. if (ret) {
  270. dev_err(info->dev, "failed to read muic register\n");
  271. goto out;
  272. }
  273. switch (charger_type) {
  274. case MAX8997_CHARGER_TYPE_NONE:
  275. ret = max8997_muic_handle_charger_type_detach(info);
  276. break;
  277. case MAX8997_CHARGER_TYPE_USB:
  278. if ((adc & STATUS1_ADC_MASK) == MAX8997_ADC_OPEN) {
  279. max8997_muic_handle_usb(info,
  280. MAX8997_USB_DEVICE, true);
  281. }
  282. break;
  283. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  284. extcon_set_cable_state(info->edev, "Charge-downstream", true);
  285. break;
  286. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  287. extcon_set_cable_state(info->edev, "TA", true);
  288. break;
  289. case MAX8997_CHARGER_TYPE_500MA:
  290. extcon_set_cable_state(info->edev, "Slow-charger", true);
  291. break;
  292. case MAX8997_CHARGER_TYPE_1A:
  293. extcon_set_cable_state(info->edev, "Fast-charger", true);
  294. break;
  295. default:
  296. ret = -EINVAL;
  297. goto out;
  298. }
  299. info->pre_charger_type = charger_type;
  300. out:
  301. return ret;
  302. }
  303. static void max8997_muic_irq_work(struct work_struct *work)
  304. {
  305. struct max8997_muic_info *info = container_of(work,
  306. struct max8997_muic_info, irq_work);
  307. u8 status[2];
  308. u8 adc, chg_type;
  309. int irq_type = 0;
  310. int i, ret;
  311. mutex_lock(&info->mutex);
  312. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  313. 2, status);
  314. if (ret) {
  315. dev_err(info->dev, "failed to read muic register\n");
  316. mutex_unlock(&info->mutex);
  317. return;
  318. }
  319. dev_dbg(info->dev, "%s: STATUS1:0x%x, 2:0x%x\n", __func__,
  320. status[0], status[1]);
  321. for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
  322. if (info->irq == muic_irqs[i].virq)
  323. irq_type = muic_irqs[i].irq;
  324. switch (irq_type) {
  325. case MAX8997_MUICIRQ_ADC:
  326. adc = status[0] & STATUS1_ADC_MASK;
  327. adc >>= STATUS1_ADC_SHIFT;
  328. max8997_muic_handle_adc(info, adc);
  329. break;
  330. case MAX8997_MUICIRQ_ChgTyp:
  331. chg_type = status[1] & STATUS2_CHGTYP_MASK;
  332. chg_type >>= STATUS2_CHGTYP_SHIFT;
  333. max8997_muic_handle_charger_type(info, chg_type);
  334. break;
  335. default:
  336. dev_info(info->dev, "misc interrupt: irq %d occurred\n",
  337. irq_type);
  338. break;
  339. }
  340. mutex_unlock(&info->mutex);
  341. return;
  342. }
  343. static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
  344. {
  345. struct max8997_muic_info *info = data;
  346. dev_dbg(info->dev, "irq:%d\n", irq);
  347. info->irq = irq;
  348. schedule_work(&info->irq_work);
  349. return IRQ_HANDLED;
  350. }
  351. static void max8997_muic_detect_dev(struct max8997_muic_info *info)
  352. {
  353. int ret;
  354. u8 status[2], adc, chg_type;
  355. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  356. 2, status);
  357. if (ret) {
  358. dev_err(info->dev, "failed to read muic register\n");
  359. return;
  360. }
  361. dev_info(info->dev, "STATUS1:0x%x, STATUS2:0x%x\n",
  362. status[0], status[1]);
  363. adc = status[0] & STATUS1_ADC_MASK;
  364. adc >>= STATUS1_ADC_SHIFT;
  365. chg_type = status[1] & STATUS2_CHGTYP_MASK;
  366. chg_type >>= STATUS2_CHGTYP_SHIFT;
  367. max8997_muic_handle_adc(info, adc);
  368. max8997_muic_handle_charger_type(info, chg_type);
  369. }
  370. static int __devinit max8997_muic_probe(struct platform_device *pdev)
  371. {
  372. struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
  373. struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
  374. struct max8997_muic_info *info;
  375. int ret, i;
  376. info = kzalloc(sizeof(struct max8997_muic_info), GFP_KERNEL);
  377. if (!info) {
  378. dev_err(&pdev->dev, "failed to allocate memory\n");
  379. ret = -ENOMEM;
  380. goto err_kfree;
  381. }
  382. info->dev = &pdev->dev;
  383. info->muic = max8997->muic;
  384. platform_set_drvdata(pdev, info);
  385. mutex_init(&info->mutex);
  386. INIT_WORK(&info->irq_work, max8997_muic_irq_work);
  387. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
  388. struct max8997_muic_irq *muic_irq = &muic_irqs[i];
  389. int virq = 0;
  390. virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
  391. if (!virq)
  392. goto err_irq;
  393. muic_irq->virq = virq;
  394. ret = request_threaded_irq(virq, NULL,max8997_muic_irq_handler,
  395. 0, muic_irq->name, info);
  396. if (ret) {
  397. dev_err(&pdev->dev,
  398. "failed: irq request (IRQ: %d,"
  399. " error :%d)\n",
  400. muic_irq->irq, ret);
  401. goto err_irq;
  402. }
  403. }
  404. /* External connector */
  405. info->edev = kzalloc(sizeof(struct extcon_dev), GFP_KERNEL);
  406. if (!info->edev) {
  407. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  408. ret = -ENOMEM;
  409. goto err_irq;
  410. }
  411. info->edev->name = DEV_NAME;
  412. info->edev->supported_cable = max8997_extcon_cable;
  413. ret = extcon_dev_register(info->edev, NULL);
  414. if (ret) {
  415. dev_err(&pdev->dev, "failed to register extcon device\n");
  416. goto err_extcon;
  417. }
  418. /* Initialize registers according to platform data */
  419. if (pdata->muic_pdata) {
  420. struct max8997_muic_platform_data *mdata = info->muic_pdata;
  421. for (i = 0; i < mdata->num_init_data; i++) {
  422. max8997_write_reg(info->muic, mdata->init_data[i].addr,
  423. mdata->init_data[i].data);
  424. }
  425. }
  426. /* Initial device detection */
  427. max8997_muic_detect_dev(info);
  428. return ret;
  429. err_extcon:
  430. kfree(info->edev);
  431. err_irq:
  432. while (--i >= 0)
  433. free_irq(muic_irqs[i].virq, info);
  434. kfree(info);
  435. err_kfree:
  436. return ret;
  437. }
  438. static int __devexit max8997_muic_remove(struct platform_device *pdev)
  439. {
  440. struct max8997_muic_info *info = platform_get_drvdata(pdev);
  441. int i;
  442. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  443. free_irq(muic_irqs[i].virq, info);
  444. cancel_work_sync(&info->irq_work);
  445. extcon_dev_unregister(info->edev);
  446. kfree(info->edev);
  447. kfree(info);
  448. return 0;
  449. }
  450. static struct platform_driver max8997_muic_driver = {
  451. .driver = {
  452. .name = DEV_NAME,
  453. .owner = THIS_MODULE,
  454. },
  455. .probe = max8997_muic_probe,
  456. .remove = __devexit_p(max8997_muic_remove),
  457. };
  458. module_platform_driver(max8997_muic_driver);
  459. MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
  460. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  461. MODULE_LICENSE("GPL");