rohm-bd718x7.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // Copyright (C) 2018 ROHM Semiconductors
  4. //
  5. // ROHM BD71837MWV and BD71847MWV PMIC driver
  6. //
  7. // Datasheet for BD71837MWV available from
  8. // https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e
  9. #include <linux/gpio_keys.h>
  10. #include <linux/i2c.h>
  11. #include <linux/input.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mfd/rohm-bd718x7.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/types.h>
  19. static struct gpio_keys_button button = {
  20. .code = KEY_POWER,
  21. .gpio = -1,
  22. .type = EV_KEY,
  23. };
  24. static struct gpio_keys_platform_data bd718xx_powerkey_data = {
  25. .buttons = &button,
  26. .nbuttons = 1,
  27. .name = "bd718xx-pwrkey",
  28. };
  29. static struct mfd_cell bd71837_mfd_cells[] = {
  30. {
  31. .name = "gpio-keys",
  32. .platform_data = &bd718xx_powerkey_data,
  33. .pdata_size = sizeof(bd718xx_powerkey_data),
  34. },
  35. { .name = "bd71837-clk", },
  36. { .name = "bd71837-pmic", },
  37. };
  38. static struct mfd_cell bd71847_mfd_cells[] = {
  39. {
  40. .name = "gpio-keys",
  41. .platform_data = &bd718xx_powerkey_data,
  42. .pdata_size = sizeof(bd718xx_powerkey_data),
  43. },
  44. { .name = "bd71847-clk", },
  45. { .name = "bd71847-pmic", },
  46. };
  47. static const struct regmap_irq bd718xx_irqs[] = {
  48. REGMAP_IRQ_REG(BD718XX_INT_SWRST, 0, BD718XX_INT_SWRST_MASK),
  49. REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_S, 0, BD718XX_INT_PWRBTN_S_MASK),
  50. REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_L, 0, BD718XX_INT_PWRBTN_L_MASK),
  51. REGMAP_IRQ_REG(BD718XX_INT_PWRBTN, 0, BD718XX_INT_PWRBTN_MASK),
  52. REGMAP_IRQ_REG(BD718XX_INT_WDOG, 0, BD718XX_INT_WDOG_MASK),
  53. REGMAP_IRQ_REG(BD718XX_INT_ON_REQ, 0, BD718XX_INT_ON_REQ_MASK),
  54. REGMAP_IRQ_REG(BD718XX_INT_STBY_REQ, 0, BD718XX_INT_STBY_REQ_MASK),
  55. };
  56. static struct regmap_irq_chip bd718xx_irq_chip = {
  57. .name = "bd718xx-irq",
  58. .irqs = bd718xx_irqs,
  59. .num_irqs = ARRAY_SIZE(bd718xx_irqs),
  60. .num_regs = 1,
  61. .irq_reg_stride = 1,
  62. .status_base = BD718XX_REG_IRQ,
  63. .mask_base = BD718XX_REG_MIRQ,
  64. .ack_base = BD718XX_REG_IRQ,
  65. .init_ack_masked = true,
  66. .mask_invert = false,
  67. };
  68. static const struct regmap_range pmic_status_range = {
  69. .range_min = BD718XX_REG_IRQ,
  70. .range_max = BD718XX_REG_POW_STATE,
  71. };
  72. static const struct regmap_access_table volatile_regs = {
  73. .yes_ranges = &pmic_status_range,
  74. .n_yes_ranges = 1,
  75. };
  76. static const struct regmap_config bd718xx_regmap_config = {
  77. .reg_bits = 8,
  78. .val_bits = 8,
  79. .volatile_table = &volatile_regs,
  80. .max_register = BD718XX_MAX_REGISTER - 1,
  81. .cache_type = REGCACHE_RBTREE,
  82. };
  83. static int bd718xx_init_press_duration(struct bd718xx *bd718xx)
  84. {
  85. struct device* dev = bd718xx->chip.dev;
  86. u32 short_press_ms, long_press_ms;
  87. u32 short_press_value, long_press_value;
  88. int ret;
  89. ret = of_property_read_u32(dev->of_node, "rohm,short-press-ms",
  90. &short_press_ms);
  91. if (!ret) {
  92. short_press_value = min(15u, (short_press_ms + 250) / 500);
  93. ret = regmap_update_bits(bd718xx->chip.regmap,
  94. BD718XX_REG_PWRONCONFIG0,
  95. BD718XX_PWRBTN_PRESS_DURATION_MASK,
  96. short_press_value);
  97. if (ret) {
  98. dev_err(dev, "Failed to init pwron short press\n");
  99. return ret;
  100. }
  101. }
  102. ret = of_property_read_u32(dev->of_node, "rohm,long-press-ms",
  103. &long_press_ms);
  104. if (!ret) {
  105. long_press_value = min(15u, (long_press_ms + 500) / 1000);
  106. ret = regmap_update_bits(bd718xx->chip.regmap,
  107. BD718XX_REG_PWRONCONFIG1,
  108. BD718XX_PWRBTN_PRESS_DURATION_MASK,
  109. long_press_value);
  110. if (ret) {
  111. dev_err(dev, "Failed to init pwron long press\n");
  112. return ret;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int bd718xx_i2c_probe(struct i2c_client *i2c,
  118. const struct i2c_device_id *id)
  119. {
  120. struct bd718xx *bd718xx;
  121. int ret;
  122. unsigned int chip_type;
  123. struct mfd_cell *mfd;
  124. int cells;
  125. if (!i2c->irq) {
  126. dev_err(&i2c->dev, "No IRQ configured\n");
  127. return -EINVAL;
  128. }
  129. bd718xx = devm_kzalloc(&i2c->dev, sizeof(struct bd718xx), GFP_KERNEL);
  130. if (!bd718xx)
  131. return -ENOMEM;
  132. bd718xx->chip_irq = i2c->irq;
  133. chip_type = (unsigned int)(uintptr_t)
  134. of_device_get_match_data(&i2c->dev);
  135. switch (chip_type) {
  136. case ROHM_CHIP_TYPE_BD71837:
  137. mfd = bd71837_mfd_cells;
  138. cells = ARRAY_SIZE(bd71837_mfd_cells);
  139. break;
  140. case ROHM_CHIP_TYPE_BD71847:
  141. mfd = bd71847_mfd_cells;
  142. cells = ARRAY_SIZE(bd71847_mfd_cells);
  143. break;
  144. default:
  145. dev_err(&i2c->dev, "Unknown device type");
  146. return -EINVAL;
  147. }
  148. bd718xx->chip.dev = &i2c->dev;
  149. dev_set_drvdata(&i2c->dev, bd718xx);
  150. bd718xx->chip.regmap = devm_regmap_init_i2c(i2c,
  151. &bd718xx_regmap_config);
  152. if (IS_ERR(bd718xx->chip.regmap)) {
  153. dev_err(&i2c->dev, "regmap initialization failed\n");
  154. return PTR_ERR(bd718xx->chip.regmap);
  155. }
  156. ret = devm_regmap_add_irq_chip(&i2c->dev, bd718xx->chip.regmap,
  157. bd718xx->chip_irq, IRQF_ONESHOT, 0,
  158. &bd718xx_irq_chip, &bd718xx->irq_data);
  159. if (ret) {
  160. dev_err(&i2c->dev, "Failed to add irq_chip\n");
  161. return ret;
  162. }
  163. ret = bd718xx_init_press_duration(bd718xx);
  164. if (ret)
  165. return ret;
  166. ret = regmap_irq_get_virq(bd718xx->irq_data, BD718XX_INT_PWRBTN_S);
  167. if (ret < 0) {
  168. dev_err(&i2c->dev, "Failed to get the IRQ\n");
  169. return ret;
  170. }
  171. button.irq = ret;
  172. ret = devm_mfd_add_devices(bd718xx->chip.dev, PLATFORM_DEVID_AUTO,
  173. mfd, cells, NULL, 0,
  174. regmap_irq_get_domain(bd718xx->irq_data));
  175. if (ret)
  176. dev_err(&i2c->dev, "Failed to create subdevices\n");
  177. return ret;
  178. }
  179. static const struct of_device_id bd718xx_of_match[] = {
  180. {
  181. .compatible = "rohm,bd71837",
  182. .data = (void *)ROHM_CHIP_TYPE_BD71837,
  183. },
  184. {
  185. .compatible = "rohm,bd71847",
  186. .data = (void *)ROHM_CHIP_TYPE_BD71847,
  187. },
  188. {
  189. .compatible = "rohm,bd71850",
  190. .data = (void *)ROHM_CHIP_TYPE_BD71847,
  191. },
  192. { }
  193. };
  194. MODULE_DEVICE_TABLE(of, bd718xx_of_match);
  195. static struct i2c_driver bd718xx_i2c_driver = {
  196. .driver = {
  197. .name = "rohm-bd718x7",
  198. .of_match_table = bd718xx_of_match,
  199. },
  200. .probe = bd718xx_i2c_probe,
  201. };
  202. static int __init bd718xx_i2c_init(void)
  203. {
  204. return i2c_add_driver(&bd718xx_i2c_driver);
  205. }
  206. /* Initialise early so consumer devices can complete system boot */
  207. subsys_initcall(bd718xx_i2c_init);
  208. static void __exit bd718xx_i2c_exit(void)
  209. {
  210. i2c_del_driver(&bd718xx_i2c_driver);
  211. }
  212. module_exit(bd718xx_i2c_exit);
  213. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  214. MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
  215. MODULE_LICENSE("GPL");