88pm80x.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. };
  34. /*
  35. * workaround: some registers needed by pm805 are defined in pm800, so
  36. * need to use this global variable to maintain the relation between
  37. * pm800 and pm805. would remove it after HW chip fixes the issue.
  38. */
  39. static struct pm80x_chip *g_pm80x_chip;
  40. const struct regmap_config pm80x_regmap_config = {
  41. .reg_bits = 8,
  42. .val_bits = 8,
  43. };
  44. EXPORT_SYMBOL_GPL(pm80x_regmap_config);
  45. int pm80x_init(struct i2c_client *client)
  46. {
  47. struct pm80x_chip *chip;
  48. struct regmap *map;
  49. unsigned int val;
  50. int i, ret = 0;
  51. chip =
  52. devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL);
  53. if (!chip)
  54. return -ENOMEM;
  55. map = devm_regmap_init_i2c(client, &pm80x_regmap_config);
  56. if (IS_ERR(map)) {
  57. ret = PTR_ERR(map);
  58. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  59. ret);
  60. return ret;
  61. }
  62. chip->client = client;
  63. chip->regmap = map;
  64. chip->irq = client->irq;
  65. chip->dev = &client->dev;
  66. dev_set_drvdata(chip->dev, chip);
  67. i2c_set_clientdata(chip->client, chip);
  68. ret = regmap_read(chip->regmap, PM80X_CHIP_ID, &val);
  69. if (ret < 0) {
  70. dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
  71. return ret;
  72. }
  73. for (i = 0; i < ARRAY_SIZE(chip_mapping); i++) {
  74. if (chip_mapping[i].id == PM80X_CHIP_ID_NUM(val)) {
  75. chip->type = chip_mapping[i].type;
  76. break;
  77. }
  78. }
  79. if (i == ARRAY_SIZE(chip_mapping)) {
  80. dev_err(chip->dev,
  81. "Failed to detect Marvell 88PM800:ChipID[0x%x]\n", val);
  82. return -EINVAL;
  83. }
  84. device_init_wakeup(&client->dev, 1);
  85. /*
  86. * workaround: set g_pm80x_chip to the first probed chip. if the
  87. * second chip is probed, just point to the companion to each
  88. * other so that pm805 can access those specific register. would
  89. * remove it after HW chip fixes the issue.
  90. */
  91. if (!g_pm80x_chip)
  92. g_pm80x_chip = chip;
  93. else {
  94. chip->companion = g_pm80x_chip->client;
  95. g_pm80x_chip->companion = chip->client;
  96. }
  97. return 0;
  98. }
  99. EXPORT_SYMBOL_GPL(pm80x_init);
  100. int pm80x_deinit(void)
  101. {
  102. /*
  103. * workaround: clear the dependency between pm800 and pm805.
  104. * would remove it after HW chip fixes the issue.
  105. */
  106. if (g_pm80x_chip->companion)
  107. g_pm80x_chip->companion = NULL;
  108. else
  109. g_pm80x_chip = NULL;
  110. return 0;
  111. }
  112. EXPORT_SYMBOL_GPL(pm80x_deinit);
  113. #ifdef CONFIG_PM_SLEEP
  114. static int pm80x_suspend(struct device *dev)
  115. {
  116. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  117. struct pm80x_chip *chip = i2c_get_clientdata(client);
  118. if (chip && chip->wu_flag)
  119. if (device_may_wakeup(chip->dev))
  120. enable_irq_wake(chip->irq);
  121. return 0;
  122. }
  123. static int pm80x_resume(struct device *dev)
  124. {
  125. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  126. struct pm80x_chip *chip = i2c_get_clientdata(client);
  127. if (chip && chip->wu_flag)
  128. if (device_may_wakeup(chip->dev))
  129. disable_irq_wake(chip->irq);
  130. return 0;
  131. }
  132. #endif
  133. SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
  134. EXPORT_SYMBOL_GPL(pm80x_pm_ops);
  135. MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
  136. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  137. MODULE_LICENSE("GPL");