mr1989

Untitled

May 13th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  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.  
  10. #include <linux/input.h> /* BUS_I2C */
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/types.h>
  15. #include <linux/pm.h>
  16. #include "adxl34x.h"
  17.  
  18. static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
  19. {
  20. struct i2c_client *client = to_i2c_client(dev);
  21.  
  22. return i2c_smbus_read_byte_data(client, reg);
  23. }
  24.  
  25. static int adxl34x_smbus_write(struct device *dev,
  26. unsigned char reg, unsigned char val)
  27. {
  28. struct i2c_client *client = to_i2c_client(dev);
  29.  
  30. return i2c_smbus_write_byte_data(client, reg, val);
  31. }
  32.  
  33. static int adxl34x_smbus_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.  
  39. return i2c_smbus_read_i2c_block_data(client, reg, count, buf);
  40. }
  41.  
  42. static int adxl34x_i2c_read_block(struct device *dev,
  43. unsigned char reg, int count,
  44. void *buf)
  45. {
  46. struct i2c_client *client = to_i2c_client(dev);
  47. int ret;
  48.  
  49. ret = i2c_master_send(client, &reg, 1);
  50. if (ret < 0)
  51. return ret;
  52.  
  53. ret = i2c_master_recv(client, buf, count);
  54. if (ret < 0)
  55. return ret;
  56.  
  57. if (ret != count)
  58. return -EIO;
  59.  
  60. return 0;
  61. }
  62.  
  63. static const struct adxl34x_bus_ops adxl34x_smbus_bops = {
  64. .bustype = BUS_I2C,
  65. .write = adxl34x_smbus_write,
  66. .read = adxl34x_smbus_read,
  67. .read_block = adxl34x_smbus_read_block,
  68. };
  69.  
  70. static const struct adxl34x_bus_ops adxl34x_i2c_bops = {
  71. .bustype = BUS_I2C,
  72. .write = adxl34x_smbus_write,
  73. .read = adxl34x_smbus_read,
  74. .read_block = adxl34x_i2c_read_block,
  75. };
  76.  
  77. static int adxl34x_i2c_probe(struct i2c_client *client,
  78. const struct i2c_device_id *id)
  79. {
  80. struct adxl34x *ac;
  81. int error;
  82. /* Return 1 if adapter supports everything we need, 0 if not. */
  83. /*
  84. I2C_FUNC_SMBUS_BYTE_DATA = (I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
  85. */
  86. error = i2c_check_functionality(client->adapter,
  87. I2C_FUNC_SMBUS_BYTE_DATA);
  88. if (!error) {
  89. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  90. return -EIO;
  91. }
  92.  
  93. ac = adxl34x_probe(&client->dev,
  94. i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK) ? &adxl34x_smbus_bops : &adxl34x_i2c_bops);
  95.  
  96. if (IS_ERR(ac))
  97. return PTR_ERR(ac);
  98.  
  99. i2c_set_clientdata(client, ac);
  100.  
  101. return 0;
  102. }
  103.  
  104. static int adxl34x_i2c_remove(struct i2c_client *client)
  105. {
  106. struct adxl34x *ac = i2c_get_clientdata(client);
  107.  
  108. return adxl34x_remove(ac);
  109. }
  110.  
  111. static int __maybe_unused adxl34x_i2c_suspend(struct device *dev)
  112. {
  113. struct i2c_client *client = to_i2c_client(dev);
  114. struct adxl34x *ac = i2c_get_clientdata(client);
  115.  
  116. //adxl34x_suspend(ac);//not using it for a while.
  117.  
  118. return 0;
  119. }
  120.  
  121. static int __maybe_unused adxl34x_i2c_resume(struct device *dev)
  122. {
  123. struct i2c_client *client = to_i2c_client(dev);
  124. struct adxl34x *ac = i2c_get_clientdata(client);
  125.  
  126. //adxl34x_resume(ac);//not using it for a while.
  127.  
  128. return 0;
  129. }
  130.  
  131. static SIMPLE_DEV_PM_OPS(adxl34x_i2c_pm, adxl34x_i2c_suspend,
  132. adxl34x_i2c_resume);
  133.  
  134. static const struct i2c_device_id adxl34x_id[] = {
  135. { "adxl34x", 0 },
  136. { }
  137. };
  138.  
  139. MODULE_DEVICE_TABLE(i2c, adxl34x_id);
  140.  
  141. #ifdef CONFIG_OF
  142. static const struct of_device_id adxl34x_of_id[] = {
  143. /*
  144. * The ADXL346 is backward-compatible with the ADXL345. Differences are
  145. * handled by runtime detection of the device model, there's thus no
  146. * need for listing the "adi,adxl346" compatible value explicitly.
  147. */
  148. { .compatible = "adi,adxl345", },
  149. /*
  150. * Deprecated, DT nodes should use one or more of the device-specific
  151. * compatible values "adi,adxl345" and "adi,adxl346".
  152. */
  153. { .compatible = "adi,adxl34x", },
  154. { }
  155. };
  156.  
  157. MODULE_DEVICE_TABLE(of, adxl34x_of_id);
  158. #endif
  159.  
  160. static struct i2c_driver adxl34x_driver = {
  161. .driver = {
  162. .name = "adxl34x",
  163. .pm = &adxl34x_i2c_pm,
  164. .of_match_table = of_match_ptr(adxl34x_of_id),
  165. },
  166. .probe = adxl34x_i2c_probe,
  167. .remove = adxl34x_i2c_remove,
  168. .id_table = adxl34x_id,
  169. };
  170.  
  171. module_i2c_driver(adxl34x_driver);
  172.  
  173. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  174. MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
  175. MODULE_LICENSE("GPL");
Add Comment
Please, Sign In to add comment