adis16136.c 14 KB

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