88pm80x.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * I2C driver for Marvell 88PM80x
  4. *
  5. * Copyright (C) 2012 Marvell International Ltd.
  6. * Haojian Zhuang <haojian.zhuang@marvell.com>
  7. * Joseph(Yossi) Hanin <yhanin@marvell.com>
  8. * Qiao Zhou <zhouqiao@marvell.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mfd/88pm80x.h>
  14. #include <linux/slab.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/err.h>
  17. /* 88pm80x chips have same definition for chip id register. */
  18. #define PM80X_CHIP_ID (0x00)
  19. #define PM80X_CHIP_ID_NUM(x) (((x) >> 5) & 0x7)
  20. #define PM80X_CHIP_ID_REVISION(x) ((x) & 0x1F)
  21. struct pm80x_chip_mapping {
  22. unsigned int id;
  23. int type;
  24. };
  25. static struct pm80x_chip_mapping chip_mapping[] = {
  26. /* 88PM800 chip id number */
  27. {0x3, CHIP_PM800},
  28. /* 88PM805 chip id number */
  29. {0x0, CHIP_PM805},
  30. /* 88PM860 chip id number */
  31. {0x4, CHIP_PM860},
  32. };
  33. /*
  34. * workaround: some registers needed by pm805 are defined in pm800, so
  35. * need to use this global variable to maintain the relation between
  36. * pm800 and pm805. would remove it after HW chip fixes the issue.
  37. */
  38. static struct pm80x_chip *g_pm80x_chip;
  39. const struct regmap_config pm80x_regmap_config = {
  40. .reg_bits = 8,
  41. .val_bits = 8,
  42. };
  43. EXPORT_SYMBOL_GPL(pm80x_regmap_config);
  44. int pm80x_init(struct i2c_client *client)
  45. {
  46. struct pm80x_chip *chip;
  47. struct regmap *map;
  48. unsigned int val;
  49. int i, ret = 0;
  50. chip =
  51. devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL);
  52. if (!chip)
  53. return -ENOMEM;
  54. map = devm_regmap_init_i2c(client, &pm80x_regmap_config);
  55. if (IS_ERR(map)) {
  56. ret = PTR_ERR(map);
  57. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  58. ret);
  59. return ret;
  60. }
  61. chip->client = client;
  62. chip->regmap = map;
  63. chip->irq = client->irq;
  64. chip->dev = &client->dev;
  65. dev_set_drvdata(chip->dev, chip);
  66. i2c_set_clientdata(chip->client, chip);
  67. ret = regmap_read(chip->regmap, PM80X_CHIP_ID, &val);
  68. if (ret < 0) {
  69. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  70. return ret;
  71. }
  72. for (i = 0; i < ARRAY_SIZE(chip_mapping); i++) {
  73. if (chip_mapping[i].id == PM80X_CHIP_ID_NUM(val)) {
  74. chip->type = chip_mapping[i].type;
  75. break;
  76. }
  77. }
  78. if (i == ARRAY_SIZE(chip_mapping)) {
  79. dev_err(chip->dev,
  80. "Failed to detect Marvell 88PM800:ChipID[0x%x]\n", val);
  81. return -EINVAL;
  82. }
  83. device_init_wakeup(&client->dev, 1);
  84. /*
  85. * workaround: set g_pm80x_chip to the first probed chip. if the
  86. * second chip is probed, just point to the companion to each
  87. * other so that pm805 can access those specific register. would
  88. * remove it after HW chip fixes the issue.
  89. */
  90. if (!g_pm80x_chip)
  91. g_pm80x_chip = chip;
  92. else {
  93. chip->companion = g_pm80x_chip->client;
  94. g_pm80x_chip->companion = chip->client;
  95. }
  96. return 0;
  97. }
  98. EXPORT_SYMBOL_GPL(pm80x_init);
  99. int pm80x_deinit(void)
  100. {
  101. /*
  102. * workaround: clear the dependency between pm800 and pm805.
  103. * would remove it after HW chip fixes the issue.
  104. */
  105. if (g_pm80x_chip->companion)
  106. g_pm80x_chip->companion = NULL;
  107. else
  108. g_pm80x_chip = NULL;
  109. return 0;
  110. }
  111. EXPORT_SYMBOL_GPL(pm80x_deinit);
  112. #ifdef CONFIG_PM_SLEEP
  113. static int pm80x_suspend(struct device *dev)
  114. {
  115. struct i2c_client *client = to_i2c_client(dev);
  116. struct pm80x_chip *chip = i2c_get_clientdata(client);
  117. if (chip && chip->wu_flag)
  118. if (device_may_wakeup(chip->dev))
  119. enable_irq_wake(chip->irq);
  120. return 0;
  121. }
  122. static int pm80x_resume(struct device *dev)
  123. {
  124. struct i2c_client *client = to_i2c_client(dev);
  125. struct pm80x_chip *chip = i2c_get_clientdata(client);
  126. if (chip && chip->wu_flag)
  127. if (device_may_wakeup(chip->dev))
  128. disable_irq_wake(chip->irq);
  129. return 0;
  130. }
  131. #endif
  132. SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
  133. EXPORT_SYMBOL_GPL(pm80x_pm_ops);
  134. MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
  135. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  136. MODULE_LICENSE("GPL");