st_gyro_core.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * STMicroelectronics gyroscopes driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <denis.ciocca@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/errno.h>
  14. #include <linux/types.h>
  15. #include <linux/mutex.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/gpio.h>
  19. #include <linux/irq.h>
  20. #include <linux/delay.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/trigger.h>
  24. #include <linux/iio/buffer.h>
  25. #include <linux/iio/common/st_sensors.h>
  26. #include "st_gyro.h"
  27. /* DEFAULT VALUE FOR SENSORS */
  28. #define ST_GYRO_DEFAULT_OUT_X_L_ADDR 0x28
  29. #define ST_GYRO_DEFAULT_OUT_Y_L_ADDR 0x2a
  30. #define ST_GYRO_DEFAULT_OUT_Z_L_ADDR 0x2c
  31. /* FULLSCALE */
  32. #define ST_GYRO_FS_AVL_250DPS 250
  33. #define ST_GYRO_FS_AVL_500DPS 500
  34. #define ST_GYRO_FS_AVL_2000DPS 2000
  35. /* CUSTOM VALUES FOR SENSOR 1 */
  36. #define ST_GYRO_1_WAI_EXP 0xd3
  37. #define ST_GYRO_1_ODR_ADDR 0x20
  38. #define ST_GYRO_1_ODR_MASK 0xc0
  39. #define ST_GYRO_1_ODR_AVL_100HZ_VAL 0x00
  40. #define ST_GYRO_1_ODR_AVL_200HZ_VAL 0x01
  41. #define ST_GYRO_1_ODR_AVL_400HZ_VAL 0x02
  42. #define ST_GYRO_1_ODR_AVL_800HZ_VAL 0x03
  43. #define ST_GYRO_1_PW_ADDR 0x20
  44. #define ST_GYRO_1_PW_MASK 0x08
  45. #define ST_GYRO_1_FS_ADDR 0x23
  46. #define ST_GYRO_1_FS_MASK 0x30
  47. #define ST_GYRO_1_FS_AVL_250_VAL 0x00
  48. #define ST_GYRO_1_FS_AVL_500_VAL 0x01
  49. #define ST_GYRO_1_FS_AVL_2000_VAL 0x02
  50. #define ST_GYRO_1_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
  51. #define ST_GYRO_1_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
  52. #define ST_GYRO_1_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
  53. #define ST_GYRO_1_BDU_ADDR 0x23
  54. #define ST_GYRO_1_BDU_MASK 0x80
  55. #define ST_GYRO_1_DRDY_IRQ_ADDR 0x22
  56. #define ST_GYRO_1_DRDY_IRQ_MASK 0x08
  57. #define ST_GYRO_1_MULTIREAD_BIT true
  58. /* CUSTOM VALUES FOR SENSOR 2 */
  59. #define ST_GYRO_2_WAI_EXP 0xd4
  60. #define ST_GYRO_2_ODR_ADDR 0x20
  61. #define ST_GYRO_2_ODR_MASK 0xc0
  62. #define ST_GYRO_2_ODR_AVL_95HZ_VAL 0x00
  63. #define ST_GYRO_2_ODR_AVL_190HZ_VAL 0x01
  64. #define ST_GYRO_2_ODR_AVL_380HZ_VAL 0x02
  65. #define ST_GYRO_2_ODR_AVL_760HZ_VAL 0x03
  66. #define ST_GYRO_2_PW_ADDR 0x20
  67. #define ST_GYRO_2_PW_MASK 0x08
  68. #define ST_GYRO_2_FS_ADDR 0x23
  69. #define ST_GYRO_2_FS_MASK 0x30
  70. #define ST_GYRO_2_FS_AVL_250_VAL 0x00
  71. #define ST_GYRO_2_FS_AVL_500_VAL 0x01
  72. #define ST_GYRO_2_FS_AVL_2000_VAL 0x02
  73. #define ST_GYRO_2_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
  74. #define ST_GYRO_2_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
  75. #define ST_GYRO_2_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
  76. #define ST_GYRO_2_BDU_ADDR 0x23
  77. #define ST_GYRO_2_BDU_MASK 0x80
  78. #define ST_GYRO_2_DRDY_IRQ_ADDR 0x22
  79. #define ST_GYRO_2_DRDY_IRQ_MASK 0x08
  80. #define ST_GYRO_2_MULTIREAD_BIT true
  81. static const struct iio_chan_spec st_gyro_16bit_channels[] = {
  82. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL, ST_SENSORS_SCAN_X,
  83. IIO_MOD_X, IIO_LE, ST_SENSORS_DEFAULT_16_REALBITS,
  84. ST_GYRO_DEFAULT_OUT_X_L_ADDR),
  85. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL, ST_SENSORS_SCAN_Y,
  86. IIO_MOD_Y, IIO_LE, ST_SENSORS_DEFAULT_16_REALBITS,
  87. ST_GYRO_DEFAULT_OUT_Y_L_ADDR),
  88. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL, ST_SENSORS_SCAN_Z,
  89. IIO_MOD_Z, IIO_LE, ST_SENSORS_DEFAULT_16_REALBITS,
  90. ST_GYRO_DEFAULT_OUT_Z_L_ADDR),
  91. IIO_CHAN_SOFT_TIMESTAMP(3)
  92. };
  93. static const struct st_sensors st_gyro_sensors[] = {
  94. {
  95. .wai = ST_GYRO_1_WAI_EXP,
  96. .sensors_supported = {
  97. [0] = L3G4200D_GYRO_DEV_NAME,
  98. [1] = LSM330DL_GYRO_DEV_NAME,
  99. },
  100. .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
  101. .odr = {
  102. .addr = ST_GYRO_1_ODR_ADDR,
  103. .mask = ST_GYRO_1_ODR_MASK,
  104. .odr_avl = {
  105. { 100, ST_GYRO_1_ODR_AVL_100HZ_VAL, },
  106. { 200, ST_GYRO_1_ODR_AVL_200HZ_VAL, },
  107. { 400, ST_GYRO_1_ODR_AVL_400HZ_VAL, },
  108. { 800, ST_GYRO_1_ODR_AVL_800HZ_VAL, },
  109. },
  110. },
  111. .pw = {
  112. .addr = ST_GYRO_1_PW_ADDR,
  113. .mask = ST_GYRO_1_PW_MASK,
  114. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  115. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  116. },
  117. .enable_axis = {
  118. .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
  119. .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
  120. },
  121. .fs = {
  122. .addr = ST_GYRO_1_FS_ADDR,
  123. .mask = ST_GYRO_1_FS_MASK,
  124. .fs_avl = {
  125. [0] = {
  126. .num = ST_GYRO_FS_AVL_250DPS,
  127. .value = ST_GYRO_1_FS_AVL_250_VAL,
  128. .gain = ST_GYRO_1_FS_AVL_250_GAIN,
  129. },
  130. [1] = {
  131. .num = ST_GYRO_FS_AVL_500DPS,
  132. .value = ST_GYRO_1_FS_AVL_500_VAL,
  133. .gain = ST_GYRO_1_FS_AVL_500_GAIN,
  134. },
  135. [2] = {
  136. .num = ST_GYRO_FS_AVL_2000DPS,
  137. .value = ST_GYRO_1_FS_AVL_2000_VAL,
  138. .gain = ST_GYRO_1_FS_AVL_2000_GAIN,
  139. },
  140. },
  141. },
  142. .bdu = {
  143. .addr = ST_GYRO_1_BDU_ADDR,
  144. .mask = ST_GYRO_1_BDU_MASK,
  145. },
  146. .drdy_irq = {
  147. .addr = ST_GYRO_1_DRDY_IRQ_ADDR,
  148. .mask = ST_GYRO_1_DRDY_IRQ_MASK,
  149. },
  150. .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT,
  151. .bootime = 2,
  152. },
  153. {
  154. .wai = ST_GYRO_2_WAI_EXP,
  155. .sensors_supported = {
  156. [0] = L3GD20_GYRO_DEV_NAME,
  157. [1] = L3GD20H_GYRO_DEV_NAME,
  158. [2] = LSM330D_GYRO_DEV_NAME,
  159. [3] = LSM330DLC_GYRO_DEV_NAME,
  160. [4] = L3G4IS_GYRO_DEV_NAME,
  161. [5] = LSM330_GYRO_DEV_NAME,
  162. },
  163. .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
  164. .odr = {
  165. .addr = ST_GYRO_2_ODR_ADDR,
  166. .mask = ST_GYRO_2_ODR_MASK,
  167. .odr_avl = {
  168. { 95, ST_GYRO_2_ODR_AVL_95HZ_VAL, },
  169. { 190, ST_GYRO_2_ODR_AVL_190HZ_VAL, },
  170. { 380, ST_GYRO_2_ODR_AVL_380HZ_VAL, },
  171. { 760, ST_GYRO_2_ODR_AVL_760HZ_VAL, },
  172. },
  173. },
  174. .pw = {
  175. .addr = ST_GYRO_2_PW_ADDR,
  176. .mask = ST_GYRO_2_PW_MASK,
  177. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  178. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  179. },
  180. .enable_axis = {
  181. .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
  182. .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
  183. },
  184. .fs = {
  185. .addr = ST_GYRO_2_FS_ADDR,
  186. .mask = ST_GYRO_2_FS_MASK,
  187. .fs_avl = {
  188. [0] = {
  189. .num = ST_GYRO_FS_AVL_250DPS,
  190. .value = ST_GYRO_2_FS_AVL_250_VAL,
  191. .gain = ST_GYRO_2_FS_AVL_250_GAIN,
  192. },
  193. [1] = {
  194. .num = ST_GYRO_FS_AVL_500DPS,
  195. .value = ST_GYRO_2_FS_AVL_500_VAL,
  196. .gain = ST_GYRO_2_FS_AVL_500_GAIN,
  197. },
  198. [2] = {
  199. .num = ST_GYRO_FS_AVL_2000DPS,
  200. .value = ST_GYRO_2_FS_AVL_2000_VAL,
  201. .gain = ST_GYRO_2_FS_AVL_2000_GAIN,
  202. },
  203. },
  204. },
  205. .bdu = {
  206. .addr = ST_GYRO_2_BDU_ADDR,
  207. .mask = ST_GYRO_2_BDU_MASK,
  208. },
  209. .drdy_irq = {
  210. .addr = ST_GYRO_2_DRDY_IRQ_ADDR,
  211. .mask = ST_GYRO_2_DRDY_IRQ_MASK,
  212. },
  213. .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
  214. .bootime = 2,
  215. },
  216. };
  217. static int st_gyro_read_raw(struct iio_dev *indio_dev,
  218. struct iio_chan_spec const *ch, int *val,
  219. int *val2, long mask)
  220. {
  221. int err;
  222. struct st_sensor_data *gdata = iio_priv(indio_dev);
  223. switch (mask) {
  224. case IIO_CHAN_INFO_RAW:
  225. err = st_sensors_read_info_raw(indio_dev, ch, val);
  226. if (err < 0)
  227. goto read_error;
  228. return IIO_VAL_INT;
  229. case IIO_CHAN_INFO_SCALE:
  230. *val = 0;
  231. *val2 = gdata->current_fullscale->gain;
  232. return IIO_VAL_INT_PLUS_MICRO;
  233. default:
  234. return -EINVAL;
  235. }
  236. read_error:
  237. return err;
  238. }
  239. static int st_gyro_write_raw(struct iio_dev *indio_dev,
  240. struct iio_chan_spec const *chan, int val, int val2, long mask)
  241. {
  242. int err;
  243. switch (mask) {
  244. case IIO_CHAN_INFO_SCALE:
  245. err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
  246. break;
  247. default:
  248. err = -EINVAL;
  249. }
  250. return err;
  251. }
  252. static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
  253. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  254. static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
  255. static struct attribute *st_gyro_attributes[] = {
  256. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  257. &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
  258. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  259. NULL,
  260. };
  261. static const struct attribute_group st_gyro_attribute_group = {
  262. .attrs = st_gyro_attributes,
  263. };
  264. static const struct iio_info gyro_info = {
  265. .driver_module = THIS_MODULE,
  266. .attrs = &st_gyro_attribute_group,
  267. .read_raw = &st_gyro_read_raw,
  268. .write_raw = &st_gyro_write_raw,
  269. };
  270. #ifdef CONFIG_IIO_TRIGGER
  271. static const struct iio_trigger_ops st_gyro_trigger_ops = {
  272. .owner = THIS_MODULE,
  273. .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
  274. };
  275. #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
  276. #else
  277. #define ST_GYRO_TRIGGER_OPS NULL
  278. #endif
  279. int st_gyro_common_probe(struct iio_dev *indio_dev)
  280. {
  281. int err;
  282. struct st_sensor_data *gdata = iio_priv(indio_dev);
  283. indio_dev->modes = INDIO_DIRECT_MODE;
  284. indio_dev->info = &gyro_info;
  285. err = st_sensors_check_device_support(indio_dev,
  286. ARRAY_SIZE(st_gyro_sensors), st_gyro_sensors);
  287. if (err < 0)
  288. goto st_gyro_common_probe_error;
  289. gdata->multiread_bit = gdata->sensor->multi_read_bit;
  290. indio_dev->channels = gdata->sensor->ch;
  291. indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  292. gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  293. &gdata->sensor->fs.fs_avl[0];
  294. gdata->odr = gdata->sensor->odr.odr_avl[0].hz;
  295. err = st_sensors_init_sensor(indio_dev);
  296. if (err < 0)
  297. goto st_gyro_common_probe_error;
  298. if (gdata->get_irq_data_ready(indio_dev) > 0) {
  299. err = st_gyro_allocate_ring(indio_dev);
  300. if (err < 0)
  301. goto st_gyro_common_probe_error;
  302. err = st_sensors_allocate_trigger(indio_dev,
  303. ST_GYRO_TRIGGER_OPS);
  304. if (err < 0)
  305. goto st_gyro_probe_trigger_error;
  306. }
  307. err = iio_device_register(indio_dev);
  308. if (err)
  309. goto st_gyro_device_register_error;
  310. return err;
  311. st_gyro_device_register_error:
  312. if (gdata->get_irq_data_ready(indio_dev) > 0)
  313. st_sensors_deallocate_trigger(indio_dev);
  314. st_gyro_probe_trigger_error:
  315. if (gdata->get_irq_data_ready(indio_dev) > 0)
  316. st_gyro_deallocate_ring(indio_dev);
  317. st_gyro_common_probe_error:
  318. return err;
  319. }
  320. EXPORT_SYMBOL(st_gyro_common_probe);
  321. void st_gyro_common_remove(struct iio_dev *indio_dev)
  322. {
  323. struct st_sensor_data *gdata = iio_priv(indio_dev);
  324. iio_device_unregister(indio_dev);
  325. if (gdata->get_irq_data_ready(indio_dev) > 0) {
  326. st_sensors_deallocate_trigger(indio_dev);
  327. st_gyro_deallocate_ring(indio_dev);
  328. }
  329. iio_device_free(indio_dev);
  330. }
  331. EXPORT_SYMBOL(st_gyro_common_remove);
  332. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  333. MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
  334. MODULE_LICENSE("GPL v2");