axp20x.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * axp20x.c - MFD core driver for the X-Powers AXP202 and AXP209
  3. *
  4. * AXP20x comprises an adaptive USB-Compatible PWM charger, 2 BUCK DC-DC
  5. * converters, 5 LDOs, multiple 12-bit ADCs of voltage, current and temperature
  6. * as well as 4 configurable GPIOs.
  7. *
  8. * Author: Carlo Caione <carlo@caione.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/mfd/axp20x.h>
  24. #include <linux/mfd/core.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_irq.h>
  27. #define AXP20X_OFF 0x80
  28. static const struct regmap_range axp20x_writeable_ranges[] = {
  29. regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
  30. regmap_reg_range(AXP20X_DCDC_MODE, AXP20X_FG_RES),
  31. };
  32. static const struct regmap_range axp20x_volatile_ranges[] = {
  33. regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE),
  34. };
  35. static const struct regmap_access_table axp20x_writeable_table = {
  36. .yes_ranges = axp20x_writeable_ranges,
  37. .n_yes_ranges = ARRAY_SIZE(axp20x_writeable_ranges),
  38. };
  39. static const struct regmap_access_table axp20x_volatile_table = {
  40. .yes_ranges = axp20x_volatile_ranges,
  41. .n_yes_ranges = ARRAY_SIZE(axp20x_volatile_ranges),
  42. };
  43. static struct resource axp20x_pek_resources[] = {
  44. {
  45. .name = "PEK_DBR",
  46. .start = AXP20X_IRQ_PEK_RIS_EDGE,
  47. .end = AXP20X_IRQ_PEK_RIS_EDGE,
  48. .flags = IORESOURCE_IRQ,
  49. }, {
  50. .name = "PEK_DBF",
  51. .start = AXP20X_IRQ_PEK_FAL_EDGE,
  52. .end = AXP20X_IRQ_PEK_FAL_EDGE,
  53. .flags = IORESOURCE_IRQ,
  54. },
  55. };
  56. static const struct regmap_config axp20x_regmap_config = {
  57. .reg_bits = 8,
  58. .val_bits = 8,
  59. .wr_table = &axp20x_writeable_table,
  60. .volatile_table = &axp20x_volatile_table,
  61. .max_register = AXP20X_FG_RES,
  62. .cache_type = REGCACHE_RBTREE,
  63. };
  64. #define AXP20X_IRQ(_irq, _off, _mask) \
  65. [AXP20X_IRQ_##_irq] = { .reg_offset = (_off), .mask = BIT(_mask) }
  66. static const struct regmap_irq axp20x_regmap_irqs[] = {
  67. AXP20X_IRQ(ACIN_OVER_V, 0, 7),
  68. AXP20X_IRQ(ACIN_PLUGIN, 0, 6),
  69. AXP20X_IRQ(ACIN_REMOVAL, 0, 5),
  70. AXP20X_IRQ(VBUS_OVER_V, 0, 4),
  71. AXP20X_IRQ(VBUS_PLUGIN, 0, 3),
  72. AXP20X_IRQ(VBUS_REMOVAL, 0, 2),
  73. AXP20X_IRQ(VBUS_V_LOW, 0, 1),
  74. AXP20X_IRQ(BATT_PLUGIN, 1, 7),
  75. AXP20X_IRQ(BATT_REMOVAL, 1, 6),
  76. AXP20X_IRQ(BATT_ENT_ACT_MODE, 1, 5),
  77. AXP20X_IRQ(BATT_EXIT_ACT_MODE, 1, 4),
  78. AXP20X_IRQ(CHARG, 1, 3),
  79. AXP20X_IRQ(CHARG_DONE, 1, 2),
  80. AXP20X_IRQ(BATT_TEMP_HIGH, 1, 1),
  81. AXP20X_IRQ(BATT_TEMP_LOW, 1, 0),
  82. AXP20X_IRQ(DIE_TEMP_HIGH, 2, 7),
  83. AXP20X_IRQ(CHARG_I_LOW, 2, 6),
  84. AXP20X_IRQ(DCDC1_V_LONG, 2, 5),
  85. AXP20X_IRQ(DCDC2_V_LONG, 2, 4),
  86. AXP20X_IRQ(DCDC3_V_LONG, 2, 3),
  87. AXP20X_IRQ(PEK_SHORT, 2, 1),
  88. AXP20X_IRQ(PEK_LONG, 2, 0),
  89. AXP20X_IRQ(N_OE_PWR_ON, 3, 7),
  90. AXP20X_IRQ(N_OE_PWR_OFF, 3, 6),
  91. AXP20X_IRQ(VBUS_VALID, 3, 5),
  92. AXP20X_IRQ(VBUS_NOT_VALID, 3, 4),
  93. AXP20X_IRQ(VBUS_SESS_VALID, 3, 3),
  94. AXP20X_IRQ(VBUS_SESS_END, 3, 2),
  95. AXP20X_IRQ(LOW_PWR_LVL1, 3, 1),
  96. AXP20X_IRQ(LOW_PWR_LVL2, 3, 0),
  97. AXP20X_IRQ(TIMER, 4, 7),
  98. AXP20X_IRQ(PEK_RIS_EDGE, 4, 6),
  99. AXP20X_IRQ(PEK_FAL_EDGE, 4, 5),
  100. AXP20X_IRQ(GPIO3_INPUT, 4, 3),
  101. AXP20X_IRQ(GPIO2_INPUT, 4, 2),
  102. AXP20X_IRQ(GPIO1_INPUT, 4, 1),
  103. AXP20X_IRQ(GPIO0_INPUT, 4, 0),
  104. };
  105. static const struct of_device_id axp20x_of_match[] = {
  106. { .compatible = "x-powers,axp202", .data = (void *) AXP202_ID },
  107. { .compatible = "x-powers,axp209", .data = (void *) AXP209_ID },
  108. { },
  109. };
  110. MODULE_DEVICE_TABLE(of, axp20x_of_match);
  111. /*
  112. * This is useless for OF-enabled devices, but it is needed by I2C subsystem
  113. */
  114. static const struct i2c_device_id axp20x_i2c_id[] = {
  115. { },
  116. };
  117. MODULE_DEVICE_TABLE(i2c, axp20x_i2c_id);
  118. static const struct regmap_irq_chip axp20x_regmap_irq_chip = {
  119. .name = "axp20x_irq_chip",
  120. .status_base = AXP20X_IRQ1_STATE,
  121. .ack_base = AXP20X_IRQ1_STATE,
  122. .mask_base = AXP20X_IRQ1_EN,
  123. .num_regs = 5,
  124. .irqs = axp20x_regmap_irqs,
  125. .num_irqs = ARRAY_SIZE(axp20x_regmap_irqs),
  126. .mask_invert = true,
  127. .init_ack_masked = true,
  128. };
  129. static const char * const axp20x_supplies[] = {
  130. "acin",
  131. "vin2",
  132. "vin3",
  133. "ldo24in",
  134. "ldo3in",
  135. "ldo5in",
  136. };
  137. static struct mfd_cell axp20x_cells[] = {
  138. {
  139. .name = "axp20x-pek",
  140. .num_resources = ARRAY_SIZE(axp20x_pek_resources),
  141. .resources = axp20x_pek_resources,
  142. }, {
  143. .name = "axp20x-regulator",
  144. .parent_supplies = axp20x_supplies,
  145. .num_parent_supplies = ARRAY_SIZE(axp20x_supplies),
  146. },
  147. };
  148. static struct axp20x_dev *axp20x_pm_power_off;
  149. static void axp20x_power_off(void)
  150. {
  151. regmap_write(axp20x_pm_power_off->regmap, AXP20X_OFF_CTRL,
  152. AXP20X_OFF);
  153. }
  154. static int axp20x_i2c_probe(struct i2c_client *i2c,
  155. const struct i2c_device_id *id)
  156. {
  157. struct axp20x_dev *axp20x;
  158. const struct of_device_id *of_id;
  159. int ret;
  160. axp20x = devm_kzalloc(&i2c->dev, sizeof(*axp20x), GFP_KERNEL);
  161. if (!axp20x)
  162. return -ENOMEM;
  163. of_id = of_match_device(axp20x_of_match, &i2c->dev);
  164. if (!of_id) {
  165. dev_err(&i2c->dev, "Unable to setup AXP20X data\n");
  166. return -ENODEV;
  167. }
  168. axp20x->variant = (long) of_id->data;
  169. axp20x->i2c_client = i2c;
  170. axp20x->dev = &i2c->dev;
  171. dev_set_drvdata(axp20x->dev, axp20x);
  172. axp20x->regmap = devm_regmap_init_i2c(i2c, &axp20x_regmap_config);
  173. if (IS_ERR(axp20x->regmap)) {
  174. ret = PTR_ERR(axp20x->regmap);
  175. dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
  176. return ret;
  177. }
  178. ret = regmap_add_irq_chip(axp20x->regmap, i2c->irq,
  179. IRQF_ONESHOT | IRQF_SHARED, -1,
  180. &axp20x_regmap_irq_chip,
  181. &axp20x->regmap_irqc);
  182. if (ret) {
  183. dev_err(&i2c->dev, "failed to add irq chip: %d\n", ret);
  184. return ret;
  185. }
  186. ret = mfd_add_devices(axp20x->dev, -1, axp20x_cells,
  187. ARRAY_SIZE(axp20x_cells), NULL, 0, NULL);
  188. if (ret) {
  189. dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
  190. regmap_del_irq_chip(i2c->irq, axp20x->regmap_irqc);
  191. return ret;
  192. }
  193. if (!pm_power_off) {
  194. axp20x_pm_power_off = axp20x;
  195. pm_power_off = axp20x_power_off;
  196. }
  197. dev_info(&i2c->dev, "AXP20X driver loaded\n");
  198. return 0;
  199. }
  200. static int axp20x_i2c_remove(struct i2c_client *i2c)
  201. {
  202. struct axp20x_dev *axp20x = i2c_get_clientdata(i2c);
  203. if (axp20x == axp20x_pm_power_off) {
  204. axp20x_pm_power_off = NULL;
  205. pm_power_off = NULL;
  206. }
  207. mfd_remove_devices(axp20x->dev);
  208. regmap_del_irq_chip(axp20x->i2c_client->irq, axp20x->regmap_irqc);
  209. return 0;
  210. }
  211. static struct i2c_driver axp20x_i2c_driver = {
  212. .driver = {
  213. .name = "axp20x",
  214. .owner = THIS_MODULE,
  215. .of_match_table = of_match_ptr(axp20x_of_match),
  216. },
  217. .probe = axp20x_i2c_probe,
  218. .remove = axp20x_i2c_remove,
  219. .id_table = axp20x_i2c_id,
  220. };
  221. module_i2c_driver(axp20x_i2c_driver);
  222. MODULE_DESCRIPTION("PMIC MFD core driver for AXP20X");
  223. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  224. MODULE_LICENSE("GPL");