88pm80x.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 __devinit 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. goto err_regmap_init;
  47. }
  48. chip->id = id->driver_data;
  49. if (chip->id < CHIP_PM800 || chip->id > CHIP_PM805) {
  50. ret = -EINVAL;
  51. goto err_chip_id;
  52. }
  53. chip->client = client;
  54. chip->regmap = map;
  55. chip->irq = client->irq;
  56. chip->dev = &client->dev;
  57. dev_set_drvdata(chip->dev, chip);
  58. i2c_set_clientdata(chip->client, chip);
  59. device_init_wakeup(&client->dev, 1);
  60. /*
  61. * workaround: set g_pm80x_chip to the first probed chip. if the
  62. * second chip is probed, just point to the companion to each
  63. * other so that pm805 can access those specific register. would
  64. * remove it after HW chip fixes the issue.
  65. */
  66. if (!g_pm80x_chip)
  67. g_pm80x_chip = chip;
  68. else {
  69. chip->companion = g_pm80x_chip->client;
  70. g_pm80x_chip->companion = chip->client;
  71. }
  72. return 0;
  73. err_chip_id:
  74. regmap_exit(map);
  75. err_regmap_init:
  76. devm_kfree(&client->dev, chip);
  77. return ret;
  78. }
  79. EXPORT_SYMBOL_GPL(pm80x_init);
  80. int pm80x_deinit(struct i2c_client *client)
  81. {
  82. struct pm80x_chip *chip = i2c_get_clientdata(client);
  83. /*
  84. * workaround: clear the dependency between pm800 and pm805.
  85. * would remove it after HW chip fixes the issue.
  86. */
  87. if (g_pm80x_chip->companion)
  88. g_pm80x_chip->companion = NULL;
  89. else
  90. g_pm80x_chip = NULL;
  91. regmap_exit(chip->regmap);
  92. devm_kfree(&client->dev, chip);
  93. return 0;
  94. }
  95. EXPORT_SYMBOL_GPL(pm80x_deinit);
  96. #ifdef CONFIG_PM_SLEEP
  97. static int pm80x_suspend(struct device *dev)
  98. {
  99. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  100. struct pm80x_chip *chip = i2c_get_clientdata(client);
  101. if (chip && chip->wu_flag)
  102. if (device_may_wakeup(chip->dev))
  103. enable_irq_wake(chip->irq);
  104. return 0;
  105. }
  106. static int pm80x_resume(struct device *dev)
  107. {
  108. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  109. struct pm80x_chip *chip = i2c_get_clientdata(client);
  110. if (chip && chip->wu_flag)
  111. if (device_may_wakeup(chip->dev))
  112. disable_irq_wake(chip->irq);
  113. return 0;
  114. }
  115. #endif
  116. SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
  117. EXPORT_SYMBOL_GPL(pm80x_pm_ops);
  118. MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
  119. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  120. MODULE_LICENSE("GPL");