st_gyro_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_245DPS 245
  34. #define ST_GYRO_FS_AVL_250DPS 250
  35. #define ST_GYRO_FS_AVL_500DPS 500
  36. #define ST_GYRO_FS_AVL_2000DPS 2000
  37. static const struct iio_chan_spec st_gyro_16bit_channels[] = {
  38. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
  39. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  40. ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
  41. ST_GYRO_DEFAULT_OUT_X_L_ADDR),
  42. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
  43. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  44. ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
  45. ST_GYRO_DEFAULT_OUT_Y_L_ADDR),
  46. ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
  47. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  48. ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
  49. ST_GYRO_DEFAULT_OUT_Z_L_ADDR),
  50. IIO_CHAN_SOFT_TIMESTAMP(3)
  51. };
  52. static const struct st_sensor_settings st_gyro_sensors_settings[] = {
  53. {
  54. .wai = 0xd3,
  55. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  56. .sensors_supported = {
  57. [0] = L3G4200D_GYRO_DEV_NAME,
  58. [1] = LSM330DL_GYRO_DEV_NAME,
  59. },
  60. .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
  61. .odr = {
  62. .addr = 0x20,
  63. .mask = 0xc0,
  64. .odr_avl = {
  65. { .hz = 100, .value = 0x00, },
  66. { .hz = 200, .value = 0x01, },
  67. { .hz = 400, .value = 0x02, },
  68. { .hz = 800, .value = 0x03, },
  69. },
  70. },
  71. .pw = {
  72. .addr = 0x20,
  73. .mask = 0x08,
  74. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  75. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  76. },
  77. .enable_axis = {
  78. .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
  79. .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
  80. },
  81. .fs = {
  82. .addr = 0x23,
  83. .mask = 0x30,
  84. .fs_avl = {
  85. [0] = {
  86. .num = ST_GYRO_FS_AVL_250DPS,
  87. .value = 0x00,
  88. .gain = IIO_DEGREE_TO_RAD(8750),
  89. },
  90. [1] = {
  91. .num = ST_GYRO_FS_AVL_500DPS,
  92. .value = 0x01,
  93. .gain = IIO_DEGREE_TO_RAD(17500),
  94. },
  95. [2] = {
  96. .num = ST_GYRO_FS_AVL_2000DPS,
  97. .value = 0x02,
  98. .gain = IIO_DEGREE_TO_RAD(70000),
  99. },
  100. },
  101. },
  102. .bdu = {
  103. .addr = 0x23,
  104. .mask = 0x80,
  105. },
  106. .drdy_irq = {
  107. .addr = 0x22,
  108. .mask_int2 = 0x08,
  109. /*
  110. * The sensor has IHL (active low) and open
  111. * drain settings, but only for INT1 and not
  112. * for the DRDY line on INT2.
  113. */
  114. .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
  115. },
  116. .multi_read_bit = true,
  117. .bootime = 2,
  118. },
  119. {
  120. .wai = 0xd4,
  121. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  122. .sensors_supported = {
  123. [0] = L3GD20_GYRO_DEV_NAME,
  124. [1] = LSM330D_GYRO_DEV_NAME,
  125. [2] = LSM330DLC_GYRO_DEV_NAME,
  126. [3] = L3G4IS_GYRO_DEV_NAME,
  127. [4] = LSM330_GYRO_DEV_NAME,
  128. [5] = LSM9DS0_GYRO_DEV_NAME,
  129. },
  130. .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
  131. .odr = {
  132. .addr = 0x20,
  133. .mask = 0xc0,
  134. .odr_avl = {
  135. { .hz = 95, .value = 0x00, },
  136. { .hz = 190, .value = 0x01, },
  137. { .hz = 380, .value = 0x02, },
  138. { .hz = 760, .value = 0x03, },
  139. },
  140. },
  141. .pw = {
  142. .addr = 0x20,
  143. .mask = 0x08,
  144. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  145. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  146. },
  147. .enable_axis = {
  148. .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
  149. .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
  150. },
  151. .fs = {
  152. .addr = 0x23,
  153. .mask = 0x30,
  154. .fs_avl = {
  155. [0] = {
  156. .num = ST_GYRO_FS_AVL_250DPS,
  157. .value = 0x00,
  158. .gain = IIO_DEGREE_TO_RAD(8750),
  159. },
  160. [1] = {
  161. .num = ST_GYRO_FS_AVL_500DPS,
  162. .value = 0x01,
  163. .gain = IIO_DEGREE_TO_RAD(17500),
  164. },
  165. [2] = {
  166. .num = ST_GYRO_FS_AVL_2000DPS,
  167. .value = 0x02,
  168. .gain = IIO_DEGREE_TO_RAD(70000),
  169. },
  170. },
  171. },
  172. .bdu = {
  173. .addr = 0x23,
  174. .mask = 0x80,
  175. },
  176. .drdy_irq = {
  177. .addr = 0x22,
  178. .mask_int2 = 0x08,
  179. /*
  180. * The sensor has IHL (active low) and open
  181. * drain settings, but only for INT1 and not
  182. * for the DRDY line on INT2.
  183. */
  184. .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
  185. },
  186. .multi_read_bit = true,
  187. .bootime = 2,
  188. },
  189. {
  190. .wai = 0xd7,
  191. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  192. .sensors_supported = {
  193. [0] = L3GD20H_GYRO_DEV_NAME,
  194. },
  195. .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
  196. .odr = {
  197. .addr = 0x20,
  198. .mask = 0xc0,
  199. .odr_avl = {
  200. { .hz = 100, .value = 0x00, },
  201. { .hz = 200, .value = 0x01, },
  202. { .hz = 400, .value = 0x02, },
  203. { .hz = 800, .value = 0x03, },
  204. },
  205. },
  206. .pw = {
  207. .addr = 0x20,
  208. .mask = 0x08,
  209. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  210. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  211. },
  212. .enable_axis = {
  213. .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
  214. .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
  215. },
  216. .fs = {
  217. .addr = 0x23,
  218. .mask = 0x30,
  219. .fs_avl = {
  220. [0] = {
  221. .num = ST_GYRO_FS_AVL_245DPS,
  222. .value = 0x00,
  223. .gain = IIO_DEGREE_TO_RAD(8750),
  224. },
  225. [1] = {
  226. .num = ST_GYRO_FS_AVL_500DPS,
  227. .value = 0x01,
  228. .gain = IIO_DEGREE_TO_RAD(17500),
  229. },
  230. [2] = {
  231. .num = ST_GYRO_FS_AVL_2000DPS,
  232. .value = 0x02,
  233. .gain = IIO_DEGREE_TO_RAD(70000),
  234. },
  235. },
  236. },
  237. .bdu = {
  238. .addr = 0x23,
  239. .mask = 0x80,
  240. },
  241. .drdy_irq = {
  242. .addr = 0x22,
  243. .mask_int2 = 0x08,
  244. /*
  245. * The sensor has IHL (active low) and open
  246. * drain settings, but only for INT1 and not
  247. * for the DRDY line on INT2.
  248. */
  249. .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
  250. },
  251. .multi_read_bit = true,
  252. .bootime = 2,
  253. },
  254. };
  255. static int st_gyro_read_raw(struct iio_dev *indio_dev,
  256. struct iio_chan_spec const *ch, int *val,
  257. int *val2, long mask)
  258. {
  259. int err;
  260. struct st_sensor_data *gdata = iio_priv(indio_dev);
  261. switch (mask) {
  262. case IIO_CHAN_INFO_RAW:
  263. err = st_sensors_read_info_raw(indio_dev, ch, val);
  264. if (err < 0)
  265. goto read_error;
  266. return IIO_VAL_INT;
  267. case IIO_CHAN_INFO_SCALE:
  268. *val = 0;
  269. *val2 = gdata->current_fullscale->gain;
  270. return IIO_VAL_INT_PLUS_MICRO;
  271. case IIO_CHAN_INFO_SAMP_FREQ:
  272. *val = gdata->odr;
  273. return IIO_VAL_INT;
  274. default:
  275. return -EINVAL;
  276. }
  277. read_error:
  278. return err;
  279. }
  280. static int st_gyro_write_raw(struct iio_dev *indio_dev,
  281. struct iio_chan_spec const *chan, int val, int val2, long mask)
  282. {
  283. int err;
  284. switch (mask) {
  285. case IIO_CHAN_INFO_SCALE:
  286. err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
  287. break;
  288. case IIO_CHAN_INFO_SAMP_FREQ:
  289. if (val2)
  290. return -EINVAL;
  291. mutex_lock(&indio_dev->mlock);
  292. err = st_sensors_set_odr(indio_dev, val);
  293. mutex_unlock(&indio_dev->mlock);
  294. return err;
  295. default:
  296. err = -EINVAL;
  297. }
  298. return err;
  299. }
  300. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  301. static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
  302. static struct attribute *st_gyro_attributes[] = {
  303. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  304. &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
  305. NULL,
  306. };
  307. static const struct attribute_group st_gyro_attribute_group = {
  308. .attrs = st_gyro_attributes,
  309. };
  310. static const struct iio_info gyro_info = {
  311. .driver_module = THIS_MODULE,
  312. .attrs = &st_gyro_attribute_group,
  313. .read_raw = &st_gyro_read_raw,
  314. .write_raw = &st_gyro_write_raw,
  315. .debugfs_reg_access = &st_sensors_debugfs_reg_access,
  316. };
  317. #ifdef CONFIG_IIO_TRIGGER
  318. static const struct iio_trigger_ops st_gyro_trigger_ops = {
  319. .owner = THIS_MODULE,
  320. .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
  321. .validate_device = st_sensors_validate_device,
  322. };
  323. #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
  324. #else
  325. #define ST_GYRO_TRIGGER_OPS NULL
  326. #endif
  327. int st_gyro_common_probe(struct iio_dev *indio_dev)
  328. {
  329. struct st_sensor_data *gdata = iio_priv(indio_dev);
  330. int irq = gdata->get_irq_data_ready(indio_dev);
  331. int err;
  332. indio_dev->modes = INDIO_DIRECT_MODE;
  333. indio_dev->info = &gyro_info;
  334. mutex_init(&gdata->tb.buf_lock);
  335. err = st_sensors_power_enable(indio_dev);
  336. if (err)
  337. return err;
  338. err = st_sensors_check_device_support(indio_dev,
  339. ARRAY_SIZE(st_gyro_sensors_settings),
  340. st_gyro_sensors_settings);
  341. if (err < 0)
  342. goto st_gyro_power_off;
  343. gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS;
  344. gdata->multiread_bit = gdata->sensor_settings->multi_read_bit;
  345. indio_dev->channels = gdata->sensor_settings->ch;
  346. indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
  347. gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  348. &gdata->sensor_settings->fs.fs_avl[0];
  349. gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz;
  350. err = st_sensors_init_sensor(indio_dev,
  351. (struct st_sensors_platform_data *)&gyro_pdata);
  352. if (err < 0)
  353. goto st_gyro_power_off;
  354. err = st_gyro_allocate_ring(indio_dev);
  355. if (err < 0)
  356. goto st_gyro_power_off;
  357. if (irq > 0) {
  358. err = st_sensors_allocate_trigger(indio_dev,
  359. ST_GYRO_TRIGGER_OPS);
  360. if (err < 0)
  361. goto st_gyro_probe_trigger_error;
  362. }
  363. err = iio_device_register(indio_dev);
  364. if (err)
  365. goto st_gyro_device_register_error;
  366. dev_info(&indio_dev->dev, "registered gyroscope %s\n",
  367. indio_dev->name);
  368. return 0;
  369. st_gyro_device_register_error:
  370. if (irq > 0)
  371. st_sensors_deallocate_trigger(indio_dev);
  372. st_gyro_probe_trigger_error:
  373. st_gyro_deallocate_ring(indio_dev);
  374. st_gyro_power_off:
  375. st_sensors_power_disable(indio_dev);
  376. return err;
  377. }
  378. EXPORT_SYMBOL(st_gyro_common_probe);
  379. void st_gyro_common_remove(struct iio_dev *indio_dev)
  380. {
  381. struct st_sensor_data *gdata = iio_priv(indio_dev);
  382. st_sensors_power_disable(indio_dev);
  383. iio_device_unregister(indio_dev);
  384. if (gdata->get_irq_data_ready(indio_dev) > 0)
  385. st_sensors_deallocate_trigger(indio_dev);
  386. st_gyro_deallocate_ring(indio_dev);
  387. }
  388. EXPORT_SYMBOL(st_gyro_common_remove);
  389. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  390. MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
  391. MODULE_LICENSE("GPL v2");