adis16136.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * ADIS16133/ADIS16135/ADIS16136 gyroscope driver
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/delay.h>
  11. #include <linux/mutex.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/module.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/buffer.h>
  21. #include <linux/iio/imu/adis.h>
  22. #include <linux/debugfs.h>
  23. #define ADIS16136_REG_FLASH_CNT 0x00
  24. #define ADIS16136_REG_TEMP_OUT 0x02
  25. #define ADIS16136_REG_GYRO_OUT2 0x04
  26. #define ADIS16136_REG_GYRO_OUT 0x06
  27. #define ADIS16136_REG_GYRO_OFF2 0x08
  28. #define ADIS16136_REG_GYRO_OFF 0x0A
  29. #define ADIS16136_REG_ALM_MAG1 0x10
  30. #define ADIS16136_REG_ALM_MAG2 0x12
  31. #define ADIS16136_REG_ALM_SAMPL1 0x14
  32. #define ADIS16136_REG_ALM_SAMPL2 0x16
  33. #define ADIS16136_REG_ALM_CTRL 0x18
  34. #define ADIS16136_REG_GPIO_CTRL 0x1A
  35. #define ADIS16136_REG_MSC_CTRL 0x1C
  36. #define ADIS16136_REG_SMPL_PRD 0x1E
  37. #define ADIS16136_REG_AVG_CNT 0x20
  38. #define ADIS16136_REG_DEC_RATE 0x22
  39. #define ADIS16136_REG_SLP_CTRL 0x24
  40. #define ADIS16136_REG_DIAG_STAT 0x26
  41. #define ADIS16136_REG_GLOB_CMD 0x28
  42. #define ADIS16136_REG_LOT1 0x32
  43. #define ADIS16136_REG_LOT2 0x34
  44. #define ADIS16136_REG_LOT3 0x36
  45. #define ADIS16136_REG_PROD_ID 0x38
  46. #define ADIS16136_REG_SERIAL_NUM 0x3A
  47. #define ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL 2
  48. #define ADIS16136_DIAG_STAT_SPI_FAIL 3
  49. #define ADIS16136_DIAG_STAT_SELF_TEST_FAIL 5
  50. #define ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL 6
  51. #define ADIS16136_MSC_CTRL_MEMORY_TEST BIT(11)
  52. #define ADIS16136_MSC_CTRL_SELF_TEST BIT(10)
  53. struct adis16136_chip_info {
  54. unsigned int precision;
  55. unsigned int fullscale;
  56. };
  57. struct adis16136 {
  58. const struct adis16136_chip_info *chip_info;
  59. struct adis adis;
  60. };
  61. #ifdef CONFIG_DEBUG_FS
  62. static ssize_t adis16136_show_serial(struct file *file,
  63. char __user *userbuf, size_t count, loff_t *ppos)
  64. {
  65. struct adis16136 *adis16136 = file->private_data;
  66. uint16_t lot1, lot2, lot3, serial;
  67. char buf[20];
  68. size_t len;
  69. int ret;
  70. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SERIAL_NUM,
  71. &serial);
  72. if (ret < 0)
  73. return ret;
  74. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT1, &lot1);
  75. if (ret < 0)
  76. return ret;
  77. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT2, &lot2);
  78. if (ret < 0)
  79. return ret;
  80. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_LOT3, &lot3);
  81. if (ret < 0)
  82. return ret;
  83. len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2,
  84. lot3, serial);
  85. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  86. }
  87. static const struct file_operations adis16136_serial_fops = {
  88. .open = simple_open,
  89. .read = adis16136_show_serial,
  90. .llseek = default_llseek,
  91. .owner = THIS_MODULE,
  92. };
  93. static int adis16136_show_product_id(void *arg, u64 *val)
  94. {
  95. struct adis16136 *adis16136 = arg;
  96. u16 prod_id;
  97. int ret;
  98. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID,
  99. &prod_id);
  100. if (ret < 0)
  101. return ret;
  102. *val = prod_id;
  103. return 0;
  104. }
  105. DEFINE_SIMPLE_ATTRIBUTE(adis16136_product_id_fops,
  106. adis16136_show_product_id, NULL, "%llu\n");
  107. static int adis16136_show_flash_count(void *arg, u64 *val)
  108. {
  109. struct adis16136 *adis16136 = arg;
  110. uint16_t flash_count;
  111. int ret;
  112. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_FLASH_CNT,
  113. &flash_count);
  114. if (ret < 0)
  115. return ret;
  116. *val = flash_count;
  117. return 0;
  118. }
  119. DEFINE_SIMPLE_ATTRIBUTE(adis16136_flash_count_fops,
  120. adis16136_show_flash_count, NULL, "%lld\n");
  121. static int adis16136_debugfs_init(struct iio_dev *indio_dev)
  122. {
  123. struct adis16136 *adis16136 = iio_priv(indio_dev);
  124. debugfs_create_file("serial_number", 0400, indio_dev->debugfs_dentry,
  125. adis16136, &adis16136_serial_fops);
  126. debugfs_create_file("product_id", 0400, indio_dev->debugfs_dentry,
  127. adis16136, &adis16136_product_id_fops);
  128. debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
  129. adis16136, &adis16136_flash_count_fops);
  130. return 0;
  131. }
  132. #else
  133. static int adis16136_debugfs_init(struct iio_dev *indio_dev)
  134. {
  135. return 0;
  136. }
  137. #endif
  138. static int adis16136_set_freq(struct adis16136 *adis16136, unsigned int freq)
  139. {
  140. unsigned int t;
  141. t = 32768 / freq;
  142. if (t < 0xf)
  143. t = 0xf;
  144. else if (t > 0xffff)
  145. t = 0xffff;
  146. else
  147. t--;
  148. return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, t);
  149. }
  150. static int adis16136_get_freq(struct adis16136 *adis16136, unsigned int *freq)
  151. {
  152. uint16_t t;
  153. int ret;
  154. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_SMPL_PRD, &t);
  155. if (ret < 0)
  156. return ret;
  157. *freq = 32768 / (t + 1);
  158. return 0;
  159. }
  160. static ssize_t adis16136_write_frequency(struct device *dev,
  161. struct device_attribute *attr, const char *buf, size_t len)
  162. {
  163. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  164. struct adis16136 *adis16136 = iio_priv(indio_dev);
  165. unsigned int val;
  166. int ret;
  167. ret = kstrtouint(buf, 10, &val);
  168. if (ret)
  169. return ret;
  170. if (val == 0)
  171. return -EINVAL;
  172. ret = adis16136_set_freq(adis16136, val);
  173. return ret ? ret : len;
  174. }
  175. static ssize_t adis16136_read_frequency(struct device *dev,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  179. struct adis16136 *adis16136 = iio_priv(indio_dev);
  180. unsigned int freq;
  181. int ret;
  182. ret = adis16136_get_freq(adis16136, &freq);
  183. if (ret < 0)
  184. return ret;
  185. return sprintf(buf, "%d\n", freq);
  186. }
  187. static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
  188. adis16136_read_frequency,
  189. adis16136_write_frequency);
  190. static const unsigned adis16136_3db_divisors[] = {
  191. [0] = 2, /* Special case */
  192. [1] = 6,
  193. [2] = 12,
  194. [3] = 25,
  195. [4] = 50,
  196. [5] = 100,
  197. [6] = 200,
  198. [7] = 200, /* Not a valid setting */
  199. };
  200. static int adis16136_set_filter(struct iio_dev *indio_dev, int val)
  201. {
  202. struct adis16136 *adis16136 = iio_priv(indio_dev);
  203. unsigned int freq;
  204. int i, ret;
  205. ret = adis16136_get_freq(adis16136, &freq);
  206. if (ret < 0)
  207. return ret;
  208. for (i = ARRAY_SIZE(adis16136_3db_divisors) - 1; i >= 1; i--) {
  209. if (freq / adis16136_3db_divisors[i] >= val)
  210. break;
  211. }
  212. return adis_write_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, i);
  213. }
  214. static int adis16136_get_filter(struct iio_dev *indio_dev, int *val)
  215. {
  216. struct adis16136 *adis16136 = iio_priv(indio_dev);
  217. unsigned int freq;
  218. uint16_t val16;
  219. int ret;
  220. mutex_lock(&indio_dev->mlock);
  221. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_AVG_CNT, &val16);
  222. if (ret < 0)
  223. goto err_unlock;
  224. ret = adis16136_get_freq(adis16136, &freq);
  225. if (ret < 0)
  226. goto err_unlock;
  227. *val = freq / adis16136_3db_divisors[val16 & 0x07];
  228. err_unlock:
  229. mutex_unlock(&indio_dev->mlock);
  230. return ret ? ret : IIO_VAL_INT;
  231. }
  232. static int adis16136_read_raw(struct iio_dev *indio_dev,
  233. const struct iio_chan_spec *chan, int *val, int *val2, long info)
  234. {
  235. struct adis16136 *adis16136 = iio_priv(indio_dev);
  236. uint32_t val32;
  237. int ret;
  238. switch (info) {
  239. case IIO_CHAN_INFO_RAW:
  240. return adis_single_conversion(indio_dev, chan, 0, val);
  241. case IIO_CHAN_INFO_SCALE:
  242. switch (chan->type) {
  243. case IIO_ANGL_VEL:
  244. *val = adis16136->chip_info->precision;
  245. *val2 = (adis16136->chip_info->fullscale << 16);
  246. return IIO_VAL_FRACTIONAL;
  247. case IIO_TEMP:
  248. *val = 10;
  249. *val2 = 697000; /* 0.010697 degree Celsius */
  250. return IIO_VAL_INT_PLUS_MICRO;
  251. default:
  252. return -EINVAL;
  253. }
  254. case IIO_CHAN_INFO_CALIBBIAS:
  255. ret = adis_read_reg_32(&adis16136->adis,
  256. ADIS16136_REG_GYRO_OFF2, &val32);
  257. if (ret < 0)
  258. return ret;
  259. *val = sign_extend32(val32, 31);
  260. return IIO_VAL_INT;
  261. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  262. return adis16136_get_filter(indio_dev, val);
  263. default:
  264. return -EINVAL;
  265. }
  266. }
  267. static int adis16136_write_raw(struct iio_dev *indio_dev,
  268. const struct iio_chan_spec *chan, int val, int val2, long info)
  269. {
  270. struct adis16136 *adis16136 = iio_priv(indio_dev);
  271. switch (info) {
  272. case IIO_CHAN_INFO_CALIBBIAS:
  273. return adis_write_reg_32(&adis16136->adis,
  274. ADIS16136_REG_GYRO_OFF2, val);
  275. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  276. return adis16136_set_filter(indio_dev, val);
  277. default:
  278. break;
  279. }
  280. return -EINVAL;
  281. }
  282. enum {
  283. ADIS16136_SCAN_GYRO,
  284. ADIS16136_SCAN_TEMP,
  285. };
  286. static const struct iio_chan_spec adis16136_channels[] = {
  287. {
  288. .type = IIO_ANGL_VEL,
  289. .modified = 1,
  290. .channel2 = IIO_MOD_X,
  291. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  292. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  293. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  294. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  295. .address = ADIS16136_REG_GYRO_OUT2,
  296. .scan_index = ADIS16136_SCAN_GYRO,
  297. .scan_type = {
  298. .sign = 's',
  299. .realbits = 32,
  300. .storagebits = 32,
  301. .endianness = IIO_BE,
  302. },
  303. }, {
  304. .type = IIO_TEMP,
  305. .indexed = 1,
  306. .channel = 0,
  307. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  308. BIT(IIO_CHAN_INFO_SCALE),
  309. .address = ADIS16136_REG_TEMP_OUT,
  310. .scan_index = ADIS16136_SCAN_TEMP,
  311. .scan_type = {
  312. .sign = 's',
  313. .realbits = 16,
  314. .storagebits = 16,
  315. .endianness = IIO_BE,
  316. },
  317. },
  318. IIO_CHAN_SOFT_TIMESTAMP(2),
  319. };
  320. static struct attribute *adis16136_attributes[] = {
  321. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  322. NULL
  323. };
  324. static const struct attribute_group adis16136_attribute_group = {
  325. .attrs = adis16136_attributes,
  326. };
  327. static const struct iio_info adis16136_info = {
  328. .driver_module = THIS_MODULE,
  329. .attrs = &adis16136_attribute_group,
  330. .read_raw = &adis16136_read_raw,
  331. .write_raw = &adis16136_write_raw,
  332. .update_scan_mode = adis_update_scan_mode,
  333. .debugfs_reg_access = adis_debugfs_reg_access,
  334. };
  335. static int adis16136_stop_device(struct iio_dev *indio_dev)
  336. {
  337. struct adis16136 *adis16136 = iio_priv(indio_dev);
  338. int ret;
  339. ret = adis_write_reg_16(&adis16136->adis, ADIS16136_REG_SLP_CTRL, 0xff);
  340. if (ret)
  341. dev_err(&indio_dev->dev,
  342. "Could not power down device: %d\n", ret);
  343. return ret;
  344. }
  345. static int adis16136_initial_setup(struct iio_dev *indio_dev)
  346. {
  347. struct adis16136 *adis16136 = iio_priv(indio_dev);
  348. unsigned int device_id;
  349. uint16_t prod_id;
  350. int ret;
  351. ret = adis_initial_startup(&adis16136->adis);
  352. if (ret)
  353. return ret;
  354. ret = adis_read_reg_16(&adis16136->adis, ADIS16136_REG_PROD_ID,
  355. &prod_id);
  356. if (ret)
  357. return ret;
  358. ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
  359. if (ret != 1)
  360. return -EINVAL;
  361. if (prod_id != device_id)
  362. dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
  363. device_id, prod_id);
  364. return 0;
  365. }
  366. static const char * const adis16136_status_error_msgs[] = {
  367. [ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL] = "Flash update failed",
  368. [ADIS16136_DIAG_STAT_SPI_FAIL] = "SPI failure",
  369. [ADIS16136_DIAG_STAT_SELF_TEST_FAIL] = "Self test error",
  370. [ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL] = "Flash checksum error",
  371. };
  372. static const struct adis_data adis16136_data = {
  373. .diag_stat_reg = ADIS16136_REG_DIAG_STAT,
  374. .glob_cmd_reg = ADIS16136_REG_GLOB_CMD,
  375. .msc_ctrl_reg = ADIS16136_REG_MSC_CTRL,
  376. .self_test_mask = ADIS16136_MSC_CTRL_SELF_TEST,
  377. .startup_delay = 80,
  378. .read_delay = 10,
  379. .write_delay = 10,
  380. .status_error_msgs = adis16136_status_error_msgs,
  381. .status_error_mask = BIT(ADIS16136_DIAG_STAT_FLASH_UPDATE_FAIL) |
  382. BIT(ADIS16136_DIAG_STAT_SPI_FAIL) |
  383. BIT(ADIS16136_DIAG_STAT_SELF_TEST_FAIL) |
  384. BIT(ADIS16136_DIAG_STAT_FLASH_CHKSUM_FAIL),
  385. };
  386. enum adis16136_id {
  387. ID_ADIS16133,
  388. ID_ADIS16135,
  389. ID_ADIS16136,
  390. ID_ADIS16137,
  391. };
  392. static const struct adis16136_chip_info adis16136_chip_info[] = {
  393. [ID_ADIS16133] = {
  394. .precision = IIO_DEGREE_TO_RAD(1200),
  395. .fullscale = 24000,
  396. },
  397. [ID_ADIS16135] = {
  398. .precision = IIO_DEGREE_TO_RAD(300),
  399. .fullscale = 24000,
  400. },
  401. [ID_ADIS16136] = {
  402. .precision = IIO_DEGREE_TO_RAD(450),
  403. .fullscale = 24623,
  404. },
  405. [ID_ADIS16137] = {
  406. .precision = IIO_DEGREE_TO_RAD(1000),
  407. .fullscale = 24609,
  408. },
  409. };
  410. static int adis16136_probe(struct spi_device *spi)
  411. {
  412. const struct spi_device_id *id = spi_get_device_id(spi);
  413. struct adis16136 *adis16136;
  414. struct iio_dev *indio_dev;
  415. int ret;
  416. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16136));
  417. if (indio_dev == NULL)
  418. return -ENOMEM;
  419. spi_set_drvdata(spi, indio_dev);
  420. adis16136 = iio_priv(indio_dev);
  421. adis16136->chip_info = &adis16136_chip_info[id->driver_data];
  422. indio_dev->dev.parent = &spi->dev;
  423. indio_dev->name = spi_get_device_id(spi)->name;
  424. indio_dev->channels = adis16136_channels;
  425. indio_dev->num_channels = ARRAY_SIZE(adis16136_channels);
  426. indio_dev->info = &adis16136_info;
  427. indio_dev->modes = INDIO_DIRECT_MODE;
  428. ret = adis_init(&adis16136->adis, indio_dev, spi, &adis16136_data);
  429. if (ret)
  430. return ret;
  431. ret = adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL);
  432. if (ret)
  433. return ret;
  434. ret = adis16136_initial_setup(indio_dev);
  435. if (ret)
  436. goto error_cleanup_buffer;
  437. ret = iio_device_register(indio_dev);
  438. if (ret)
  439. goto error_stop_device;
  440. adis16136_debugfs_init(indio_dev);
  441. return 0;
  442. error_stop_device:
  443. adis16136_stop_device(indio_dev);
  444. error_cleanup_buffer:
  445. adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev);
  446. return ret;
  447. }
  448. static int adis16136_remove(struct spi_device *spi)
  449. {
  450. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  451. struct adis16136 *adis16136 = iio_priv(indio_dev);
  452. iio_device_unregister(indio_dev);
  453. adis16136_stop_device(indio_dev);
  454. adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev);
  455. return 0;
  456. }
  457. static const struct spi_device_id adis16136_ids[] = {
  458. { "adis16133", ID_ADIS16133 },
  459. { "adis16135", ID_ADIS16135 },
  460. { "adis16136", ID_ADIS16136 },
  461. { "adis16137", ID_ADIS16137 },
  462. { }
  463. };
  464. MODULE_DEVICE_TABLE(spi, adis16136_ids);
  465. static struct spi_driver adis16136_driver = {
  466. .driver = {
  467. .name = "adis16136",
  468. },
  469. .id_table = adis16136_ids,
  470. .probe = adis16136_probe,
  471. .remove = adis16136_remove,
  472. };
  473. module_spi_driver(adis16136_driver);
  474. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  475. MODULE_DESCRIPTION("Analog Devices ADIS16133/ADIS16135/ADIS16136 gyroscope driver");
  476. MODULE_LICENSE("GPL v2");