itg3200_core.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * itg3200_core.c -- support InvenSense ITG3200
  3. * Digital 3-Axis Gyroscope driver
  4. *
  5. * Copyright (c) 2011 Christian Strobel <christian.strobel@iis.fraunhofer.de>
  6. * Copyright (c) 2011 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
  7. * Copyright (c) 2012 Thorsten Nowak <thorsten.nowak@iis.fraunhofer.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * TODO:
  14. * - Support digital low pass filter
  15. * - Support power management
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/i2c.h>
  20. #include <linux/gpio.h>
  21. #include <linux/slab.h>
  22. #include <linux/stat.h>
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/events.h>
  28. #include <linux/iio/buffer.h>
  29. #include <linux/iio/gyro/itg3200.h>
  30. int itg3200_write_reg_8(struct iio_dev *indio_dev,
  31. u8 reg_address, u8 val)
  32. {
  33. struct itg3200 *st = iio_priv(indio_dev);
  34. return i2c_smbus_write_byte_data(st->i2c, 0x80 | reg_address, val);
  35. }
  36. int itg3200_read_reg_8(struct iio_dev *indio_dev,
  37. u8 reg_address, u8 *val)
  38. {
  39. struct itg3200 *st = iio_priv(indio_dev);
  40. int ret;
  41. ret = i2c_smbus_read_byte_data(st->i2c, reg_address);
  42. if (ret < 0)
  43. return ret;
  44. *val = ret;
  45. return 0;
  46. }
  47. static int itg3200_read_reg_s16(struct iio_dev *indio_dev, u8 lower_reg_address,
  48. int *val)
  49. {
  50. struct itg3200 *st = iio_priv(indio_dev);
  51. struct i2c_client *client = st->i2c;
  52. int ret;
  53. s16 out;
  54. struct i2c_msg msg[2] = {
  55. {
  56. .addr = client->addr,
  57. .flags = client->flags,
  58. .len = 1,
  59. .buf = (char *)&lower_reg_address,
  60. },
  61. {
  62. .addr = client->addr,
  63. .flags = client->flags | I2C_M_RD,
  64. .len = 2,
  65. .buf = (char *)&out,
  66. },
  67. };
  68. lower_reg_address |= 0x80;
  69. ret = i2c_transfer(client->adapter, msg, 2);
  70. be16_to_cpus(&out);
  71. *val = out;
  72. return (ret == 2) ? 0 : ret;
  73. }
  74. static int itg3200_read_raw(struct iio_dev *indio_dev,
  75. const struct iio_chan_spec *chan,
  76. int *val, int *val2, long info)
  77. {
  78. int ret = 0;
  79. u8 reg;
  80. u8 regval;
  81. switch (info) {
  82. case IIO_CHAN_INFO_RAW:
  83. reg = (u8)chan->address;
  84. ret = itg3200_read_reg_s16(indio_dev, reg, val);
  85. return IIO_VAL_INT;
  86. case IIO_CHAN_INFO_SCALE:
  87. *val = 0;
  88. if (chan->type == IIO_TEMP)
  89. *val2 = 1000000000/280;
  90. else
  91. *val2 = 1214142; /* (1 / 14,375) * (PI / 180) */
  92. return IIO_VAL_INT_PLUS_NANO;
  93. case IIO_CHAN_INFO_OFFSET:
  94. /* Only the temperature channel has an offset */
  95. *val = 23000;
  96. return IIO_VAL_INT;
  97. case IIO_CHAN_INFO_SAMP_FREQ:
  98. ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &regval);
  99. if (ret)
  100. return ret;
  101. *val = (regval & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
  102. ret = itg3200_read_reg_8(indio_dev,
  103. ITG3200_REG_SAMPLE_RATE_DIV,
  104. &regval);
  105. if (ret)
  106. return ret;
  107. *val /= regval + 1;
  108. return IIO_VAL_INT;
  109. default:
  110. return -EINVAL;
  111. }
  112. }
  113. static int itg3200_write_raw(struct iio_dev *indio_dev,
  114. struct iio_chan_spec const *chan,
  115. int val,
  116. int val2,
  117. long mask)
  118. {
  119. int ret;
  120. u8 t;
  121. switch (mask) {
  122. case IIO_CHAN_INFO_SAMP_FREQ:
  123. if (val == 0 || val2 != 0)
  124. return -EINVAL;
  125. mutex_lock(&indio_dev->mlock);
  126. ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
  127. if (ret) {
  128. mutex_unlock(&indio_dev->mlock);
  129. return ret;
  130. }
  131. t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
  132. ret = itg3200_write_reg_8(indio_dev,
  133. ITG3200_REG_SAMPLE_RATE_DIV,
  134. t);
  135. mutex_unlock(&indio_dev->mlock);
  136. return ret;
  137. default:
  138. return -EINVAL;
  139. }
  140. }
  141. /*
  142. * Reset device and internal registers to the power-up-default settings
  143. * Use the gyro clock as reference, as suggested by the datasheet
  144. */
  145. static int itg3200_reset(struct iio_dev *indio_dev)
  146. {
  147. struct itg3200 *st = iio_priv(indio_dev);
  148. int ret;
  149. dev_dbg(&st->i2c->dev, "reset device");
  150. ret = itg3200_write_reg_8(indio_dev,
  151. ITG3200_REG_POWER_MANAGEMENT,
  152. ITG3200_RESET);
  153. if (ret) {
  154. dev_err(&st->i2c->dev, "error resetting device");
  155. goto error_ret;
  156. }
  157. /* Wait for PLL (1ms according to datasheet) */
  158. udelay(1500);
  159. ret = itg3200_write_reg_8(indio_dev,
  160. ITG3200_REG_IRQ_CONFIG,
  161. ITG3200_IRQ_ACTIVE_HIGH |
  162. ITG3200_IRQ_PUSH_PULL |
  163. ITG3200_IRQ_LATCH_50US_PULSE |
  164. ITG3200_IRQ_LATCH_CLEAR_ANY);
  165. if (ret)
  166. dev_err(&st->i2c->dev, "error init device");
  167. error_ret:
  168. return ret;
  169. }
  170. /* itg3200_enable_full_scale() - Disables the digital low pass filter */
  171. static int itg3200_enable_full_scale(struct iio_dev *indio_dev)
  172. {
  173. u8 val;
  174. int ret;
  175. ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
  176. if (ret)
  177. goto err_ret;
  178. val |= ITG3200_DLPF_FS_SEL_2000;
  179. return itg3200_write_reg_8(indio_dev, ITG3200_REG_DLPF, val);
  180. err_ret:
  181. return ret;
  182. }
  183. static int itg3200_initial_setup(struct iio_dev *indio_dev)
  184. {
  185. struct itg3200 *st = iio_priv(indio_dev);
  186. int ret;
  187. u8 val;
  188. ret = itg3200_reset(indio_dev);
  189. if (ret)
  190. goto err_ret;
  191. ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
  192. if (ret)
  193. goto err_ret;
  194. if (((val >> 1) & 0x3f) != 0x34) {
  195. dev_err(&st->i2c->dev, "invalid reg value 0x%02x", val);
  196. ret = -ENXIO;
  197. goto err_ret;
  198. }
  199. ret = itg3200_enable_full_scale(indio_dev);
  200. err_ret:
  201. return ret;
  202. }
  203. #define ITG3200_ST \
  204. { .sign = 's', .realbits = 16, .storagebits = 16, .endianness = IIO_BE }
  205. #define ITG3200_GYRO_CHAN(_mod) { \
  206. .type = IIO_ANGL_VEL, \
  207. .modified = 1, \
  208. .channel2 = IIO_MOD_ ## _mod, \
  209. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  210. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  211. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  212. .address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
  213. .scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
  214. .scan_type = ITG3200_ST, \
  215. }
  216. static const struct iio_chan_spec itg3200_channels[] = {
  217. {
  218. .type = IIO_TEMP,
  219. .channel2 = IIO_NO_MOD,
  220. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  221. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  222. BIT(IIO_CHAN_INFO_SCALE),
  223. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  224. .address = ITG3200_REG_TEMP_OUT_H,
  225. .scan_index = ITG3200_SCAN_TEMP,
  226. .scan_type = ITG3200_ST,
  227. },
  228. ITG3200_GYRO_CHAN(X),
  229. ITG3200_GYRO_CHAN(Y),
  230. ITG3200_GYRO_CHAN(Z),
  231. IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
  232. };
  233. static const struct iio_info itg3200_info = {
  234. .read_raw = &itg3200_read_raw,
  235. .write_raw = &itg3200_write_raw,
  236. .driver_module = THIS_MODULE,
  237. };
  238. static const unsigned long itg3200_available_scan_masks[] = { 0xffffffff, 0x0 };
  239. static int itg3200_probe(struct i2c_client *client,
  240. const struct i2c_device_id *id)
  241. {
  242. int ret;
  243. struct itg3200 *st;
  244. struct iio_dev *indio_dev;
  245. dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
  246. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
  247. if (!indio_dev)
  248. return -ENOMEM;
  249. st = iio_priv(indio_dev);
  250. i2c_set_clientdata(client, indio_dev);
  251. st->i2c = client;
  252. indio_dev->dev.parent = &client->dev;
  253. indio_dev->name = client->dev.driver->name;
  254. indio_dev->channels = itg3200_channels;
  255. indio_dev->num_channels = ARRAY_SIZE(itg3200_channels);
  256. indio_dev->available_scan_masks = itg3200_available_scan_masks;
  257. indio_dev->info = &itg3200_info;
  258. indio_dev->modes = INDIO_DIRECT_MODE;
  259. ret = itg3200_buffer_configure(indio_dev);
  260. if (ret)
  261. return ret;
  262. if (client->irq) {
  263. ret = itg3200_probe_trigger(indio_dev);
  264. if (ret)
  265. goto error_unconfigure_buffer;
  266. }
  267. ret = itg3200_initial_setup(indio_dev);
  268. if (ret)
  269. goto error_remove_trigger;
  270. ret = iio_device_register(indio_dev);
  271. if (ret)
  272. goto error_remove_trigger;
  273. return 0;
  274. error_remove_trigger:
  275. if (client->irq)
  276. itg3200_remove_trigger(indio_dev);
  277. error_unconfigure_buffer:
  278. itg3200_buffer_unconfigure(indio_dev);
  279. return ret;
  280. }
  281. static int itg3200_remove(struct i2c_client *client)
  282. {
  283. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  284. iio_device_unregister(indio_dev);
  285. if (client->irq)
  286. itg3200_remove_trigger(indio_dev);
  287. itg3200_buffer_unconfigure(indio_dev);
  288. return 0;
  289. }
  290. static int __maybe_unused itg3200_suspend(struct device *dev)
  291. {
  292. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  293. struct itg3200 *st = iio_priv(indio_dev);
  294. dev_dbg(&st->i2c->dev, "suspend device");
  295. return itg3200_write_reg_8(indio_dev, ITG3200_REG_POWER_MANAGEMENT,
  296. ITG3200_SLEEP);
  297. }
  298. static int __maybe_unused itg3200_resume(struct device *dev)
  299. {
  300. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  301. return itg3200_initial_setup(indio_dev);
  302. }
  303. static SIMPLE_DEV_PM_OPS(itg3200_pm_ops, itg3200_suspend, itg3200_resume);
  304. static const struct i2c_device_id itg3200_id[] = {
  305. { "itg3200", 0 },
  306. { }
  307. };
  308. MODULE_DEVICE_TABLE(i2c, itg3200_id);
  309. static const struct of_device_id itg3200_of_match[] = {
  310. { .compatible = "invensense,itg3200" },
  311. { }
  312. };
  313. MODULE_DEVICE_TABLE(of, itg3200_of_match);
  314. static struct i2c_driver itg3200_driver = {
  315. .driver = {
  316. .name = "itg3200",
  317. .of_match_table = itg3200_of_match,
  318. .pm = &itg3200_pm_ops,
  319. },
  320. .id_table = itg3200_id,
  321. .probe = itg3200_probe,
  322. .remove = itg3200_remove,
  323. };
  324. module_i2c_driver(itg3200_driver);
  325. MODULE_AUTHOR("Christian Strobel <christian.strobel@iis.fraunhofer.de>");
  326. MODULE_DESCRIPTION("ITG3200 Gyroscope I2C driver");
  327. MODULE_LICENSE("GPL v2");