ab8500-i2c.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/db8500-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_read(struct ab8500 *ab8500, u16 addr)
  23. {
  24. int ret;
  25. u8 data;
  26. ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
  27. if (ret < 0) {
  28. dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
  29. return ret;
  30. }
  31. return (int)data;
  32. }
  33. static int __devinit ab8500_i2c_probe(struct platform_device *plf)
  34. {
  35. const struct platform_device_id *platid = platform_get_device_id(plf);
  36. struct ab8500 *ab8500;
  37. struct resource *resource;
  38. int ret;
  39. ab8500 = kzalloc(sizeof *ab8500, GFP_KERNEL);
  40. if (!ab8500)
  41. return -ENOMEM;
  42. ab8500->dev = &plf->dev;
  43. resource = platform_get_resource(plf, IORESOURCE_IRQ, 0);
  44. if (!resource) {
  45. kfree(ab8500);
  46. return -ENODEV;
  47. }
  48. ab8500->irq = resource->start;
  49. ab8500->read = ab8500_i2c_read;
  50. ab8500->write = ab8500_i2c_write;
  51. platform_set_drvdata(plf, ab8500);
  52. ret = ab8500_init(ab8500, platid->driver_data);
  53. if (ret)
  54. kfree(ab8500);
  55. return ret;
  56. }
  57. static int __devexit ab8500_i2c_remove(struct platform_device *plf)
  58. {
  59. struct ab8500 *ab8500 = platform_get_drvdata(plf);
  60. ab8500_exit(ab8500);
  61. kfree(ab8500);
  62. return 0;
  63. }
  64. static const struct platform_device_id ab8500_id[] = {
  65. { "ab8500-i2c", AB8500_VERSION_AB8500 },
  66. { "ab8505-i2c", AB8500_VERSION_AB8505 },
  67. { "ab9540-i2c", AB8500_VERSION_AB9540 },
  68. { "ab8540-i2c", AB8500_VERSION_AB8540 },
  69. { }
  70. };
  71. static struct platform_driver ab8500_i2c_driver = {
  72. .driver = {
  73. .name = "ab8500-i2c",
  74. .owner = THIS_MODULE,
  75. },
  76. .probe = ab8500_i2c_probe,
  77. .remove = __devexit_p(ab8500_i2c_remove),
  78. .id_table = ab8500_id,
  79. };
  80. static int __init ab8500_i2c_init(void)
  81. {
  82. return platform_driver_register(&ab8500_i2c_driver);
  83. }
  84. static void __exit ab8500_i2c_exit(void)
  85. {
  86. platform_driver_unregister(&ab8500_i2c_driver);
  87. }
  88. arch_initcall(ab8500_i2c_init);
  89. module_exit(ab8500_i2c_exit);
  90. MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
  91. MODULE_DESCRIPTION("AB8500 Core access via PRCMU I2C");
  92. MODULE_LICENSE("GPL v2");