intel_soc_pmic_core.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. static int intel_soc_pmic_find_gpio_irq(struct device *dev)
  28. {
  29. struct gpio_desc *desc;
  30. int irq;
  31. desc = devm_gpiod_get_index(dev, "intel_soc_pmic", 0, GPIOD_IN);
  32. if (IS_ERR(desc))
  33. return PTR_ERR(desc);
  34. irq = gpiod_to_irq(desc);
  35. if (irq < 0)
  36. dev_warn(dev, "Can't get irq: %d\n", irq);
  37. return irq;
  38. }
  39. static int intel_soc_pmic_i2c_probe(struct i2c_client *i2c,
  40. const struct i2c_device_id *i2c_id)
  41. {
  42. struct device *dev = &i2c->dev;
  43. const struct acpi_device_id *id;
  44. struct intel_soc_pmic_config *config;
  45. struct intel_soc_pmic *pmic;
  46. int ret;
  47. int irq;
  48. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  49. if (!id || !id->driver_data)
  50. return -ENODEV;
  51. config = (struct intel_soc_pmic_config *)id->driver_data;
  52. pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
  53. if (!pmic)
  54. return -ENOMEM;
  55. dev_set_drvdata(dev, pmic);
  56. pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config);
  57. /*
  58. * On some boards the PMIC interrupt may come from a GPIO line. Try to
  59. * lookup the ACPI table for a such connection and setup a GPIO
  60. * interrupt if it exists. Otherwise use the IRQ provided by I2C
  61. */
  62. irq = intel_soc_pmic_find_gpio_irq(dev);
  63. pmic->irq = (irq < 0) ? i2c->irq : irq;
  64. ret = regmap_add_irq_chip(pmic->regmap, pmic->irq,
  65. config->irq_flags | IRQF_ONESHOT,
  66. 0, config->irq_chip,
  67. &pmic->irq_chip_data);
  68. if (ret)
  69. return ret;
  70. ret = enable_irq_wake(pmic->irq);
  71. if (ret)
  72. dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret);
  73. ret = mfd_add_devices(dev, -1, config->cell_dev,
  74. config->n_cell_devs, NULL, 0,
  75. regmap_irq_get_domain(pmic->irq_chip_data));
  76. if (ret)
  77. goto err_del_irq_chip;
  78. return 0;
  79. err_del_irq_chip:
  80. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
  81. return ret;
  82. }
  83. static int intel_soc_pmic_i2c_remove(struct i2c_client *i2c)
  84. {
  85. struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
  86. regmap_del_irq_chip(pmic->irq, pmic->irq_chip_data);
  87. mfd_remove_devices(&i2c->dev);
  88. return 0;
  89. }
  90. static void intel_soc_pmic_shutdown(struct i2c_client *i2c)
  91. {
  92. struct intel_soc_pmic *pmic = dev_get_drvdata(&i2c->dev);
  93. disable_irq(pmic->irq);
  94. return;
  95. }
  96. #if defined(CONFIG_PM_SLEEP)
  97. static int intel_soc_pmic_suspend(struct device *dev)
  98. {
  99. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  100. disable_irq(pmic->irq);
  101. return 0;
  102. }
  103. static int intel_soc_pmic_resume(struct device *dev)
  104. {
  105. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  106. enable_irq(pmic->irq);
  107. return 0;
  108. }
  109. #endif
  110. static SIMPLE_DEV_PM_OPS(intel_soc_pmic_pm_ops, intel_soc_pmic_suspend,
  111. intel_soc_pmic_resume);
  112. static const struct i2c_device_id intel_soc_pmic_i2c_id[] = {
  113. { }
  114. };
  115. MODULE_DEVICE_TABLE(i2c, intel_soc_pmic_i2c_id);
  116. #if defined(CONFIG_ACPI)
  117. static struct acpi_device_id intel_soc_pmic_acpi_match[] = {
  118. {"INT33FD", (kernel_ulong_t)&intel_soc_pmic_config_crc},
  119. { },
  120. };
  121. MODULE_DEVICE_TABLE(acpi, intel_soc_pmic_acpi_match);
  122. #endif
  123. static struct i2c_driver intel_soc_pmic_i2c_driver = {
  124. .driver = {
  125. .name = "intel_soc_pmic_i2c",
  126. .owner = THIS_MODULE,
  127. .pm = &intel_soc_pmic_pm_ops,
  128. .acpi_match_table = ACPI_PTR(intel_soc_pmic_acpi_match),
  129. },
  130. .probe = intel_soc_pmic_i2c_probe,
  131. .remove = intel_soc_pmic_i2c_remove,
  132. .id_table = intel_soc_pmic_i2c_id,
  133. .shutdown = intel_soc_pmic_shutdown,
  134. };
  135. module_i2c_driver(intel_soc_pmic_i2c_driver);
  136. MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC");
  137. MODULE_LICENSE("GPL v2");
  138. MODULE_AUTHOR("Yang, Bin <bin.yang@intel.com>");
  139. MODULE_AUTHOR("Zhu, Lejun <lejun.zhu@linux.intel.com>");