rohm-bd718x7.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // Copyright (C) 2018 ROHM Semiconductors
  4. //
  5. // ROHM BD71837MWV PMIC driver
  6. //
  7. // Datasheet available from
  8. // https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e
  9. #include <linux/i2c.h>
  10. #include <linux/input.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mfd/rohm-bd718x7.h>
  13. #include <linux/mfd/core.h>
  14. #include <linux/module.h>
  15. #include <linux/regmap.h>
  16. /*
  17. * gpio_keys.h requires definiton of bool. It is brought in
  18. * by above includes. Keep this as last until gpio_keys.h gets fixed.
  19. */
  20. #include <linux/gpio_keys.h>
  21. static const u8 supported_revisions[] = { 0xA2 /* BD71837 */ };
  22. static struct gpio_keys_button button = {
  23. .code = KEY_POWER,
  24. .gpio = -1,
  25. .type = EV_KEY,
  26. };
  27. static struct gpio_keys_platform_data bd718xx_powerkey_data = {
  28. .buttons = &button,
  29. .nbuttons = 1,
  30. .name = "bd718xx-pwrkey",
  31. };
  32. static struct mfd_cell bd71837_mfd_cells[] = {
  33. {
  34. .name = "gpio-keys",
  35. .platform_data = &bd718xx_powerkey_data,
  36. .pdata_size = sizeof(bd718xx_powerkey_data),
  37. },
  38. { .name = "bd71837-clk", },
  39. { .name = "bd71837-pmic", },
  40. };
  41. static const struct regmap_irq bd71837_irqs[] = {
  42. REGMAP_IRQ_REG(BD71837_INT_SWRST, 0, BD71837_INT_SWRST_MASK),
  43. REGMAP_IRQ_REG(BD71837_INT_PWRBTN_S, 0, BD71837_INT_PWRBTN_S_MASK),
  44. REGMAP_IRQ_REG(BD71837_INT_PWRBTN_L, 0, BD71837_INT_PWRBTN_L_MASK),
  45. REGMAP_IRQ_REG(BD71837_INT_PWRBTN, 0, BD71837_INT_PWRBTN_MASK),
  46. REGMAP_IRQ_REG(BD71837_INT_WDOG, 0, BD71837_INT_WDOG_MASK),
  47. REGMAP_IRQ_REG(BD71837_INT_ON_REQ, 0, BD71837_INT_ON_REQ_MASK),
  48. REGMAP_IRQ_REG(BD71837_INT_STBY_REQ, 0, BD71837_INT_STBY_REQ_MASK),
  49. };
  50. static struct regmap_irq_chip bd71837_irq_chip = {
  51. .name = "bd71837-irq",
  52. .irqs = bd71837_irqs,
  53. .num_irqs = ARRAY_SIZE(bd71837_irqs),
  54. .num_regs = 1,
  55. .irq_reg_stride = 1,
  56. .status_base = BD71837_REG_IRQ,
  57. .mask_base = BD71837_REG_MIRQ,
  58. .ack_base = BD71837_REG_IRQ,
  59. .init_ack_masked = true,
  60. .mask_invert = false,
  61. };
  62. static const struct regmap_range pmic_status_range = {
  63. .range_min = BD71837_REG_IRQ,
  64. .range_max = BD71837_REG_POW_STATE,
  65. };
  66. static const struct regmap_access_table volatile_regs = {
  67. .yes_ranges = &pmic_status_range,
  68. .n_yes_ranges = 1,
  69. };
  70. static const struct regmap_config bd71837_regmap_config = {
  71. .reg_bits = 8,
  72. .val_bits = 8,
  73. .volatile_table = &volatile_regs,
  74. .max_register = BD71837_MAX_REGISTER - 1,
  75. .cache_type = REGCACHE_RBTREE,
  76. };
  77. static int bd71837_i2c_probe(struct i2c_client *i2c,
  78. const struct i2c_device_id *id)
  79. {
  80. struct bd71837 *bd71837;
  81. int ret, i;
  82. unsigned int val;
  83. bd71837 = devm_kzalloc(&i2c->dev, sizeof(struct bd71837), GFP_KERNEL);
  84. if (!bd71837)
  85. return -ENOMEM;
  86. bd71837->chip_irq = i2c->irq;
  87. if (!bd71837->chip_irq) {
  88. dev_err(&i2c->dev, "No IRQ configured\n");
  89. return -EINVAL;
  90. }
  91. bd71837->dev = &i2c->dev;
  92. dev_set_drvdata(&i2c->dev, bd71837);
  93. bd71837->regmap = devm_regmap_init_i2c(i2c, &bd71837_regmap_config);
  94. if (IS_ERR(bd71837->regmap)) {
  95. dev_err(&i2c->dev, "regmap initialization failed\n");
  96. return PTR_ERR(bd71837->regmap);
  97. }
  98. ret = regmap_read(bd71837->regmap, BD71837_REG_REV, &val);
  99. if (ret) {
  100. dev_err(&i2c->dev, "Read BD71837_REG_DEVICE failed\n");
  101. return ret;
  102. }
  103. for (i = 0; i < ARRAY_SIZE(supported_revisions); i++)
  104. if (supported_revisions[i] == val)
  105. break;
  106. if (i == ARRAY_SIZE(supported_revisions)) {
  107. dev_err(&i2c->dev, "Unsupported chip revision\n");
  108. return -ENODEV;
  109. }
  110. ret = devm_regmap_add_irq_chip(&i2c->dev, bd71837->regmap,
  111. bd71837->chip_irq, IRQF_ONESHOT, 0,
  112. &bd71837_irq_chip, &bd71837->irq_data);
  113. if (ret) {
  114. dev_err(&i2c->dev, "Failed to add irq_chip\n");
  115. return ret;
  116. }
  117. /* Configure short press to 10 milliseconds */
  118. ret = regmap_update_bits(bd71837->regmap,
  119. BD71837_REG_PWRONCONFIG0,
  120. BD718XX_PWRBTN_PRESS_DURATION_MASK,
  121. BD718XX_PWRBTN_SHORT_PRESS_10MS);
  122. if (ret) {
  123. dev_err(&i2c->dev,
  124. "Failed to configure button short press timeout\n");
  125. return ret;
  126. }
  127. /* Configure long press to 10 seconds */
  128. ret = regmap_update_bits(bd71837->regmap,
  129. BD71837_REG_PWRONCONFIG1,
  130. BD718XX_PWRBTN_PRESS_DURATION_MASK,
  131. BD718XX_PWRBTN_LONG_PRESS_10S);
  132. if (ret) {
  133. dev_err(&i2c->dev,
  134. "Failed to configure button long press timeout\n");
  135. return ret;
  136. }
  137. ret = regmap_irq_get_virq(bd71837->irq_data, BD71837_INT_PWRBTN_S);
  138. if (ret < 0) {
  139. dev_err(&i2c->dev, "Failed to get the IRQ\n");
  140. return ret;
  141. }
  142. button.irq = ret;
  143. ret = devm_mfd_add_devices(bd71837->dev, PLATFORM_DEVID_AUTO,
  144. bd71837_mfd_cells,
  145. ARRAY_SIZE(bd71837_mfd_cells), NULL, 0,
  146. regmap_irq_get_domain(bd71837->irq_data));
  147. if (ret)
  148. dev_err(&i2c->dev, "Failed to create subdevices\n");
  149. return ret;
  150. }
  151. static const struct of_device_id bd71837_of_match[] = {
  152. { .compatible = "rohm,bd71837", },
  153. { }
  154. };
  155. MODULE_DEVICE_TABLE(of, bd71837_of_match);
  156. static struct i2c_driver bd71837_i2c_driver = {
  157. .driver = {
  158. .name = "rohm-bd718x7",
  159. .of_match_table = bd71837_of_match,
  160. },
  161. .probe = bd71837_i2c_probe,
  162. };
  163. static int __init bd71837_i2c_init(void)
  164. {
  165. return i2c_add_driver(&bd71837_i2c_driver);
  166. }
  167. /* Initialise early so consumer devices can complete system boot */
  168. subsys_initcall(bd71837_i2c_init);
  169. static void __exit bd71837_i2c_exit(void)
  170. {
  171. i2c_del_driver(&bd71837_i2c_driver);
  172. }
  173. module_exit(bd71837_i2c_exit);
  174. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  175. MODULE_DESCRIPTION("ROHM BD71837 Power Management IC driver");
  176. MODULE_LICENSE("GPL");