max77714.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Maxim MAX77714 Core Driver
  4. *
  5. * Copyright (C) 2022 Luca Ceresoli
  6. * Author: Luca Ceresoli <[email protected]>
  7. */
  8. #include <linux/i2c.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mfd/core.h>
  11. #include <linux/mfd/max77714.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/regmap.h>
  15. static const struct mfd_cell max77714_cells[] = {
  16. { .name = "max77714-watchdog" },
  17. { .name = "max77714-rtc" },
  18. };
  19. static const struct regmap_range max77714_readable_ranges[] = {
  20. regmap_reg_range(MAX77714_INT_TOP, MAX77714_INT_TOP),
  21. regmap_reg_range(MAX77714_INT_TOPM, MAX77714_INT_TOPM),
  22. regmap_reg_range(MAX77714_32K_STATUS, MAX77714_32K_CONFIG),
  23. regmap_reg_range(MAX77714_CNFG_GLBL2, MAX77714_CNFG2_ONOFF),
  24. };
  25. static const struct regmap_range max77714_writable_ranges[] = {
  26. regmap_reg_range(MAX77714_INT_TOPM, MAX77714_INT_TOPM),
  27. regmap_reg_range(MAX77714_32K_CONFIG, MAX77714_32K_CONFIG),
  28. regmap_reg_range(MAX77714_CNFG_GLBL2, MAX77714_CNFG2_ONOFF),
  29. };
  30. static const struct regmap_access_table max77714_readable_table = {
  31. .yes_ranges = max77714_readable_ranges,
  32. .n_yes_ranges = ARRAY_SIZE(max77714_readable_ranges),
  33. };
  34. static const struct regmap_access_table max77714_writable_table = {
  35. .yes_ranges = max77714_writable_ranges,
  36. .n_yes_ranges = ARRAY_SIZE(max77714_writable_ranges),
  37. };
  38. static const struct regmap_config max77714_regmap_config = {
  39. .reg_bits = 8,
  40. .val_bits = 8,
  41. .max_register = MAX77714_CNFG2_ONOFF,
  42. .rd_table = &max77714_readable_table,
  43. .wr_table = &max77714_writable_table,
  44. };
  45. static const struct regmap_irq max77714_top_irqs[] = {
  46. REGMAP_IRQ_REG(MAX77714_IRQ_TOP_ONOFF, 0, MAX77714_INT_TOP_ONOFF),
  47. REGMAP_IRQ_REG(MAX77714_IRQ_TOP_RTC, 0, MAX77714_INT_TOP_RTC),
  48. REGMAP_IRQ_REG(MAX77714_IRQ_TOP_GPIO, 0, MAX77714_INT_TOP_GPIO),
  49. REGMAP_IRQ_REG(MAX77714_IRQ_TOP_LDO, 0, MAX77714_INT_TOP_LDO),
  50. REGMAP_IRQ_REG(MAX77714_IRQ_TOP_SD, 0, MAX77714_INT_TOP_SD),
  51. REGMAP_IRQ_REG(MAX77714_IRQ_TOP_GLBL, 0, MAX77714_INT_TOP_GLBL),
  52. };
  53. static const struct regmap_irq_chip max77714_irq_chip = {
  54. .name = "max77714-pmic",
  55. .status_base = MAX77714_INT_TOP,
  56. .mask_base = MAX77714_INT_TOPM,
  57. .num_regs = 1,
  58. .irqs = max77714_top_irqs,
  59. .num_irqs = ARRAY_SIZE(max77714_top_irqs),
  60. };
  61. /*
  62. * MAX77714 initially uses the internal, low precision oscillator. Enable
  63. * the external oscillator by setting the XOSC_RETRY bit. If the external
  64. * oscillator is not OK (probably not installed) this has no effect.
  65. */
  66. static int max77714_setup_xosc(struct device *dev, struct regmap *regmap)
  67. {
  68. /* Internal Crystal Load Capacitance, indexed by value of 32KLOAD bits */
  69. static const unsigned int load_cap[4] = {0, 10, 12, 22}; /* pF */
  70. unsigned int load_cap_idx;
  71. unsigned int status;
  72. int err;
  73. err = regmap_update_bits(regmap, MAX77714_32K_CONFIG,
  74. MAX77714_32K_CONFIG_XOSC_RETRY,
  75. MAX77714_32K_CONFIG_XOSC_RETRY);
  76. if (err)
  77. return dev_err_probe(dev, err, "Failed to configure the external oscillator\n");
  78. err = regmap_read(regmap, MAX77714_32K_STATUS, &status);
  79. if (err)
  80. return dev_err_probe(dev, err, "Failed to read external oscillator status\n");
  81. load_cap_idx = (status >> MAX77714_32K_STATUS_32KLOAD_SHF)
  82. & MAX77714_32K_STATUS_32KLOAD_MSK;
  83. dev_info(dev, "Using %s oscillator, %d pF load cap\n",
  84. status & MAX77714_32K_STATUS_32KSOURCE ? "internal" : "external",
  85. load_cap[load_cap_idx]);
  86. return 0;
  87. }
  88. static int max77714_probe(struct i2c_client *client)
  89. {
  90. struct device *dev = &client->dev;
  91. struct regmap *regmap;
  92. struct regmap_irq_chip_data *irq_data;
  93. int err;
  94. regmap = devm_regmap_init_i2c(client, &max77714_regmap_config);
  95. if (IS_ERR(regmap))
  96. return dev_err_probe(dev, PTR_ERR(regmap),
  97. "Failed to initialise regmap\n");
  98. err = max77714_setup_xosc(dev, regmap);
  99. if (err)
  100. return err;
  101. err = devm_regmap_add_irq_chip(dev, regmap, client->irq,
  102. IRQF_ONESHOT | IRQF_SHARED, 0,
  103. &max77714_irq_chip, &irq_data);
  104. if (err)
  105. return dev_err_probe(dev, err, "Failed to add PMIC IRQ chip\n");
  106. err = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
  107. max77714_cells, ARRAY_SIZE(max77714_cells),
  108. NULL, 0, NULL);
  109. if (err)
  110. return dev_err_probe(dev, err, "Failed to register child devices\n");
  111. return 0;
  112. }
  113. static const struct of_device_id max77714_dt_match[] = {
  114. { .compatible = "maxim,max77714" },
  115. {},
  116. };
  117. MODULE_DEVICE_TABLE(of, max77714_dt_match);
  118. static struct i2c_driver max77714_driver = {
  119. .driver = {
  120. .name = "max77714",
  121. .of_match_table = max77714_dt_match,
  122. },
  123. .probe_new = max77714_probe,
  124. };
  125. module_i2c_driver(max77714_driver);
  126. MODULE_DESCRIPTION("Maxim MAX77714 MFD core driver");
  127. MODULE_AUTHOR("Luca Ceresoli <[email protected]>");
  128. MODULE_LICENSE("GPL");