intel_soc_pmic_core.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * intel_soc_pmic_core.c - Intel SoC PMIC MFD Driver
  3. *
  4. * Copyright (C) 2013, 2014 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Author: Yang, Bin <bin.yang@intel.com>
  16. * Author: Zhu, Lejun <lejun.zhu@linux.intel.com>
  17. */
  18. #include <linux/module.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/i2c.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/acpi.h>
  24. #include <linux/regmap.h>
  25. #include <linux/mfd/intel_soc_pmic.h>
  26. #include "intel_soc_pmic_core.h"
  27. /*
  28. * On some boards the PMIC interrupt may come from a GPIO line.
  29. * Try to lookup the ACPI table and see if such connection exists. If not,
  30. * return -ENOENT and use the IRQ provided by I2C.
  31. */
  32. static int intel_soc_pmic_find_gpio_irq(struct device *dev)
  33. {
  34. struct gpio_desc *desc;
  35. int irq;
  36. desc = devm_gpiod_get_index(dev, "intel_soc_pmic", 0);
  37. if (IS_ERR(desc))
  38. return -ENOENT;
  39. irq = gpiod_to_irq(desc);
  40. if (irq < 0)
  41. dev_warn(dev, "Can't get irq: %d\n", irq);
  42. return irq;
  43. }
  44. static int intel_soc_pmic_i2c_probe(struct i2c_client *i2c,
  45. const struct i2c_device_id *i2c_id)
  46. {
  47. struct device *dev = &i2c->dev;
  48. const struct acpi_device_id *id;
  49. struct intel_soc_pmic_config *config;
  50. struct intel_soc_pmic *pmic;
  51. int ret;
  52. int irq;
  53. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  54. if (!id || !id->driver_data)
  55. return -ENODEV;
  56. config = (struct intel_soc_pmic_config *)id->driver_data;
  57. pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
  58. dev_set_drvdata(dev, pmic);
  59. pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config);
  60. irq = intel_soc_pmic_find_gpio_irq(dev);
  61. pmic->irq = (irq < 0) ? i2c->irq : irq;
  62. ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
  63. config->irq_flags | IRQF_ONESHOT,
  64. 0, config->irq_chip,
  65. &pmic->irq_chip_data);
  66. if (ret)
  67. return ret;
  68. ret = enable_irq_wake(pmic->irq);
  69. if (ret)
  70. dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret);
  71. ret = mfd_add_devices(dev, -1, config->cell_dev,
  72. config->n_cell_devs, NULL, 0,
  73. regmap_irq_get_domain(pmic->irq_chip_data));
  74. if (ret)
  75. goto err_del_irq_chip;
  76. return 0;
  77. err_del_irq_chip:
  78. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
  79. return ret;
  80. }
  81. static int intel_soc_pmic_i2c_remove(struct i2c_client *i2c)
  82. {
  83. struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
  84. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
  85. mfd_remove_devices(&i2c->dev);
  86. return 0;
  87. }
  88. static void intel_soc_pmic_shutdown(struct i2c_client *i2c)
  89. {
  90. struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
  91. disable_irq(pmic->irq);
  92. return;
  93. }
  94. #if defined(CONFIG_PM_SLEEP)
  95. static int intel_soc_pmic_suspend(struct device *dev)
  96. {
  97. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  98. disable_irq(pmic->irq);
  99. return 0;
  100. }
  101. static int intel_soc_pmic_resume(struct device *dev)
  102. {
  103. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  104. enable_irq(pmic->irq);
  105. return 0;
  106. }
  107. #endif
  108. static SIMPLE_DEV_PM_OPS(intel_soc_pmic_pm_ops, intel_soc_pmic_suspend,
  109. intel_soc_pmic_resume);
  110. static const struct i2c_device_id intel_soc_pmic_i2c_id[] = {
  111. { }
  112. };
  113. MODULE_DEVICE_TABLE(i2c, intel_soc_pmic_i2c_id);
  114. #if defined(CONFIG_ACPI)
  115. static struct acpi_device_id intel_soc_pmic_acpi_match[] = {
  116. {"INT33FD", (kernel_ulong_t)&intel_soc_pmic_config_crc},
  117. { },
  118. };
  119. MODULE_DEVICE_TABLE(acpi, intel_soc_pmic_acpi_match);
  120. #endif
  121. static struct i2c_driver intel_soc_pmic_i2c_driver = {
  122. .driver = {
  123. .name = "intel_soc_pmic_i2c",
  124. .owner = THIS_MODULE,
  125. .pm = &intel_soc_pmic_pm_ops,
  126. .acpi_match_table = ACPI_PTR(intel_soc_pmic_acpi_match),
  127. },
  128. .probe = intel_soc_pmic_i2c_probe,
  129. .remove = intel_soc_pmic_i2c_remove,
  130. .id_table = intel_soc_pmic_i2c_id,
  131. .shutdown = intel_soc_pmic_shutdown,
  132. };
  133. module_i2c_driver(intel_soc_pmic_i2c_driver);
  134. MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC");
  135. MODULE_LICENSE("GPL v2");
  136. MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
  137. MODULE_AUTHOR("Zhu, Lejun <lejun.zhu@linux.intel.com>");