da311.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * IIO driver for the MiraMEMS DA311 3-axis accelerometer
  3. *
  4. * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
  5. * Copyright (c) 2011-2013 MiraMEMS Sensing Technology Co., Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/sysfs.h>
  15. #include <linux/byteorder/generic.h>
  16. #define DA311_CHIP_ID 0x13
  17. /*
  18. * Note register addressed go from 0 - 0x3f and then wrap.
  19. * For some reason there are 2 banks with 0 - 0x3f addresses,
  20. * rather then a single 0-0x7f bank.
  21. */
  22. /* Bank 0 regs */
  23. #define DA311_REG_BANK 0x0000
  24. #define DA311_REG_LDO_REG 0x0006
  25. #define DA311_REG_CHIP_ID 0x000f
  26. #define DA311_REG_TEMP_CFG_REG 0x001f
  27. #define DA311_REG_CTRL_REG1 0x0020
  28. #define DA311_REG_CTRL_REG3 0x0022
  29. #define DA311_REG_CTRL_REG4 0x0023
  30. #define DA311_REG_CTRL_REG5 0x0024
  31. #define DA311_REG_CTRL_REG6 0x0025
  32. #define DA311_REG_STATUS_REG 0x0027
  33. #define DA311_REG_OUT_X_L 0x0028
  34. #define DA311_REG_OUT_X_H 0x0029
  35. #define DA311_REG_OUT_Y_L 0x002a
  36. #define DA311_REG_OUT_Y_H 0x002b
  37. #define DA311_REG_OUT_Z_L 0x002c
  38. #define DA311_REG_OUT_Z_H 0x002d
  39. #define DA311_REG_INT1_CFG 0x0030
  40. #define DA311_REG_INT1_SRC 0x0031
  41. #define DA311_REG_INT1_THS 0x0032
  42. #define DA311_REG_INT1_DURATION 0x0033
  43. #define DA311_REG_INT2_CFG 0x0034
  44. #define DA311_REG_INT2_SRC 0x0035
  45. #define DA311_REG_INT2_THS 0x0036
  46. #define DA311_REG_INT2_DURATION 0x0037
  47. #define DA311_REG_CLICK_CFG 0x0038
  48. #define DA311_REG_CLICK_SRC 0x0039
  49. #define DA311_REG_CLICK_THS 0x003a
  50. #define DA311_REG_TIME_LIMIT 0x003b
  51. #define DA311_REG_TIME_LATENCY 0x003c
  52. #define DA311_REG_TIME_WINDOW 0x003d
  53. /* Bank 1 regs */
  54. #define DA311_REG_SOFT_RESET 0x0105
  55. #define DA311_REG_OTP_XOFF_L 0x0110
  56. #define DA311_REG_OTP_XOFF_H 0x0111
  57. #define DA311_REG_OTP_YOFF_L 0x0112
  58. #define DA311_REG_OTP_YOFF_H 0x0113
  59. #define DA311_REG_OTP_ZOFF_L 0x0114
  60. #define DA311_REG_OTP_ZOFF_H 0x0115
  61. #define DA311_REG_OTP_XSO 0x0116
  62. #define DA311_REG_OTP_YSO 0x0117
  63. #define DA311_REG_OTP_ZSO 0x0118
  64. #define DA311_REG_OTP_TRIM_OSC 0x011b
  65. #define DA311_REG_LPF_ABSOLUTE 0x011c
  66. #define DA311_REG_TEMP_OFF1 0x0127
  67. #define DA311_REG_TEMP_OFF2 0x0128
  68. #define DA311_REG_TEMP_OFF3 0x0129
  69. #define DA311_REG_OTP_TRIM_THERM_H 0x011a
  70. /*
  71. * a value of + or -1024 corresponds to + or - 1G
  72. * scale = 9.81 / 1024 = 0.009580078
  73. */
  74. static const int da311_nscale = 9580078;
  75. #define DA311_CHANNEL(reg, axis) { \
  76. .type = IIO_ACCEL, \
  77. .address = reg, \
  78. .modified = 1, \
  79. .channel2 = IIO_MOD_##axis, \
  80. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  81. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  82. }
  83. static const struct iio_chan_spec da311_channels[] = {
  84. /* | 0x80 comes from the android driver */
  85. DA311_CHANNEL(DA311_REG_OUT_X_L | 0x80, X),
  86. DA311_CHANNEL(DA311_REG_OUT_Y_L | 0x80, Y),
  87. DA311_CHANNEL(DA311_REG_OUT_Z_L | 0x80, Z),
  88. };
  89. struct da311_data {
  90. struct i2c_client *client;
  91. };
  92. static int da311_register_mask_write(struct i2c_client *client, u16 addr,
  93. u8 mask, u8 data)
  94. {
  95. int ret;
  96. u8 tmp_data = 0;
  97. if (addr & 0xff00) {
  98. /* Select bank 1 */
  99. ret = i2c_smbus_write_byte_data(client, DA311_REG_BANK, 0x01);
  100. if (ret < 0)
  101. return ret;
  102. }
  103. if (mask != 0xff) {
  104. ret = i2c_smbus_read_byte_data(client, addr);
  105. if (ret < 0)
  106. return ret;
  107. tmp_data = ret;
  108. }
  109. tmp_data &= ~mask;
  110. tmp_data |= data & mask;
  111. ret = i2c_smbus_write_byte_data(client, addr & 0xff, tmp_data);
  112. if (ret < 0)
  113. return ret;
  114. if (addr & 0xff00) {
  115. /* Back to bank 0 */
  116. ret = i2c_smbus_write_byte_data(client, DA311_REG_BANK, 0x00);
  117. if (ret < 0)
  118. return ret;
  119. }
  120. return 0;
  121. }
  122. /* Init sequence taken from the android driver */
  123. static int da311_reset(struct i2c_client *client)
  124. {
  125. const struct {
  126. u16 addr;
  127. u8 mask;
  128. u8 data;
  129. } init_data[] = {
  130. { DA311_REG_TEMP_CFG_REG, 0xff, 0x08 },
  131. { DA311_REG_CTRL_REG5, 0xff, 0x80 },
  132. { DA311_REG_CTRL_REG4, 0x30, 0x00 },
  133. { DA311_REG_CTRL_REG1, 0xff, 0x6f },
  134. { DA311_REG_TEMP_CFG_REG, 0xff, 0x88 },
  135. { DA311_REG_LDO_REG, 0xff, 0x02 },
  136. { DA311_REG_OTP_TRIM_OSC, 0xff, 0x27 },
  137. { DA311_REG_LPF_ABSOLUTE, 0xff, 0x30 },
  138. { DA311_REG_TEMP_OFF1, 0xff, 0x3f },
  139. { DA311_REG_TEMP_OFF2, 0xff, 0xff },
  140. { DA311_REG_TEMP_OFF3, 0xff, 0x0f },
  141. };
  142. int i, ret;
  143. /* Reset */
  144. ret = da311_register_mask_write(client, DA311_REG_SOFT_RESET,
  145. 0xff, 0xaa);
  146. if (ret < 0)
  147. return ret;
  148. for (i = 0; i < ARRAY_SIZE(init_data); i++) {
  149. ret = da311_register_mask_write(client,
  150. init_data[i].addr,
  151. init_data[i].mask,
  152. init_data[i].data);
  153. if (ret < 0)
  154. return ret;
  155. }
  156. return 0;
  157. }
  158. static int da311_enable(struct i2c_client *client, bool enable)
  159. {
  160. u8 data = enable ? 0x00 : 0x20;
  161. return da311_register_mask_write(client, DA311_REG_TEMP_CFG_REG,
  162. 0x20, data);
  163. }
  164. static int da311_read_raw(struct iio_dev *indio_dev,
  165. struct iio_chan_spec const *chan,
  166. int *val, int *val2, long mask)
  167. {
  168. struct da311_data *data = iio_priv(indio_dev);
  169. int ret;
  170. switch (mask) {
  171. case IIO_CHAN_INFO_RAW:
  172. ret = i2c_smbus_read_word_data(data->client, chan->address);
  173. if (ret < 0)
  174. return ret;
  175. /*
  176. * Values are 12 bits, stored as 16 bits with the 4
  177. * least significant bits always 0.
  178. */
  179. *val = (short)ret >> 4;
  180. return IIO_VAL_INT;
  181. case IIO_CHAN_INFO_SCALE:
  182. *val = 0;
  183. *val2 = da311_nscale;
  184. return IIO_VAL_INT_PLUS_NANO;
  185. default:
  186. return -EINVAL;
  187. }
  188. }
  189. static const struct iio_info da311_info = {
  190. .driver_module = THIS_MODULE,
  191. .read_raw = da311_read_raw,
  192. };
  193. static int da311_probe(struct i2c_client *client,
  194. const struct i2c_device_id *id)
  195. {
  196. int ret;
  197. struct iio_dev *indio_dev;
  198. struct da311_data *data;
  199. ret = i2c_smbus_read_byte_data(client, DA311_REG_CHIP_ID);
  200. if (ret != DA311_CHIP_ID)
  201. return (ret < 0) ? ret : -ENODEV;
  202. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  203. if (!indio_dev)
  204. return -ENOMEM;
  205. data = iio_priv(indio_dev);
  206. data->client = client;
  207. i2c_set_clientdata(client, indio_dev);
  208. indio_dev->dev.parent = &client->dev;
  209. indio_dev->info = &da311_info;
  210. indio_dev->name = "da311";
  211. indio_dev->modes = INDIO_DIRECT_MODE;
  212. indio_dev->channels = da311_channels;
  213. indio_dev->num_channels = ARRAY_SIZE(da311_channels);
  214. ret = da311_reset(client);
  215. if (ret < 0)
  216. return ret;
  217. ret = da311_enable(client, true);
  218. if (ret < 0)
  219. return ret;
  220. ret = iio_device_register(indio_dev);
  221. if (ret < 0) {
  222. dev_err(&client->dev, "device_register failed\n");
  223. da311_enable(client, false);
  224. }
  225. return ret;
  226. }
  227. static int da311_remove(struct i2c_client *client)
  228. {
  229. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  230. iio_device_unregister(indio_dev);
  231. return da311_enable(client, false);
  232. }
  233. #ifdef CONFIG_PM_SLEEP
  234. static int da311_suspend(struct device *dev)
  235. {
  236. return da311_enable(to_i2c_client(dev), false);
  237. }
  238. static int da311_resume(struct device *dev)
  239. {
  240. return da311_enable(to_i2c_client(dev), true);
  241. }
  242. #endif
  243. static SIMPLE_DEV_PM_OPS(da311_pm_ops, da311_suspend, da311_resume);
  244. static const struct i2c_device_id da311_i2c_id[] = {
  245. {"da311", 0},
  246. {}
  247. };
  248. MODULE_DEVICE_TABLE(i2c, da311_i2c_id);
  249. static struct i2c_driver da311_driver = {
  250. .driver = {
  251. .name = "da311",
  252. .pm = &da311_pm_ops,
  253. },
  254. .probe = da311_probe,
  255. .remove = da311_remove,
  256. .id_table = da311_i2c_id,
  257. };
  258. module_i2c_driver(da311_driver);
  259. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  260. MODULE_DESCRIPTION("MiraMEMS DA311 3-Axis Accelerometer driver");
  261. MODULE_LICENSE("GPL v2");