st_gyro_core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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,
  83. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  84. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
  85. ST_GYRO_DEFAULT_OUT_X_L_ADDR),
  86. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
  87. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  88. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
  89. ST_GYRO_DEFAULT_OUT_Y_L_ADDR),
  90. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
  91. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  92. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
  93. ST_GYRO_DEFAULT_OUT_Z_L_ADDR),
  94. IIO_CHAN_SOFT_TIMESTAMP(3)
  95. };
  96. static const struct st_sensors st_gyro_sensors[] = {
  97. {
  98. .wai = ST_GYRO_1_WAI_EXP,
  99. .sensors_supported = {
  100. [0] = L3G4200D_GYRO_DEV_NAME,
  101. [1] = LSM330DL_GYRO_DEV_NAME,
  102. },
  103. .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
  104. .odr = {
  105. .addr = ST_GYRO_1_ODR_ADDR,
  106. .mask = ST_GYRO_1_ODR_MASK,
  107. .odr_avl = {
  108. { 100, ST_GYRO_1_ODR_AVL_100HZ_VAL, },
  109. { 200, ST_GYRO_1_ODR_AVL_200HZ_VAL, },
  110. { 400, ST_GYRO_1_ODR_AVL_400HZ_VAL, },
  111. { 800, ST_GYRO_1_ODR_AVL_800HZ_VAL, },
  112. },
  113. },
  114. .pw = {
  115. .addr = ST_GYRO_1_PW_ADDR,
  116. .mask = ST_GYRO_1_PW_MASK,
  117. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  118. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  119. },
  120. .enable_axis = {
  121. .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
  122. .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
  123. },
  124. .fs = {
  125. .addr = ST_GYRO_1_FS_ADDR,
  126. .mask = ST_GYRO_1_FS_MASK,
  127. .fs_avl = {
  128. [0] = {
  129. .num = ST_GYRO_FS_AVL_250DPS,
  130. .value = ST_GYRO_1_FS_AVL_250_VAL,
  131. .gain = ST_GYRO_1_FS_AVL_250_GAIN,
  132. },
  133. [1] = {
  134. .num = ST_GYRO_FS_AVL_500DPS,
  135. .value = ST_GYRO_1_FS_AVL_500_VAL,
  136. .gain = ST_GYRO_1_FS_AVL_500_GAIN,
  137. },
  138. [2] = {
  139. .num = ST_GYRO_FS_AVL_2000DPS,
  140. .value = ST_GYRO_1_FS_AVL_2000_VAL,
  141. .gain = ST_GYRO_1_FS_AVL_2000_GAIN,
  142. },
  143. },
  144. },
  145. .bdu = {
  146. .addr = ST_GYRO_1_BDU_ADDR,
  147. .mask = ST_GYRO_1_BDU_MASK,
  148. },
  149. .drdy_irq = {
  150. .addr = ST_GYRO_1_DRDY_IRQ_ADDR,
  151. .mask = ST_GYRO_1_DRDY_IRQ_MASK,
  152. },
  153. .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT,
  154. .bootime = 2,
  155. },
  156. {
  157. .wai = ST_GYRO_2_WAI_EXP,
  158. .sensors_supported = {
  159. [0] = L3GD20_GYRO_DEV_NAME,
  160. [1] = L3GD20H_GYRO_DEV_NAME,
  161. [2] = LSM330D_GYRO_DEV_NAME,
  162. [3] = LSM330DLC_GYRO_DEV_NAME,
  163. [4] = L3G4IS_GYRO_DEV_NAME,
  164. [5] = LSM330_GYRO_DEV_NAME,
  165. },
  166. .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
  167. .odr = {
  168. .addr = ST_GYRO_2_ODR_ADDR,
  169. .mask = ST_GYRO_2_ODR_MASK,
  170. .odr_avl = {
  171. { 95, ST_GYRO_2_ODR_AVL_95HZ_VAL, },
  172. { 190, ST_GYRO_2_ODR_AVL_190HZ_VAL, },
  173. { 380, ST_GYRO_2_ODR_AVL_380HZ_VAL, },
  174. { 760, ST_GYRO_2_ODR_AVL_760HZ_VAL, },
  175. },
  176. },
  177. .pw = {
  178. .addr = ST_GYRO_2_PW_ADDR,
  179. .mask = ST_GYRO_2_PW_MASK,
  180. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  181. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  182. },
  183. .enable_axis = {
  184. .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
  185. .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
  186. },
  187. .fs = {
  188. .addr = ST_GYRO_2_FS_ADDR,
  189. .mask = ST_GYRO_2_FS_MASK,
  190. .fs_avl = {
  191. [0] = {
  192. .num = ST_GYRO_FS_AVL_250DPS,
  193. .value = ST_GYRO_2_FS_AVL_250_VAL,
  194. .gain = ST_GYRO_2_FS_AVL_250_GAIN,
  195. },
  196. [1] = {
  197. .num = ST_GYRO_FS_AVL_500DPS,
  198. .value = ST_GYRO_2_FS_AVL_500_VAL,
  199. .gain = ST_GYRO_2_FS_AVL_500_GAIN,
  200. },
  201. [2] = {
  202. .num = ST_GYRO_FS_AVL_2000DPS,
  203. .value = ST_GYRO_2_FS_AVL_2000_VAL,
  204. .gain = ST_GYRO_2_FS_AVL_2000_GAIN,
  205. },
  206. },
  207. },
  208. .bdu = {
  209. .addr = ST_GYRO_2_BDU_ADDR,
  210. .mask = ST_GYRO_2_BDU_MASK,
  211. },
  212. .drdy_irq = {
  213. .addr = ST_GYRO_2_DRDY_IRQ_ADDR,
  214. .mask = ST_GYRO_2_DRDY_IRQ_MASK,
  215. },
  216. .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
  217. .bootime = 2,
  218. },
  219. };
  220. static int st_gyro_read_raw(struct iio_dev *indio_dev,
  221. struct iio_chan_spec const *ch, int *val,
  222. int *val2, long mask)
  223. {
  224. int err;
  225. struct st_sensor_data *gdata = iio_priv(indio_dev);
  226. switch (mask) {
  227. case IIO_CHAN_INFO_RAW:
  228. err = st_sensors_read_info_raw(indio_dev, ch, val);
  229. if (err < 0)
  230. goto read_error;
  231. return IIO_VAL_INT;
  232. case IIO_CHAN_INFO_SCALE:
  233. *val = 0;
  234. *val2 = gdata->current_fullscale->gain;
  235. return IIO_VAL_INT_PLUS_MICRO;
  236. default:
  237. return -EINVAL;
  238. }
  239. read_error:
  240. return err;
  241. }
  242. static int st_gyro_write_raw(struct iio_dev *indio_dev,
  243. struct iio_chan_spec const *chan, int val, int val2, long mask)
  244. {
  245. int err;
  246. switch (mask) {
  247. case IIO_CHAN_INFO_SCALE:
  248. err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
  249. break;
  250. default:
  251. err = -EINVAL;
  252. }
  253. return err;
  254. }
  255. static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
  256. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  257. static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
  258. static struct attribute *st_gyro_attributes[] = {
  259. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  260. &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
  261. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  262. NULL,
  263. };
  264. static const struct attribute_group st_gyro_attribute_group = {
  265. .attrs = st_gyro_attributes,
  266. };
  267. static const struct iio_info gyro_info = {
  268. .driver_module = THIS_MODULE,
  269. .attrs = &st_gyro_attribute_group,
  270. .read_raw = &st_gyro_read_raw,
  271. .write_raw = &st_gyro_write_raw,
  272. };
  273. #ifdef CONFIG_IIO_TRIGGER
  274. static const struct iio_trigger_ops st_gyro_trigger_ops = {
  275. .owner = THIS_MODULE,
  276. .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
  277. };
  278. #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
  279. #else
  280. #define ST_GYRO_TRIGGER_OPS NULL
  281. #endif
  282. int st_gyro_common_probe(struct iio_dev *indio_dev)
  283. {
  284. int err;
  285. struct st_sensor_data *gdata = iio_priv(indio_dev);
  286. indio_dev->modes = INDIO_DIRECT_MODE;
  287. indio_dev->info = &gyro_info;
  288. err = st_sensors_check_device_support(indio_dev,
  289. ARRAY_SIZE(st_gyro_sensors), st_gyro_sensors);
  290. if (err < 0)
  291. goto st_gyro_common_probe_error;
  292. gdata->multiread_bit = gdata->sensor->multi_read_bit;
  293. indio_dev->channels = gdata->sensor->ch;
  294. indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  295. gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  296. &gdata->sensor->fs.fs_avl[0];
  297. gdata->odr = gdata->sensor->odr.odr_avl[0].hz;
  298. err = st_sensors_init_sensor(indio_dev);
  299. if (err < 0)
  300. goto st_gyro_common_probe_error;
  301. if (gdata->get_irq_data_ready(indio_dev) > 0) {
  302. err = st_gyro_allocate_ring(indio_dev);
  303. if (err < 0)
  304. goto st_gyro_common_probe_error;
  305. err = st_sensors_allocate_trigger(indio_dev,
  306. ST_GYRO_TRIGGER_OPS);
  307. if (err < 0)
  308. goto st_gyro_probe_trigger_error;
  309. }
  310. err = iio_device_register(indio_dev);
  311. if (err)
  312. goto st_gyro_device_register_error;
  313. return err;
  314. st_gyro_device_register_error:
  315. if (gdata->get_irq_data_ready(indio_dev) > 0)
  316. st_sensors_deallocate_trigger(indio_dev);
  317. st_gyro_probe_trigger_error:
  318. if (gdata->get_irq_data_ready(indio_dev) > 0)
  319. st_gyro_deallocate_ring(indio_dev);
  320. st_gyro_common_probe_error:
  321. return err;
  322. }
  323. EXPORT_SYMBOL(st_gyro_common_probe);
  324. void st_gyro_common_remove(struct iio_dev *indio_dev)
  325. {
  326. struct st_sensor_data *gdata = iio_priv(indio_dev);
  327. iio_device_unregister(indio_dev);
  328. if (gdata->get_irq_data_ready(indio_dev) > 0) {
  329. st_sensors_deallocate_trigger(indio_dev);
  330. st_gyro_deallocate_ring(indio_dev);
  331. }
  332. iio_device_free(indio_dev);
  333. }
  334. EXPORT_SYMBOL(st_gyro_common_remove);
  335. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  336. MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
  337. MODULE_LICENSE("GPL v2");