extcon-max8997.c 13 KB

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