of_extcon.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * OF helpers for External connector (extcon) framework
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. * Kishon Vijay Abraham I <kishon@ti.com>
  6. *
  7. * Copyright (C) 2013 Samsung Electronics
  8. * Chanwoo Choi <cw00.choi@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/extcon.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/extcon/of_extcon.h>
  22. /*
  23. * of_extcon_get_extcon_dev - Get the name of extcon device from devicetree
  24. * @dev - instance to the given device
  25. * @index - index into list of extcon_dev
  26. *
  27. * return the instance of extcon device
  28. */
  29. struct extcon_dev *of_extcon_get_extcon_dev(struct device *dev, int index)
  30. {
  31. struct device_node *node;
  32. struct extcon_dev *edev;
  33. struct platform_device *extcon_parent_dev;
  34. if (!dev->of_node) {
  35. dev_dbg(dev, "device does not have a device node entry\n");
  36. return ERR_PTR(-EINVAL);
  37. }
  38. node = of_parse_phandle(dev->of_node, "extcon", index);
  39. if (!node) {
  40. dev_dbg(dev, "failed to get phandle in %s node\n",
  41. dev->of_node->full_name);
  42. return ERR_PTR(-ENODEV);
  43. }
  44. extcon_parent_dev = of_find_device_by_node(node);
  45. if (!extcon_parent_dev) {
  46. dev_dbg(dev, "unable to find device by node\n");
  47. return ERR_PTR(-EPROBE_DEFER);
  48. }
  49. edev = extcon_get_extcon_dev(dev_name(&extcon_parent_dev->dev));
  50. if (!edev) {
  51. dev_dbg(dev, "unable to get extcon device : %s\n",
  52. dev_name(&extcon_parent_dev->dev));
  53. return ERR_PTR(-ENODEV);
  54. }
  55. return edev;
  56. }
  57. EXPORT_SYMBOL_GPL(of_extcon_get_extcon_dev);