adxl34x-i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface)
  3. *
  4. * Enter bugs at http://blackfin.uclinux.org/
  5. *
  6. * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/input.h> /* BUS_I2C */
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include "adxl34x.h"
  14. static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
  15. {
  16. struct i2c_client *client = to_i2c_client(dev);
  17. return i2c_smbus_read_byte_data(client, reg);
  18. }
  19. static int adxl34x_smbus_write(struct device *dev,
  20. unsigned char reg, unsigned char val)
  21. {
  22. struct i2c_client *client = to_i2c_client(dev);
  23. return i2c_smbus_write_byte_data(client, reg, val);
  24. }
  25. static int adxl34x_smbus_read_block(struct device *dev,
  26. unsigned char reg, int count,
  27. void *buf)
  28. {
  29. struct i2c_client *client = to_i2c_client(dev);
  30. return i2c_smbus_read_i2c_block_data(client, reg, count, buf);
  31. }
  32. static int adxl34x_i2c_read_block(struct device *dev,
  33. unsigned char reg, int count,
  34. void *buf)
  35. {
  36. struct i2c_client *client = to_i2c_client(dev);
  37. int ret;
  38. ret = i2c_master_send(client, &reg, 1);
  39. if (ret < 0)
  40. return ret;
  41. ret = i2c_master_recv(client, buf, count);
  42. if (ret < 0)
  43. return ret;
  44. if (ret != count)
  45. return -EIO;
  46. return 0;
  47. }
  48. static const struct adxl34x_bus_ops adxl34x_smbus_bops = {
  49. .bustype = BUS_I2C,
  50. .write = adxl34x_smbus_write,
  51. .read = adxl34x_smbus_read,
  52. .read_block = adxl34x_smbus_read_block,
  53. };
  54. static const struct adxl34x_bus_ops adxl34x_i2c_bops = {
  55. .bustype = BUS_I2C,
  56. .write = adxl34x_smbus_write,
  57. .read = adxl34x_smbus_read,
  58. .read_block = adxl34x_i2c_read_block,
  59. };
  60. static int __devinit adxl34x_i2c_probe(struct i2c_client *client,
  61. const struct i2c_device_id *id)
  62. {
  63. struct adxl34x *ac;
  64. int error;
  65. error = i2c_check_functionality(client->adapter,
  66. I2C_FUNC_SMBUS_BYTE_DATA);
  67. if (!error) {
  68. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  69. return -EIO;
  70. }
  71. ac = adxl34x_probe(&client->dev, client->irq, false,
  72. i2c_check_functionality(client->adapter,
  73. I2C_FUNC_SMBUS_READ_I2C_BLOCK) ?
  74. &adxl34x_smbus_bops : &adxl34x_i2c_bops);
  75. if (IS_ERR(ac))
  76. return PTR_ERR(ac);
  77. i2c_set_clientdata(client, ac);
  78. return 0;
  79. }
  80. static int __devexit adxl34x_i2c_remove(struct i2c_client *client)
  81. {
  82. struct adxl34x *ac = i2c_get_clientdata(client);
  83. return adxl34x_remove(ac);
  84. }
  85. #ifdef CONFIG_PM
  86. static int adxl34x_i2c_suspend(struct i2c_client *client, pm_message_t message)
  87. {
  88. struct adxl34x *ac = i2c_get_clientdata(client);
  89. adxl34x_suspend(ac);
  90. return 0;
  91. }
  92. static int adxl34x_i2c_resume(struct i2c_client *client)
  93. {
  94. struct adxl34x *ac = i2c_get_clientdata(client);
  95. adxl34x_resume(ac);
  96. return 0;
  97. }
  98. #else
  99. # define adxl34x_i2c_suspend NULL
  100. # define adxl34x_i2c_resume NULL
  101. #endif
  102. static const struct i2c_device_id adxl34x_id[] = {
  103. { "adxl34x", 0 },
  104. { }
  105. };
  106. MODULE_DEVICE_TABLE(i2c, adxl34x_id);
  107. static struct i2c_driver adxl34x_driver = {
  108. .driver = {
  109. .name = "adxl34x",
  110. .owner = THIS_MODULE,
  111. },
  112. .probe = adxl34x_i2c_probe,
  113. .remove = __devexit_p(adxl34x_i2c_remove),
  114. .suspend = adxl34x_i2c_suspend,
  115. .resume = adxl34x_i2c_resume,
  116. .id_table = adxl34x_id,
  117. };
  118. static int __init adxl34x_i2c_init(void)
  119. {
  120. return i2c_add_driver(&adxl34x_driver);
  121. }
  122. module_init(adxl34x_i2c_init);
  123. static void __exit adxl34x_i2c_exit(void)
  124. {
  125. i2c_del_driver(&adxl34x_driver);
  126. }
  127. module_exit(adxl34x_i2c_exit);
  128. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  129. MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
  130. MODULE_LICENSE("GPL");