adxl34x-i2c.c 3.6 KB

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