88pm80x.c 4.0 KB

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