extcon-max8997.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  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. switch (info->pre_charger_type) {
  241. case MAX8997_CHARGER_TYPE_USB:
  242. extcon_set_cable_state(info->edev, "USB", false);
  243. break;
  244. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  245. extcon_set_cable_state(info->edev, "Charge-downstream", false);
  246. break;
  247. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  248. extcon_set_cable_state(info->edev, "TA", false);
  249. break;
  250. case MAX8997_CHARGER_TYPE_500MA:
  251. extcon_set_cable_state(info->edev, "Slow-charger", false);
  252. break;
  253. case MAX8997_CHARGER_TYPE_1A:
  254. extcon_set_cable_state(info->edev, "Fast-charger", false);
  255. break;
  256. default:
  257. return -EINVAL;
  258. break;
  259. }
  260. return 0;
  261. }
  262. static int max8997_muic_handle_charger_type(struct max8997_muic_info *info,
  263. enum max8997_muic_charger_type charger_type)
  264. {
  265. u8 adc;
  266. int ret;
  267. ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_STATUS1, &adc);
  268. if (ret) {
  269. dev_err(info->dev, "failed to read muic register\n");
  270. goto out;
  271. }
  272. switch (charger_type) {
  273. case MAX8997_CHARGER_TYPE_NONE:
  274. ret = max8997_muic_handle_charger_type_detach(info);
  275. break;
  276. case MAX8997_CHARGER_TYPE_USB:
  277. if ((adc & STATUS1_ADC_MASK) == MAX8997_ADC_OPEN) {
  278. max8997_muic_handle_usb(info,
  279. MAX8997_USB_DEVICE, true);
  280. }
  281. break;
  282. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  283. extcon_set_cable_state(info->edev, "Charge-downstream", true);
  284. break;
  285. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  286. extcon_set_cable_state(info->edev, "TA", true);
  287. break;
  288. case MAX8997_CHARGER_TYPE_500MA:
  289. extcon_set_cable_state(info->edev, "Slow-charger", true);
  290. break;
  291. case MAX8997_CHARGER_TYPE_1A:
  292. extcon_set_cable_state(info->edev, "Fast-charger", true);
  293. break;
  294. default:
  295. ret = -EINVAL;
  296. goto out;
  297. }
  298. info->pre_charger_type = charger_type;
  299. out:
  300. return ret;
  301. }
  302. static void max8997_muic_irq_work(struct work_struct *work)
  303. {
  304. struct max8997_muic_info *info = container_of(work,
  305. struct max8997_muic_info, irq_work);
  306. u8 status[2];
  307. u8 adc, chg_type;
  308. int irq_type = 0;
  309. int i, ret;
  310. mutex_lock(&info->mutex);
  311. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  312. 2, status);
  313. if (ret) {
  314. dev_err(info->dev, "failed to read muic register\n");
  315. mutex_unlock(&info->mutex);
  316. return;
  317. }
  318. dev_dbg(info->dev, "%s: STATUS1:0x%x, 2:0x%x\n", __func__,
  319. status[0], status[1]);
  320. for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
  321. if (info->irq == muic_irqs[i].virq)
  322. irq_type = muic_irqs[i].irq;
  323. switch (irq_type) {
  324. case MAX8997_MUICIRQ_ADC:
  325. adc = status[0] & STATUS1_ADC_MASK;
  326. adc >>= STATUS1_ADC_SHIFT;
  327. max8997_muic_handle_adc(info, adc);
  328. break;
  329. case MAX8997_MUICIRQ_ChgTyp:
  330. chg_type = status[1] & STATUS2_CHGTYP_MASK;
  331. chg_type >>= STATUS2_CHGTYP_SHIFT;
  332. max8997_muic_handle_charger_type(info, chg_type);
  333. break;
  334. default:
  335. dev_info(info->dev, "misc interrupt: irq %d occurred\n",
  336. irq_type);
  337. break;
  338. }
  339. mutex_unlock(&info->mutex);
  340. return;
  341. }
  342. static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
  343. {
  344. struct max8997_muic_info *info = data;
  345. dev_dbg(info->dev, "irq:%d\n", irq);
  346. info->irq = irq;
  347. schedule_work(&info->irq_work);
  348. return IRQ_HANDLED;
  349. }
  350. static void max8997_muic_detect_dev(struct max8997_muic_info *info)
  351. {
  352. int ret;
  353. u8 status[2], adc, chg_type;
  354. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  355. 2, status);
  356. if (ret) {
  357. dev_err(info->dev, "failed to read muic register\n");
  358. return;
  359. }
  360. dev_info(info->dev, "STATUS1:0x%x, STATUS2:0x%x\n",
  361. status[0], status[1]);
  362. adc = status[0] & STATUS1_ADC_MASK;
  363. adc >>= STATUS1_ADC_SHIFT;
  364. chg_type = status[1] & STATUS2_CHGTYP_MASK;
  365. chg_type >>= STATUS2_CHGTYP_SHIFT;
  366. max8997_muic_handle_adc(info, adc);
  367. max8997_muic_handle_charger_type(info, chg_type);
  368. }
  369. static int max8997_muic_probe(struct platform_device *pdev)
  370. {
  371. struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
  372. struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
  373. struct max8997_muic_info *info;
  374. int ret, i;
  375. info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
  376. GFP_KERNEL);
  377. if (!info) {
  378. dev_err(&pdev->dev, "failed to allocate memory\n");
  379. return -ENOMEM;
  380. }
  381. info->dev = &pdev->dev;
  382. info->muic = max8997->muic;
  383. platform_set_drvdata(pdev, info);
  384. mutex_init(&info->mutex);
  385. INIT_WORK(&info->irq_work, max8997_muic_irq_work);
  386. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
  387. struct max8997_muic_irq *muic_irq = &muic_irqs[i];
  388. unsigned int virq = 0;
  389. virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
  390. if (!virq) {
  391. ret = -EINVAL;
  392. goto err_irq;
  393. }
  394. muic_irq->virq = virq;
  395. ret = request_threaded_irq(virq, NULL, max8997_muic_irq_handler,
  396. 0, muic_irq->name, info);
  397. if (ret) {
  398. dev_err(&pdev->dev,
  399. "failed: irq request (IRQ: %d,"
  400. " error :%d)\n",
  401. muic_irq->irq, ret);
  402. goto err_irq;
  403. }
  404. }
  405. /* External connector */
  406. info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
  407. GFP_KERNEL);
  408. if (!info->edev) {
  409. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  410. ret = -ENOMEM;
  411. goto err_irq;
  412. }
  413. info->edev->name = DEV_NAME;
  414. info->edev->supported_cable = max8997_extcon_cable;
  415. ret = extcon_dev_register(info->edev, NULL);
  416. if (ret) {
  417. dev_err(&pdev->dev, "failed to register extcon device\n");
  418. goto err_irq;
  419. }
  420. /* Initialize registers according to platform data */
  421. if (pdata->muic_pdata) {
  422. struct max8997_muic_platform_data *mdata = info->muic_pdata;
  423. for (i = 0; i < mdata->num_init_data; i++) {
  424. max8997_write_reg(info->muic, mdata->init_data[i].addr,
  425. mdata->init_data[i].data);
  426. }
  427. }
  428. /* Initial device detection */
  429. max8997_muic_detect_dev(info);
  430. return ret;
  431. err_irq:
  432. while (--i >= 0)
  433. free_irq(muic_irqs[i].virq, info);
  434. return ret;
  435. }
  436. static int max8997_muic_remove(struct platform_device *pdev)
  437. {
  438. struct max8997_muic_info *info = platform_get_drvdata(pdev);
  439. int i;
  440. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  441. free_irq(muic_irqs[i].virq, info);
  442. cancel_work_sync(&info->irq_work);
  443. extcon_dev_unregister(info->edev);
  444. return 0;
  445. }
  446. static struct platform_driver max8997_muic_driver = {
  447. .driver = {
  448. .name = DEV_NAME,
  449. .owner = THIS_MODULE,
  450. },
  451. .probe = max8997_muic_probe,
  452. .remove = max8997_muic_remove,
  453. };
  454. module_platform_driver(max8997_muic_driver);
  455. MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
  456. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  457. MODULE_LICENSE("GPL");