intel_soc_pmic_core.c 5.3 KB

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