ab8500-i2c.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. static struct platform_driver ab8500_i2c_driver = {
  83. .driver = {
  84. .name = "ab8500-i2c",
  85. .owner = THIS_MODULE,
  86. },
  87. .probe = ab8500_i2c_probe,
  88. .remove = __devexit_p(ab8500_i2c_remove),
  89. .id_table = ab8500_id,
  90. };
  91. static int __init ab8500_i2c_init(void)
  92. {
  93. return platform_driver_register(&ab8500_i2c_driver);
  94. }
  95. static void __exit ab8500_i2c_exit(void)
  96. {
  97. platform_driver_unregister(&ab8500_i2c_driver);
  98. }
  99. arch_initcall(ab8500_i2c_init);
  100. module_exit(ab8500_i2c_exit);
  101. MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
  102. MODULE_DESCRIPTION("AB8500 Core access via PRCMU I2C");
  103. MODULE_LICENSE("GPL v2");