max8997-muic.c 12 KB

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