extcon-max8997.c 13 KB

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