mr1989

Untitled

May 21st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 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/slab.h>
  16. #include <linux/pm.h>
  17. #include <linux/input/adxl34x.h>
  18. #include "adxl34x.h"
  19.  
  20.  
  21. static const struct adxl34x_platform_data adxl34x_default_init = {
  22. .tap_threshold = 35,
  23. .tap_duration = 3,
  24. .tap_latency = 20,
  25. .tap_window = 20,
  26. .tap_axis_control = ADXL_TAP_X_EN | ADXL_TAP_Y_EN | ADXL_TAP_Z_EN,
  27. .act_axis_control = 0xFF,
  28. .activity_threshold = 6,
  29. .inactivity_threshold = 4,
  30. .inactivity_time = 3,
  31. .free_fall_threshold = 8,
  32. .free_fall_time = 0x20,
  33. .data_rate = 8,
  34. .data_range = ADXL_FULL_RES,
  35.  
  36. .ev_type = EV_ABS,
  37. .ev_code_x = ABS_X, /* EV_REL */
  38. .ev_code_y = ABS_Y, /* EV_REL */
  39. .ev_code_z = ABS_Z, /* EV_REL */
  40.  
  41. .ev_code_tap = {BTN_TOUCH, BTN_TOUCH, BTN_TOUCH}, /* EV_KEY {x,y,z} */
  42. .power_mode = ADXL_AUTO_SLEEP | ADXL_LINK,
  43. .fifo_mode = ADXL_FIFO_STREAM,
  44. .watermark = 0,
  45. };
  46.  
  47. int adxl34x_remove(struct adxl34x *ac)
  48. {
  49. //sysfs_remove_group(&ac->dev->kobj, &adxl34x_attr_group);
  50. //free_irq(ac->irq, ac);
  51. // input_unregister_device(ac->input);
  52. dev_dbg(ac->dev, "unregistered accelerometer\n");
  53. kfree(ac);
  54.  
  55. return 0;
  56. }
  57. EXPORT_SYMBOL_GPL(adxl34x_remove);
  58. static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
  59. {
  60. struct i2c_client *client = to_i2c_client(dev);
  61. printk("adxl34x_smbus_read<<\n");
  62. return i2c_smbus_read_byte_data(client, reg);
  63. }
  64.  
  65. static int adxl34x_smbus_write(struct device *dev,
  66. unsigned char reg, unsigned char val)
  67. {
  68. struct i2c_client *client = to_i2c_client(dev);
  69. printk("adxl34x_smbus_write<<\n");
  70. return i2c_smbus_write_byte_data(client, reg, val);
  71. }
  72.  
  73.  
  74. static const struct adxl34x_bus_ops adxl34x_smbus_bops = {
  75. .bustype = BUS_I2C,
  76. .write = adxl34x_smbus_write,
  77. .read = adxl34x_smbus_read,
  78. };
  79.  
  80. static int adxl34x_i2c_probe(struct i2c_client *client)
  81. {
  82. struct adxl34x *ac;
  83. const struct adxl34x_platform_data *pdata;
  84. int error, err=0;
  85. unsigned char revid;
  86.  
  87. /*
  88. check if i2c adapter supports smbus byte transactions
  89. */
  90. error = i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA);
  91. if (!error) {
  92. printk( "SMBUS Byte Data not Supported\n");
  93. return -EIO;
  94. }
  95.  
  96. /*
  97. allocating memory for struct adxl34x
  98. */
  99. ac = kzalloc(sizeof(*ac), GFP_KERNEL);
  100.  
  101. if (!ac) {
  102. err = -ENOMEM;
  103. printk("Kzalloc failed\n");
  104. goto err_free_mem;
  105. }
  106.  
  107. /*
  108. pdata gets the pointer to the driver data
  109. */
  110. pdata = dev_get_platdata(&client->dev);
  111. if (!pdata) {
  112. pdata = &adxl34x_default_init;
  113. };
  114.  
  115. /*
  116. putting data to struct adxl34x
  117. */
  118. ac->pdata = *pdata;
  119. pdata = &ac->pdata;
  120.  
  121. ac->dev = &client->dev;
  122. ac->bops = &adxl34x_smbus_bops;
  123. revid = AC_READ(ac, DEVID);
  124. printk("REVID : %c\n", revid);
  125.  
  126. switch (revid) {
  127. case ID_ADXL345:
  128. ac->model = 345;
  129. break;
  130. case ID_ADXL346:
  131. ac->model = 346;
  132. break;
  133. default:
  134. //dev_err(dev, "Failed to probe %s\n", input_dev->name);
  135. err = -ENODEV;
  136. goto err_free_mem;
  137. }
  138.  
  139. i2c_set_clientdata(client, ac);
  140.  
  141. err_free_mem:
  142. printk("ADXL-PROBE: before kfree, err %d, ac->model %d\n", err, ac->model);
  143. kfree(ac);
  144.  
  145. return err;
  146.  
  147. }
  148.  
  149. static int adxl34x_i2c_remove(struct i2c_client *client)
  150. {
  151. struct adxl34x *ac = i2c_get_clientdata(client);
  152.  
  153. return adxl34x_remove(ac);
  154. }
  155.  
  156. static int __maybe_unused adxl34x_i2c_suspend(struct device *dev)
  157. {
  158. struct i2c_client *client = to_i2c_client(dev);
  159. struct adxl34x *ac = i2c_get_clientdata(client);
  160.  
  161. //adxl34x_suspend(ac);//not using it for a while.
  162.  
  163. return 0;
  164. }
  165.  
  166. /*
  167. static const struct i2c_device_id adxl34x_id[] = {
  168. { "adxl345", 0 },
  169. { }
  170. };
  171.  
  172. MODULE_DEVICE_TABLE(i2c, adxl34x_id);
  173. */
  174. #ifdef CONFIG_OF
  175. static const struct of_device_id adxl34x_of_id[] = {
  176. /*
  177. * The ADXL346 is backward-compatible with the ADXL345. Differences are
  178. * handled by runtime detection of the device model, there's thus no
  179. * need for listing the "adi,adxl346" compatible value explicitly.
  180. */
  181. { .compatible = "adi,adxl345", },
  182. /*
  183. * Deprecated, DT nodes should use one or more of the device-specific
  184. * compatible values "adi,adxl345" and "adi,adxl346".
  185. */
  186. { .compatible = "adi,adxl34x", },
  187. { }
  188. };
  189.  
  190. MODULE_DEVICE_TABLE(of, adxl34x_of_id);
  191. #endif
  192.  
  193. static struct i2c_driver adxl34x_driver = {
  194. .driver = {
  195. .name = "adxl34x",
  196. //.pm = &adxl34x_i2c_pm,
  197. .of_match_table = of_match_ptr(adxl34x_of_id),
  198. },
  199. .probe_new = adxl34x_i2c_probe,
  200. .remove = adxl34x_i2c_remove,
  201. //.id_table = adxl34x_id,
  202. };
  203.  
  204. module_i2c_driver(adxl34x_driver);
  205.  
  206. MODULE_AUTHOR("Michael Hennerich <[email protected]>");
  207. MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
  208. MODULE_LICENSE("GPL");
Add Comment
Please, Sign In to add comment