ab8500-i2c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson.
  4. * License Terms: GNU General Public License v2
  5. * This file was based on drivers/mfd/ab8500-spi.c
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mfd/abx500/ab8500.h>
  13. #include <linux/mfd/dbx500-prcmu.h>
  14. static int ab8500_i2c_write(struct ab8500 *ab8500, u16 addr, u8 data)
  15. {
  16. int ret;
  17. ret = prcmu_abb_write((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
  18. if (ret < 0)
  19. dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
  20. return ret;
  21. }
  22. static int ab8500_i2c_write_masked(struct ab8500 *ab8500, u16 addr, u8 mask,
  23. u8 data)
  24. {
  25. int ret;
  26. ret = prcmu_abb_write_masked((u8)(addr >> 8), (u8)(addr & 0xFF), &data,
  27. &mask, 1);
  28. if (ret < 0)
  29. dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
  30. return ret;
  31. }
  32. static int ab8500_i2c_read(struct ab8500 *ab8500, u16 addr)
  33. {
  34. int ret;
  35. u8 data;
  36. ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
  37. if (ret < 0) {
  38. dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
  39. return ret;
  40. }
  41. return (int)data;
  42. }
  43. static int __devinit ab8500_i2c_probe(struct platform_device *plf)
  44. {
  45. const struct platform_device_id *platid = platform_get_device_id(plf);
  46. struct ab8500 *ab8500;
  47. struct resource *resource;
  48. int ret;
  49. ab8500 = kzalloc(sizeof *ab8500, GFP_KERNEL);
  50. if (!ab8500)
  51. return -ENOMEM;
  52. ab8500->dev = &plf->dev;
  53. resource = platform_get_resource(plf, IORESOURCE_IRQ, 0);
  54. if (!resource) {
  55. kfree(ab8500);
  56. return -ENODEV;
  57. }
  58. ab8500->irq = resource->start;
  59. ab8500->read = ab8500_i2c_read;
  60. ab8500->write = ab8500_i2c_write;
  61. ab8500->write_masked = ab8500_i2c_write_masked;
  62. platform_set_drvdata(plf, ab8500);
  63. ret = ab8500_init(ab8500, platid->driver_data);
  64. if (ret)
  65. kfree(ab8500);
  66. return ret;
  67. }
  68. static int __devexit ab8500_i2c_remove(struct platform_device *plf)
  69. {
  70. struct ab8500 *ab8500 = platform_get_drvdata(plf);
  71. ab8500_exit(ab8500);
  72. kfree(ab8500);
  73. return 0;
  74. }
  75. static const struct platform_device_id ab8500_id[] = {
  76. { "ab8500-i2c", AB8500_VERSION_AB8500 },
  77. { "ab8505-i2c", AB8500_VERSION_AB8505 },
  78. { "ab9540-i2c", AB8500_VERSION_AB9540 },
  79. { "ab8540-i2c", AB8500_VERSION_AB8540 },
  80. { }
  81. };
  82. #ifdef CONFIG_PM
  83. static int ab8500_i2c_suspend(struct device *dev)
  84. {
  85. struct ab8500 *ab = dev_get_drvdata(dev);
  86. disable_irq(ab->irq);
  87. enable_irq_wake(ab->irq);
  88. return 0;
  89. }
  90. static int ab8500_i2c_resume(struct device *dev)
  91. {
  92. struct ab8500 *ab = dev_get_drvdata(dev);
  93. disable_irq_wake(ab->irq);
  94. enable_irq(ab->irq);
  95. return 0;
  96. }
  97. static const struct dev_pm_ops ab8500_i2c_pm_ops = {
  98. .suspend = ab8500_i2c_suspend,
  99. .resume = ab8500_i2c_resume,
  100. };
  101. #define AB8500_I2C_PM_OPS (&ab8500_i2c_pm_ops)
  102. #else
  103. #define AB8500_I2C_PM_OPS NULL
  104. #endif
  105. static struct platform_driver ab8500_i2c_driver = {
  106. .driver = {
  107. .name = "ab8500-i2c",
  108. .owner = THIS_MODULE,
  109. .pm = AB8500_I2C_PM_OPS,
  110. },
  111. .probe = ab8500_i2c_probe,
  112. .remove = __devexit_p(ab8500_i2c_remove),
  113. .id_table = ab8500_id,
  114. };
  115. static int __init ab8500_i2c_init(void)
  116. {
  117. return platform_driver_register(&ab8500_i2c_driver);
  118. }
  119. static void __exit ab8500_i2c_exit(void)
  120. {
  121. platform_driver_unregister(&ab8500_i2c_driver);
  122. }
  123. arch_initcall(ab8500_i2c_init);
  124. module_exit(ab8500_i2c_exit);
  125. MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
  126. MODULE_DESCRIPTION("AB8500 Core access via PRCMU I2C");
  127. MODULE_LICENSE("GPL v2");