st_gyro_core.c 10 KB

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