intel_soc_pmic_crc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Device access for Crystal Cove PMIC
  4. *
  5. * Copyright (C) 2012-2014, 2022 Intel Corporation. All rights reserved.
  6. *
  7. * Author: Yang, Bin <[email protected]>
  8. * Author: Zhu, Lejun <[email protected]>
  9. */
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/mfd/intel_soc_pmic.h>
  16. #include <linux/platform_data/x86/soc.h>
  17. #include <linux/pwm.h>
  18. #include <linux/regmap.h>
  19. #define CRYSTAL_COVE_MAX_REGISTER 0xC6
  20. #define CRYSTAL_COVE_REG_IRQLVL1 0x02
  21. #define CRYSTAL_COVE_REG_MIRQLVL1 0x0E
  22. #define CRYSTAL_COVE_IRQ_PWRSRC 0
  23. #define CRYSTAL_COVE_IRQ_THRM 1
  24. #define CRYSTAL_COVE_IRQ_BCU 2
  25. #define CRYSTAL_COVE_IRQ_ADC 3
  26. #define CRYSTAL_COVE_IRQ_CHGR 4
  27. #define CRYSTAL_COVE_IRQ_GPIO 5
  28. #define CRYSTAL_COVE_IRQ_VHDMIOCP 6
  29. static const struct resource pwrsrc_resources[] = {
  30. DEFINE_RES_IRQ_NAMED(CRYSTAL_COVE_IRQ_PWRSRC, "PWRSRC"),
  31. };
  32. static const struct resource thermal_resources[] = {
  33. DEFINE_RES_IRQ_NAMED(CRYSTAL_COVE_IRQ_THRM, "THERMAL"),
  34. };
  35. static const struct resource bcu_resources[] = {
  36. DEFINE_RES_IRQ_NAMED(CRYSTAL_COVE_IRQ_BCU, "BCU"),
  37. };
  38. static const struct resource adc_resources[] = {
  39. DEFINE_RES_IRQ_NAMED(CRYSTAL_COVE_IRQ_ADC, "ADC"),
  40. };
  41. static const struct resource charger_resources[] = {
  42. DEFINE_RES_IRQ_NAMED(CRYSTAL_COVE_IRQ_CHGR, "CHGR"),
  43. };
  44. static const struct resource gpio_resources[] = {
  45. DEFINE_RES_IRQ_NAMED(CRYSTAL_COVE_IRQ_GPIO, "GPIO"),
  46. };
  47. static struct mfd_cell crystal_cove_byt_dev[] = {
  48. {
  49. .name = "crystal_cove_pwrsrc",
  50. .num_resources = ARRAY_SIZE(pwrsrc_resources),
  51. .resources = pwrsrc_resources,
  52. },
  53. {
  54. .name = "crystal_cove_thermal",
  55. .num_resources = ARRAY_SIZE(thermal_resources),
  56. .resources = thermal_resources,
  57. },
  58. {
  59. .name = "crystal_cove_bcu",
  60. .num_resources = ARRAY_SIZE(bcu_resources),
  61. .resources = bcu_resources,
  62. },
  63. {
  64. .name = "crystal_cove_adc",
  65. .num_resources = ARRAY_SIZE(adc_resources),
  66. .resources = adc_resources,
  67. },
  68. {
  69. .name = "crystal_cove_charger",
  70. .num_resources = ARRAY_SIZE(charger_resources),
  71. .resources = charger_resources,
  72. },
  73. {
  74. .name = "crystal_cove_gpio",
  75. .num_resources = ARRAY_SIZE(gpio_resources),
  76. .resources = gpio_resources,
  77. },
  78. {
  79. .name = "byt_crystal_cove_pmic",
  80. },
  81. {
  82. .name = "crystal_cove_pwm",
  83. },
  84. };
  85. static struct mfd_cell crystal_cove_cht_dev[] = {
  86. {
  87. .name = "crystal_cove_gpio",
  88. .num_resources = ARRAY_SIZE(gpio_resources),
  89. .resources = gpio_resources,
  90. },
  91. {
  92. .name = "cht_crystal_cove_pmic",
  93. },
  94. {
  95. .name = "crystal_cove_pwm",
  96. },
  97. };
  98. static const struct regmap_config crystal_cove_regmap_config = {
  99. .reg_bits = 8,
  100. .val_bits = 8,
  101. .max_register = CRYSTAL_COVE_MAX_REGISTER,
  102. .cache_type = REGCACHE_NONE,
  103. };
  104. static const struct regmap_irq crystal_cove_irqs[] = {
  105. REGMAP_IRQ_REG(CRYSTAL_COVE_IRQ_PWRSRC, 0, BIT(CRYSTAL_COVE_IRQ_PWRSRC)),
  106. REGMAP_IRQ_REG(CRYSTAL_COVE_IRQ_THRM, 0, BIT(CRYSTAL_COVE_IRQ_THRM)),
  107. REGMAP_IRQ_REG(CRYSTAL_COVE_IRQ_BCU, 0, BIT(CRYSTAL_COVE_IRQ_BCU)),
  108. REGMAP_IRQ_REG(CRYSTAL_COVE_IRQ_ADC, 0, BIT(CRYSTAL_COVE_IRQ_ADC)),
  109. REGMAP_IRQ_REG(CRYSTAL_COVE_IRQ_CHGR, 0, BIT(CRYSTAL_COVE_IRQ_CHGR)),
  110. REGMAP_IRQ_REG(CRYSTAL_COVE_IRQ_GPIO, 0, BIT(CRYSTAL_COVE_IRQ_GPIO)),
  111. REGMAP_IRQ_REG(CRYSTAL_COVE_IRQ_VHDMIOCP, 0, BIT(CRYSTAL_COVE_IRQ_VHDMIOCP)),
  112. };
  113. static const struct regmap_irq_chip crystal_cove_irq_chip = {
  114. .name = "Crystal Cove",
  115. .irqs = crystal_cove_irqs,
  116. .num_irqs = ARRAY_SIZE(crystal_cove_irqs),
  117. .num_regs = 1,
  118. .status_base = CRYSTAL_COVE_REG_IRQLVL1,
  119. .mask_base = CRYSTAL_COVE_REG_MIRQLVL1,
  120. };
  121. /* PWM consumed by the Intel GFX */
  122. static struct pwm_lookup crc_pwm_lookup[] = {
  123. PWM_LOOKUP("crystal_cove_pwm", 0, "0000:00:02.0", "pwm_pmic_backlight", 0, PWM_POLARITY_NORMAL),
  124. };
  125. struct crystal_cove_config {
  126. unsigned long irq_flags;
  127. struct mfd_cell *cell_dev;
  128. int n_cell_devs;
  129. const struct regmap_config *regmap_config;
  130. const struct regmap_irq_chip *irq_chip;
  131. };
  132. static const struct crystal_cove_config crystal_cove_config_byt_crc = {
  133. .irq_flags = IRQF_TRIGGER_RISING,
  134. .cell_dev = crystal_cove_byt_dev,
  135. .n_cell_devs = ARRAY_SIZE(crystal_cove_byt_dev),
  136. .regmap_config = &crystal_cove_regmap_config,
  137. .irq_chip = &crystal_cove_irq_chip,
  138. };
  139. static const struct crystal_cove_config crystal_cove_config_cht_crc = {
  140. .irq_flags = IRQF_TRIGGER_RISING,
  141. .cell_dev = crystal_cove_cht_dev,
  142. .n_cell_devs = ARRAY_SIZE(crystal_cove_cht_dev),
  143. .regmap_config = &crystal_cove_regmap_config,
  144. .irq_chip = &crystal_cove_irq_chip,
  145. };
  146. static int crystal_cove_i2c_probe(struct i2c_client *i2c)
  147. {
  148. const struct crystal_cove_config *config;
  149. struct device *dev = &i2c->dev;
  150. struct intel_soc_pmic *pmic;
  151. int ret;
  152. if (soc_intel_is_byt())
  153. config = &crystal_cove_config_byt_crc;
  154. else
  155. config = &crystal_cove_config_cht_crc;
  156. pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
  157. if (!pmic)
  158. return -ENOMEM;
  159. i2c_set_clientdata(i2c, pmic);
  160. pmic->regmap = devm_regmap_init_i2c(i2c, config->regmap_config);
  161. if (IS_ERR(pmic->regmap))
  162. return PTR_ERR(pmic->regmap);
  163. pmic->irq = i2c->irq;
  164. ret = devm_regmap_add_irq_chip(dev, pmic->regmap, pmic->irq,
  165. config->irq_flags | IRQF_ONESHOT,
  166. 0, config->irq_chip, &pmic->irq_chip_data);
  167. if (ret)
  168. return ret;
  169. ret = enable_irq_wake(pmic->irq);
  170. if (ret)
  171. dev_warn(dev, "Can't enable IRQ as wake source: %d\n", ret);
  172. /* Add lookup table for crc-pwm */
  173. pwm_add_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
  174. /* To distuingish this domain from the GPIO/charger's irqchip domains */
  175. irq_domain_update_bus_token(regmap_irq_get_domain(pmic->irq_chip_data),
  176. DOMAIN_BUS_NEXUS);
  177. ret = mfd_add_devices(dev, PLATFORM_DEVID_NONE, config->cell_dev,
  178. config->n_cell_devs, NULL, 0,
  179. regmap_irq_get_domain(pmic->irq_chip_data));
  180. if (ret)
  181. pwm_remove_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
  182. return ret;
  183. }
  184. static void crystal_cove_i2c_remove(struct i2c_client *i2c)
  185. {
  186. /* remove crc-pwm lookup table */
  187. pwm_remove_table(crc_pwm_lookup, ARRAY_SIZE(crc_pwm_lookup));
  188. mfd_remove_devices(&i2c->dev);
  189. }
  190. static void crystal_cove_shutdown(struct i2c_client *i2c)
  191. {
  192. struct intel_soc_pmic *pmic = i2c_get_clientdata(i2c);
  193. disable_irq(pmic->irq);
  194. return;
  195. }
  196. static int crystal_cove_suspend(struct device *dev)
  197. {
  198. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  199. disable_irq(pmic->irq);
  200. return 0;
  201. }
  202. static int crystal_cove_resume(struct device *dev)
  203. {
  204. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  205. enable_irq(pmic->irq);
  206. return 0;
  207. }
  208. static DEFINE_SIMPLE_DEV_PM_OPS(crystal_cove_pm_ops, crystal_cove_suspend, crystal_cove_resume);
  209. static const struct acpi_device_id crystal_cove_acpi_match[] = {
  210. { "INT33FD" },
  211. { },
  212. };
  213. MODULE_DEVICE_TABLE(acpi, crystal_cove_acpi_match);
  214. static struct i2c_driver crystal_cove_i2c_driver = {
  215. .driver = {
  216. .name = "crystal_cove_i2c",
  217. .pm = pm_sleep_ptr(&crystal_cove_pm_ops),
  218. .acpi_match_table = crystal_cove_acpi_match,
  219. },
  220. .probe_new = crystal_cove_i2c_probe,
  221. .remove = crystal_cove_i2c_remove,
  222. .shutdown = crystal_cove_shutdown,
  223. };
  224. module_i2c_driver(crystal_cove_i2c_driver);
  225. MODULE_DESCRIPTION("I2C driver for Intel SoC PMIC");
  226. MODULE_LICENSE("GPL v2");
  227. MODULE_AUTHOR("Yang, Bin <[email protected]>");
  228. MODULE_AUTHOR("Zhu, Lejun <[email protected]>");