extcon-max8997.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. enum max8997_muic_adc_debounce_time {
  31. ADC_DEBOUNCE_TIME_0_5MS = 0, /* 0.5ms */
  32. ADC_DEBOUNCE_TIME_10MS, /* 10ms */
  33. ADC_DEBOUNCE_TIME_25MS, /* 25ms */
  34. ADC_DEBOUNCE_TIME_38_62MS, /* 38.62ms */
  35. };
  36. struct max8997_muic_irq {
  37. unsigned int irq;
  38. const char *name;
  39. unsigned int virq;
  40. };
  41. static struct max8997_muic_irq muic_irqs[] = {
  42. { MAX8997_MUICIRQ_ADCError, "muic-ADC_error" },
  43. { MAX8997_MUICIRQ_ADCLow, "muic-ADC_low" },
  44. { MAX8997_MUICIRQ_ADC, "muic-ADC" },
  45. { MAX8997_MUICIRQ_VBVolt, "muic-VB_voltage" },
  46. { MAX8997_MUICIRQ_DBChg, "muic-DB_charger" },
  47. { MAX8997_MUICIRQ_DCDTmr, "muic-DCD_timer" },
  48. { MAX8997_MUICIRQ_ChgDetRun, "muic-CDR_status" },
  49. { MAX8997_MUICIRQ_ChgTyp, "muic-charger_type" },
  50. { MAX8997_MUICIRQ_OVP, "muic-over_voltage" },
  51. };
  52. struct max8997_muic_info {
  53. struct device *dev;
  54. struct i2c_client *muic;
  55. struct max8997_muic_platform_data *muic_pdata;
  56. int irq;
  57. struct work_struct irq_work;
  58. enum max8997_muic_charger_type pre_charger_type;
  59. int pre_adc;
  60. struct mutex mutex;
  61. struct extcon_dev *edev;
  62. };
  63. enum {
  64. EXTCON_CABLE_USB = 0,
  65. EXTCON_CABLE_USB_HOST,
  66. EXTCON_CABLE_TA,
  67. EXTCON_CABLE_FAST_CHARGER,
  68. EXTCON_CABLE_SLOW_CHARGER,
  69. EXTCON_CABLE_CHARGE_DOWNSTREAM,
  70. EXTCON_CABLE_MHL,
  71. EXTCON_CABLE_DOCK_DESK,
  72. EXTCON_CABLE_DOCK_CARD,
  73. EXTCON_CABLE_JIG,
  74. _EXTCON_CABLE_NUM,
  75. };
  76. static const char *max8997_extcon_cable[] = {
  77. [EXTCON_CABLE_USB] = "USB",
  78. [EXTCON_CABLE_USB_HOST] = "USB-Host",
  79. [EXTCON_CABLE_TA] = "TA",
  80. [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
  81. [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
  82. [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
  83. [EXTCON_CABLE_MHL] = "MHL",
  84. [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk",
  85. [EXTCON_CABLE_DOCK_CARD] = "Dock-Card",
  86. [EXTCON_CABLE_JIG] = "JIG",
  87. NULL,
  88. };
  89. /*
  90. * max8997_muic_set_debounce_time - Set the debounce time of ADC
  91. * @info: the instance including private data of max8997 MUIC
  92. * @time: the debounce time of ADC
  93. */
  94. static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
  95. enum max8997_muic_adc_debounce_time time)
  96. {
  97. int ret;
  98. switch (time) {
  99. case ADC_DEBOUNCE_TIME_0_5MS:
  100. case ADC_DEBOUNCE_TIME_10MS:
  101. case ADC_DEBOUNCE_TIME_25MS:
  102. case ADC_DEBOUNCE_TIME_38_62MS:
  103. ret = max8997_update_reg(info->muic,
  104. MAX8997_MUIC_REG_CONTROL3,
  105. time << CONTROL3_ADCDBSET_SHIFT,
  106. CONTROL3_ADCDBSET_MASK);
  107. if (ret) {
  108. dev_err(info->dev, "failed to set ADC debounce time\n");
  109. return -EAGAIN;
  110. }
  111. break;
  112. default:
  113. dev_err(info->dev, "invalid ADC debounce time\n");
  114. return -EINVAL;
  115. }
  116. return 0;
  117. };
  118. /*
  119. * max8997_muic_set_path - Set hardware line according to attached cable
  120. * @info: the instance including private data of max8997 MUIC
  121. * @value: the path according to attached cable
  122. * @attached: the state of cable (true:attached, false:detached)
  123. *
  124. * The max8997 MUIC device share outside H/W line among a varity of cables,
  125. * so this function set internal path of H/W line according to the type of
  126. * attached cable.
  127. */
  128. static int max8997_muic_set_path(struct max8997_muic_info *info,
  129. u8 val, bool attached)
  130. {
  131. int ret = 0;
  132. u8 ctrl1, ctrl2 = 0;
  133. if (attached)
  134. ctrl1 = val;
  135. else
  136. ctrl1 = CONTROL1_SW_OPEN;
  137. ret = max8997_update_reg(info->muic,
  138. MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
  139. if (ret < 0) {
  140. dev_err(info->dev, "failed to update MUIC register\n");
  141. return -EAGAIN;
  142. }
  143. if (attached)
  144. ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
  145. else
  146. ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
  147. ret = max8997_update_reg(info->muic,
  148. MAX8997_MUIC_REG_CONTROL2, ctrl2,
  149. CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
  150. if (ret < 0) {
  151. dev_err(info->dev, "failed to update MUIC register\n");
  152. return -EAGAIN;
  153. }
  154. dev_info(info->dev,
  155. "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
  156. ctrl1, ctrl2, attached ? "attached" : "detached");
  157. return 0;
  158. }
  159. static int max8997_muic_handle_usb(struct max8997_muic_info *info,
  160. enum max8997_muic_usb_type usb_type, bool attached)
  161. {
  162. int ret = 0;
  163. if (usb_type == MAX8997_USB_HOST) {
  164. ret = max8997_muic_set_path(info, CONTROL1_SW_USB, attached);
  165. if (ret) {
  166. dev_err(info->dev, "failed to update muic register\n");
  167. goto out;
  168. }
  169. }
  170. switch (usb_type) {
  171. case MAX8997_USB_HOST:
  172. extcon_set_cable_state(info->edev, "USB-Host", attached);
  173. break;
  174. case MAX8997_USB_DEVICE:
  175. extcon_set_cable_state(info->edev, "USB", attached);
  176. break;
  177. default:
  178. ret = -EINVAL;
  179. break;
  180. }
  181. out:
  182. return ret;
  183. }
  184. static int max8997_muic_handle_dock(struct max8997_muic_info *info,
  185. int adc, bool attached)
  186. {
  187. int ret = 0;
  188. ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
  189. if (ret) {
  190. dev_err(info->dev, "failed to update muic register\n");
  191. goto out;
  192. }
  193. switch (adc) {
  194. case MAX8997_ADC_DESKDOCK:
  195. extcon_set_cable_state(info->edev, "Dock-desk", attached);
  196. break;
  197. case MAX8997_ADC_CARDOCK:
  198. extcon_set_cable_state(info->edev, "Dock-card", attached);
  199. break;
  200. default:
  201. ret = -EINVAL;
  202. break;
  203. }
  204. out:
  205. return ret;
  206. }
  207. static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
  208. bool attached)
  209. {
  210. int ret = 0;
  211. /* switch to UART */
  212. ret = max8997_muic_set_path(info, CONTROL1_SW_UART, attached);
  213. if (ret) {
  214. dev_err(info->dev, "failed to update muic register\n");
  215. goto out;
  216. }
  217. extcon_set_cable_state(info->edev, "JIG", attached);
  218. out:
  219. return ret;
  220. }
  221. static int max8997_muic_handle_adc_detach(struct max8997_muic_info *info)
  222. {
  223. int ret = 0;
  224. switch (info->pre_adc) {
  225. case MAX8997_ADC_GROUND:
  226. ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, false);
  227. break;
  228. case MAX8997_ADC_MHL:
  229. extcon_set_cable_state(info->edev, "MHL", false);
  230. break;
  231. case MAX8997_ADC_JIG_USB_1:
  232. case MAX8997_ADC_JIG_USB_2:
  233. ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, false);
  234. break;
  235. case MAX8997_ADC_DESKDOCK:
  236. case MAX8997_ADC_CARDOCK:
  237. ret = max8997_muic_handle_dock(info, info->pre_adc, false);
  238. break;
  239. case MAX8997_ADC_JIG_UART:
  240. ret = max8997_muic_handle_jig_uart(info, false);
  241. break;
  242. default:
  243. break;
  244. }
  245. return ret;
  246. }
  247. static int max8997_muic_handle_adc(struct max8997_muic_info *info, int adc)
  248. {
  249. int ret = 0;
  250. switch (adc) {
  251. case MAX8997_ADC_GROUND:
  252. ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, true);
  253. break;
  254. case MAX8997_ADC_MHL:
  255. extcon_set_cable_state(info->edev, "MHL", true);
  256. break;
  257. case MAX8997_ADC_JIG_USB_1:
  258. case MAX8997_ADC_JIG_USB_2:
  259. ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, true);
  260. break;
  261. case MAX8997_ADC_DESKDOCK:
  262. case MAX8997_ADC_CARDOCK:
  263. ret = max8997_muic_handle_dock(info, adc, true);
  264. break;
  265. case MAX8997_ADC_JIG_UART:
  266. ret = max8997_muic_handle_jig_uart(info, true);
  267. break;
  268. case MAX8997_ADC_OPEN:
  269. ret = max8997_muic_handle_adc_detach(info);
  270. break;
  271. default:
  272. ret = -EINVAL;
  273. goto out;
  274. }
  275. info->pre_adc = adc;
  276. out:
  277. return ret;
  278. }
  279. static int max8997_muic_handle_charger_type_detach(
  280. struct max8997_muic_info *info)
  281. {
  282. switch (info->pre_charger_type) {
  283. case MAX8997_CHARGER_TYPE_USB:
  284. extcon_set_cable_state(info->edev, "USB", false);
  285. break;
  286. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  287. extcon_set_cable_state(info->edev, "Charge-downstream", false);
  288. break;
  289. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  290. extcon_set_cable_state(info->edev, "TA", false);
  291. break;
  292. case MAX8997_CHARGER_TYPE_500MA:
  293. extcon_set_cable_state(info->edev, "Slow-charger", false);
  294. break;
  295. case MAX8997_CHARGER_TYPE_1A:
  296. extcon_set_cable_state(info->edev, "Fast-charger", false);
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. return 0;
  302. }
  303. static int max8997_muic_handle_charger_type(struct max8997_muic_info *info,
  304. enum max8997_muic_charger_type charger_type)
  305. {
  306. u8 adc;
  307. int ret;
  308. ret = max8997_read_reg(info->muic, MAX8997_MUIC_REG_STATUS1, &adc);
  309. if (ret) {
  310. dev_err(info->dev, "failed to read muic register\n");
  311. goto out;
  312. }
  313. switch (charger_type) {
  314. case MAX8997_CHARGER_TYPE_NONE:
  315. ret = max8997_muic_handle_charger_type_detach(info);
  316. break;
  317. case MAX8997_CHARGER_TYPE_USB:
  318. if ((adc & STATUS1_ADC_MASK) == MAX8997_ADC_OPEN) {
  319. max8997_muic_handle_usb(info,
  320. MAX8997_USB_DEVICE, true);
  321. }
  322. break;
  323. case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
  324. extcon_set_cable_state(info->edev, "Charge-downstream", true);
  325. break;
  326. case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
  327. extcon_set_cable_state(info->edev, "TA", true);
  328. break;
  329. case MAX8997_CHARGER_TYPE_500MA:
  330. extcon_set_cable_state(info->edev, "Slow-charger", true);
  331. break;
  332. case MAX8997_CHARGER_TYPE_1A:
  333. extcon_set_cable_state(info->edev, "Fast-charger", true);
  334. break;
  335. default:
  336. ret = -EINVAL;
  337. goto out;
  338. }
  339. info->pre_charger_type = charger_type;
  340. out:
  341. return ret;
  342. }
  343. static void max8997_muic_irq_work(struct work_struct *work)
  344. {
  345. struct max8997_muic_info *info = container_of(work,
  346. struct max8997_muic_info, irq_work);
  347. u8 status[2];
  348. u8 adc, chg_type;
  349. int irq_type = 0;
  350. int i, ret;
  351. mutex_lock(&info->mutex);
  352. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  353. 2, status);
  354. if (ret) {
  355. dev_err(info->dev, "failed to read muic register\n");
  356. mutex_unlock(&info->mutex);
  357. return;
  358. }
  359. dev_dbg(info->dev, "%s: STATUS1:0x%x, 2:0x%x\n", __func__,
  360. status[0], status[1]);
  361. for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++)
  362. if (info->irq == muic_irqs[i].virq)
  363. irq_type = muic_irqs[i].irq;
  364. switch (irq_type) {
  365. case MAX8997_MUICIRQ_ADC:
  366. adc = status[0] & STATUS1_ADC_MASK;
  367. adc >>= STATUS1_ADC_SHIFT;
  368. max8997_muic_handle_adc(info, adc);
  369. break;
  370. case MAX8997_MUICIRQ_ChgTyp:
  371. chg_type = status[1] & STATUS2_CHGTYP_MASK;
  372. chg_type >>= STATUS2_CHGTYP_SHIFT;
  373. max8997_muic_handle_charger_type(info, chg_type);
  374. break;
  375. default:
  376. dev_info(info->dev, "misc interrupt: irq %d occurred\n",
  377. irq_type);
  378. break;
  379. }
  380. mutex_unlock(&info->mutex);
  381. return;
  382. }
  383. static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
  384. {
  385. struct max8997_muic_info *info = data;
  386. dev_dbg(info->dev, "irq:%d\n", irq);
  387. info->irq = irq;
  388. schedule_work(&info->irq_work);
  389. return IRQ_HANDLED;
  390. }
  391. static void max8997_muic_detect_dev(struct max8997_muic_info *info)
  392. {
  393. int ret;
  394. u8 status[2], adc, chg_type;
  395. ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
  396. 2, status);
  397. if (ret) {
  398. dev_err(info->dev, "failed to read muic register\n");
  399. return;
  400. }
  401. dev_info(info->dev, "STATUS1:0x%x, STATUS2:0x%x\n",
  402. status[0], status[1]);
  403. adc = status[0] & STATUS1_ADC_MASK;
  404. adc >>= STATUS1_ADC_SHIFT;
  405. chg_type = status[1] & STATUS2_CHGTYP_MASK;
  406. chg_type >>= STATUS2_CHGTYP_SHIFT;
  407. max8997_muic_handle_adc(info, adc);
  408. max8997_muic_handle_charger_type(info, chg_type);
  409. }
  410. static int max8997_muic_probe(struct platform_device *pdev)
  411. {
  412. struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
  413. struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
  414. struct max8997_muic_info *info;
  415. int ret, i;
  416. info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
  417. GFP_KERNEL);
  418. if (!info) {
  419. dev_err(&pdev->dev, "failed to allocate memory\n");
  420. return -ENOMEM;
  421. }
  422. info->dev = &pdev->dev;
  423. info->muic = max8997->muic;
  424. platform_set_drvdata(pdev, info);
  425. mutex_init(&info->mutex);
  426. INIT_WORK(&info->irq_work, max8997_muic_irq_work);
  427. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
  428. struct max8997_muic_irq *muic_irq = &muic_irqs[i];
  429. unsigned int virq = 0;
  430. virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
  431. if (!virq) {
  432. ret = -EINVAL;
  433. goto err_irq;
  434. }
  435. muic_irq->virq = virq;
  436. ret = request_threaded_irq(virq, NULL,
  437. max8997_muic_irq_handler,
  438. IRQF_NO_SUSPEND,
  439. muic_irq->name, info);
  440. if (ret) {
  441. dev_err(&pdev->dev,
  442. "failed: irq request (IRQ: %d,"
  443. " error :%d)\n",
  444. muic_irq->irq, ret);
  445. goto err_irq;
  446. }
  447. }
  448. /* External connector */
  449. info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev),
  450. GFP_KERNEL);
  451. if (!info->edev) {
  452. dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
  453. ret = -ENOMEM;
  454. goto err_irq;
  455. }
  456. info->edev->name = DEV_NAME;
  457. info->edev->supported_cable = max8997_extcon_cable;
  458. ret = extcon_dev_register(info->edev, NULL);
  459. if (ret) {
  460. dev_err(&pdev->dev, "failed to register extcon device\n");
  461. goto err_irq;
  462. }
  463. /* Initialize registers according to platform data */
  464. if (pdata->muic_pdata) {
  465. struct max8997_muic_platform_data *mdata = info->muic_pdata;
  466. for (i = 0; i < mdata->num_init_data; i++) {
  467. max8997_write_reg(info->muic, mdata->init_data[i].addr,
  468. mdata->init_data[i].data);
  469. }
  470. }
  471. /* Set ADC debounce time */
  472. max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
  473. /* Initial device detection */
  474. max8997_muic_detect_dev(info);
  475. return ret;
  476. err_irq:
  477. while (--i >= 0)
  478. free_irq(muic_irqs[i].virq, info);
  479. return ret;
  480. }
  481. static int max8997_muic_remove(struct platform_device *pdev)
  482. {
  483. struct max8997_muic_info *info = platform_get_drvdata(pdev);
  484. int i;
  485. for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
  486. free_irq(muic_irqs[i].virq, info);
  487. cancel_work_sync(&info->irq_work);
  488. extcon_dev_unregister(info->edev);
  489. return 0;
  490. }
  491. static struct platform_driver max8997_muic_driver = {
  492. .driver = {
  493. .name = DEV_NAME,
  494. .owner = THIS_MODULE,
  495. },
  496. .probe = max8997_muic_probe,
  497. .remove = max8997_muic_remove,
  498. };
  499. module_platform_driver(max8997_muic_driver);
  500. MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
  501. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  502. MODULE_LICENSE("GPL");