intel_soc_pmic_core.c 5.3 KB

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