88pm80x.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /*
  21. * workaround: some registers needed by pm805 are defined in pm800, so
  22. * need to use this global variable to maintain the relation between
  23. * pm800 and pm805. would remove it after HW chip fixes the issue.
  24. */
  25. static struct pm80x_chip *g_pm80x_chip;
  26. const struct regmap_config pm80x_regmap_config = {
  27. .reg_bits = 8,
  28. .val_bits = 8,
  29. };
  30. EXPORT_SYMBOL_GPL(pm80x_regmap_config);
  31. int pm80x_init(struct i2c_client *client,
  32. const struct i2c_device_id *id)
  33. {
  34. struct pm80x_chip *chip;
  35. struct regmap *map;
  36. int ret = 0;
  37. chip =
  38. devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL);
  39. if (!chip)
  40. return -ENOMEM;
  41. map = devm_regmap_init_i2c(client, &pm80x_regmap_config);
  42. if (IS_ERR(map)) {
  43. ret = PTR_ERR(map);
  44. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  45. ret);
  46. return ret;
  47. }
  48. chip->id = id->driver_data;
  49. if (chip->id < CHIP_PM800 || chip->id > CHIP_PM805)
  50. return -EINVAL;
  51. chip->client = client;
  52. chip->regmap = map;
  53. chip->irq = client->irq;
  54. chip->dev = &client->dev;
  55. dev_set_drvdata(chip->dev, chip);
  56. i2c_set_clientdata(chip->client, chip);
  57. device_init_wakeup(&client->dev, 1);
  58. /*
  59. * workaround: set g_pm80x_chip to the first probed chip. if the
  60. * second chip is probed, just point to the companion to each
  61. * other so that pm805 can access those specific register. would
  62. * remove it after HW chip fixes the issue.
  63. */
  64. if (!g_pm80x_chip)
  65. g_pm80x_chip = chip;
  66. else {
  67. chip->companion = g_pm80x_chip->client;
  68. g_pm80x_chip->companion = chip->client;
  69. }
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(pm80x_init);
  73. int pm80x_deinit(void)
  74. {
  75. /*
  76. * workaround: clear the dependency between pm800 and pm805.
  77. * would remove it after HW chip fixes the issue.
  78. */
  79. if (g_pm80x_chip->companion)
  80. g_pm80x_chip->companion = NULL;
  81. else
  82. g_pm80x_chip = NULL;
  83. return 0;
  84. }
  85. EXPORT_SYMBOL_GPL(pm80x_deinit);
  86. #ifdef CONFIG_PM_SLEEP
  87. static int pm80x_suspend(struct device *dev)
  88. {
  89. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  90. struct pm80x_chip *chip = i2c_get_clientdata(client);
  91. if (chip && chip->wu_flag)
  92. if (device_may_wakeup(chip->dev))
  93. enable_irq_wake(chip->irq);
  94. return 0;
  95. }
  96. static int pm80x_resume(struct device *dev)
  97. {
  98. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  99. struct pm80x_chip *chip = i2c_get_clientdata(client);
  100. if (chip && chip->wu_flag)
  101. if (device_may_wakeup(chip->dev))
  102. disable_irq_wake(chip->irq);
  103. return 0;
  104. }
  105. #endif
  106. SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
  107. EXPORT_SYMBOL_GPL(pm80x_pm_ops);
  108. MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
  109. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  110. MODULE_LICENSE("GPL");