/drivers/staging/iio/accel/adis16240_core.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 647 lines · 535 code · 77 blank · 35 comment · 45 complexity · e6d2f492c9ea429ce0f44d046c9e026b MD5 · raw file

  1. /*
  2. * ADIS16240 Programmable Impact Sensor and Recorder driver
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/gpio.h>
  11. #include <linux/delay.h>
  12. #include <linux/mutex.h>
  13. #include <linux/device.h>
  14. #include <linux/kernel.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/slab.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/list.h>
  19. #include "../iio.h"
  20. #include "../sysfs.h"
  21. #include "../ring_generic.h"
  22. #include "accel.h"
  23. #include "../adc/adc.h"
  24. #include "adis16240.h"
  25. #define DRIVER_NAME "adis16240"
  26. static int adis16240_check_status(struct iio_dev *indio_dev);
  27. /**
  28. * adis16240_spi_write_reg_8() - write single byte to a register
  29. * @indio_dev: iio_dev associated with device
  30. * @reg_address: the address of the register to be written
  31. * @val: the value to write
  32. **/
  33. static int adis16240_spi_write_reg_8(struct iio_dev *indio_dev,
  34. u8 reg_address,
  35. u8 val)
  36. {
  37. int ret;
  38. struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
  39. mutex_lock(&st->buf_lock);
  40. st->tx[0] = ADIS16240_WRITE_REG(reg_address);
  41. st->tx[1] = val;
  42. ret = spi_write(st->us, st->tx, 2);
  43. mutex_unlock(&st->buf_lock);
  44. return ret;
  45. }
  46. /**
  47. * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
  48. * @indio_dev: iio_dev for this device
  49. * @reg_address: the address of the lower of the two registers. Second register
  50. * is assumed to have address one greater.
  51. * @val: value to be written
  52. **/
  53. static int adis16240_spi_write_reg_16(struct iio_dev *indio_dev,
  54. u8 lower_reg_address,
  55. u16 value)
  56. {
  57. int ret;
  58. struct spi_message msg;
  59. struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
  60. struct spi_transfer xfers[] = {
  61. {
  62. .tx_buf = st->tx,
  63. .bits_per_word = 8,
  64. .len = 2,
  65. .cs_change = 1,
  66. .delay_usecs = 35,
  67. }, {
  68. .tx_buf = st->tx + 2,
  69. .bits_per_word = 8,
  70. .len = 2,
  71. .delay_usecs = 35,
  72. },
  73. };
  74. mutex_lock(&st->buf_lock);
  75. st->tx[0] = ADIS16240_WRITE_REG(lower_reg_address);
  76. st->tx[1] = value & 0xFF;
  77. st->tx[2] = ADIS16240_WRITE_REG(lower_reg_address + 1);
  78. st->tx[3] = (value >> 8) & 0xFF;
  79. spi_message_init(&msg);
  80. spi_message_add_tail(&xfers[0], &msg);
  81. spi_message_add_tail(&xfers[1], &msg);
  82. ret = spi_sync(st->us, &msg);
  83. mutex_unlock(&st->buf_lock);
  84. return ret;
  85. }
  86. /**
  87. * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
  88. * @indio_dev: iio_dev for this device
  89. * @reg_address: the address of the lower of the two registers. Second register
  90. * is assumed to have address one greater.
  91. * @val: somewhere to pass back the value read
  92. **/
  93. static int adis16240_spi_read_reg_16(struct iio_dev *indio_dev,
  94. u8 lower_reg_address,
  95. u16 *val)
  96. {
  97. struct spi_message msg;
  98. struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
  99. int ret;
  100. struct spi_transfer xfers[] = {
  101. {
  102. .tx_buf = st->tx,
  103. .bits_per_word = 8,
  104. .len = 2,
  105. .cs_change = 1,
  106. .delay_usecs = 35,
  107. }, {
  108. .rx_buf = st->rx,
  109. .bits_per_word = 8,
  110. .len = 2,
  111. .cs_change = 1,
  112. .delay_usecs = 35,
  113. },
  114. };
  115. mutex_lock(&st->buf_lock);
  116. st->tx[0] = ADIS16240_READ_REG(lower_reg_address);
  117. st->tx[1] = 0;
  118. st->tx[2] = 0;
  119. st->tx[3] = 0;
  120. spi_message_init(&msg);
  121. spi_message_add_tail(&xfers[0], &msg);
  122. spi_message_add_tail(&xfers[1], &msg);
  123. ret = spi_sync(st->us, &msg);
  124. if (ret) {
  125. dev_err(&st->us->dev,
  126. "problem when reading 16 bit register 0x%02X",
  127. lower_reg_address);
  128. goto error_ret;
  129. }
  130. *val = (st->rx[0] << 8) | st->rx[1];
  131. error_ret:
  132. mutex_unlock(&st->buf_lock);
  133. return ret;
  134. }
  135. static ssize_t adis16240_spi_read_signed(struct device *dev,
  136. struct device_attribute *attr,
  137. char *buf,
  138. unsigned bits)
  139. {
  140. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  141. int ret;
  142. s16 val = 0;
  143. unsigned shift = 16 - bits;
  144. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  145. ret = adis16240_spi_read_reg_16(indio_dev,
  146. this_attr->address, (u16 *)&val);
  147. if (ret)
  148. return ret;
  149. if (val & ADIS16240_ERROR_ACTIVE)
  150. adis16240_check_status(indio_dev);
  151. val = ((s16)(val << shift) >> shift);
  152. return sprintf(buf, "%d\n", val);
  153. }
  154. static ssize_t adis16240_read_12bit_signed(struct device *dev,
  155. struct device_attribute *attr,
  156. char *buf)
  157. {
  158. ssize_t ret;
  159. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  160. /* Take the iio_dev status lock */
  161. mutex_lock(&indio_dev->mlock);
  162. ret = adis16240_spi_read_signed(dev, attr, buf, 12);
  163. mutex_unlock(&indio_dev->mlock);
  164. return ret;
  165. }
  166. static int adis16240_reset(struct iio_dev *indio_dev)
  167. {
  168. int ret;
  169. ret = adis16240_spi_write_reg_8(indio_dev,
  170. ADIS16240_GLOB_CMD,
  171. ADIS16240_GLOB_CMD_SW_RESET);
  172. if (ret)
  173. dev_err(&indio_dev->dev, "problem resetting device");
  174. return ret;
  175. }
  176. static ssize_t adis16240_write_reset(struct device *dev,
  177. struct device_attribute *attr,
  178. const char *buf, size_t len)
  179. {
  180. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  181. if (len < 1)
  182. return -EINVAL;
  183. switch (buf[0]) {
  184. case '1':
  185. case 'y':
  186. case 'Y':
  187. return adis16240_reset(indio_dev);
  188. }
  189. return -EINVAL;
  190. }
  191. int adis16240_set_irq(struct iio_dev *indio_dev, bool enable)
  192. {
  193. int ret = 0;
  194. u16 msc;
  195. ret = adis16240_spi_read_reg_16(indio_dev,
  196. ADIS16240_MSC_CTRL, &msc);
  197. if (ret)
  198. goto error_ret;
  199. msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
  200. msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
  201. if (enable)
  202. msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
  203. else
  204. msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
  205. ret = adis16240_spi_write_reg_16(indio_dev,
  206. ADIS16240_MSC_CTRL, msc);
  207. error_ret:
  208. return ret;
  209. }
  210. static int adis16240_self_test(struct iio_dev *indio_dev)
  211. {
  212. int ret;
  213. ret = adis16240_spi_write_reg_16(indio_dev,
  214. ADIS16240_MSC_CTRL,
  215. ADIS16240_MSC_CTRL_SELF_TEST_EN);
  216. if (ret) {
  217. dev_err(&indio_dev->dev, "problem starting self test");
  218. goto err_ret;
  219. }
  220. msleep(ADIS16240_STARTUP_DELAY);
  221. adis16240_check_status(indio_dev);
  222. err_ret:
  223. return ret;
  224. }
  225. static int adis16240_check_status(struct iio_dev *indio_dev)
  226. {
  227. u16 status;
  228. int ret;
  229. struct device *dev = &indio_dev->dev;
  230. ret = adis16240_spi_read_reg_16(indio_dev,
  231. ADIS16240_DIAG_STAT, &status);
  232. if (ret < 0) {
  233. dev_err(dev, "Reading status failed\n");
  234. goto error_ret;
  235. }
  236. ret = status & 0x2F;
  237. if (status & ADIS16240_DIAG_STAT_PWRON_FAIL)
  238. dev_err(dev, "Power-on, self-test fail\n");
  239. if (status & ADIS16240_DIAG_STAT_SPI_FAIL)
  240. dev_err(dev, "SPI failure\n");
  241. if (status & ADIS16240_DIAG_STAT_FLASH_UPT)
  242. dev_err(dev, "Flash update failed\n");
  243. if (status & ADIS16240_DIAG_STAT_POWER_HIGH)
  244. dev_err(dev, "Power supply above 3.625V\n");
  245. if (status & ADIS16240_DIAG_STAT_POWER_LOW)
  246. dev_err(dev, "Power supply below 2.225V\n");
  247. error_ret:
  248. return ret;
  249. }
  250. static int adis16240_initial_setup(struct iio_dev *indio_dev)
  251. {
  252. int ret;
  253. struct device *dev = &indio_dev->dev;
  254. /* Disable IRQ */
  255. ret = adis16240_set_irq(indio_dev, false);
  256. if (ret) {
  257. dev_err(dev, "disable irq failed");
  258. goto err_ret;
  259. }
  260. /* Do self test */
  261. ret = adis16240_self_test(indio_dev);
  262. if (ret) {
  263. dev_err(dev, "self test failure");
  264. goto err_ret;
  265. }
  266. /* Read status register to check the result */
  267. ret = adis16240_check_status(indio_dev);
  268. if (ret) {
  269. adis16240_reset(indio_dev);
  270. dev_err(dev, "device not playing ball -> reset");
  271. msleep(ADIS16240_STARTUP_DELAY);
  272. ret = adis16240_check_status(indio_dev);
  273. if (ret) {
  274. dev_err(dev, "giving up");
  275. goto err_ret;
  276. }
  277. }
  278. err_ret:
  279. return ret;
  280. }
  281. static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw, S_IRUGO,
  282. adis16240_read_12bit_signed, NULL,
  283. ADIS16240_XYZPEAK_OUT);
  284. static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0);
  285. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
  286. enum adis16240_chan {
  287. in_supply,
  288. in_aux,
  289. accel_x,
  290. accel_y,
  291. accel_z,
  292. temp,
  293. };
  294. static const u8 adis16240_addresses[6][3] = {
  295. [in_supply] = { ADIS16240_SUPPLY_OUT },
  296. [in_aux] = { ADIS16240_AUX_ADC },
  297. [accel_x] = { ADIS16240_XACCL_OUT, ADIS16240_XACCL_OFF,
  298. ADIS16240_XPEAK_OUT },
  299. [accel_y] = { ADIS16240_YACCL_OUT, ADIS16240_YACCL_OFF,
  300. ADIS16240_YPEAK_OUT },
  301. [accel_z] = { ADIS16240_ZACCL_OUT, ADIS16240_ZACCL_OFF,
  302. ADIS16240_ZPEAK_OUT },
  303. [temp] = { ADIS16240_TEMP_OUT },
  304. };
  305. static int adis16240_read_raw(struct iio_dev *indio_dev,
  306. struct iio_chan_spec const *chan,
  307. int *val, int *val2,
  308. long mask)
  309. {
  310. int ret;
  311. int bits;
  312. u8 addr;
  313. s16 val16;
  314. switch (mask) {
  315. case 0:
  316. mutex_lock(&indio_dev->mlock);
  317. addr = adis16240_addresses[chan->address][0];
  318. ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
  319. if (ret)
  320. return ret;
  321. if (val16 & ADIS16240_ERROR_ACTIVE) {
  322. ret = adis16240_check_status(indio_dev);
  323. if (ret)
  324. return ret;
  325. }
  326. val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
  327. if (chan->scan_type.sign == 's')
  328. val16 = (s16)(val16 <<
  329. (16 - chan->scan_type.realbits)) >>
  330. (16 - chan->scan_type.realbits);
  331. *val = val16;
  332. mutex_unlock(&indio_dev->mlock);
  333. return IIO_VAL_INT;
  334. case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
  335. case (1 << IIO_CHAN_INFO_SCALE_SHARED):
  336. switch (chan->type) {
  337. case IIO_IN:
  338. *val = 0;
  339. if (chan->channel == 0)
  340. *val2 = 4880;
  341. else
  342. return -EINVAL;
  343. return IIO_VAL_INT_PLUS_MICRO;
  344. case IIO_TEMP:
  345. *val = 0;
  346. *val2 = 244000;
  347. return IIO_VAL_INT_PLUS_MICRO;
  348. case IIO_ACCEL:
  349. *val = 0;
  350. *val2 = 504062;
  351. return IIO_VAL_INT_PLUS_MICRO;
  352. default:
  353. return -EINVAL;
  354. }
  355. break;
  356. case (1 << IIO_CHAN_INFO_PEAK_SCALE_SHARED):
  357. *val = 6;
  358. *val2 = 629295;
  359. return IIO_VAL_INT_PLUS_MICRO;
  360. case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
  361. *val = 25;
  362. return IIO_VAL_INT;
  363. case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
  364. bits = 10;
  365. mutex_lock(&indio_dev->mlock);
  366. addr = adis16240_addresses[chan->address][1];
  367. ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
  368. if (ret) {
  369. mutex_unlock(&indio_dev->mlock);
  370. return ret;
  371. }
  372. val16 &= (1 << bits) - 1;
  373. val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
  374. *val = val16;
  375. mutex_unlock(&indio_dev->mlock);
  376. return IIO_VAL_INT;
  377. case (1 << IIO_CHAN_INFO_PEAK_SEPARATE):
  378. bits = 10;
  379. mutex_lock(&indio_dev->mlock);
  380. addr = adis16240_addresses[chan->address][2];
  381. ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
  382. if (ret) {
  383. mutex_unlock(&indio_dev->mlock);
  384. return ret;
  385. }
  386. val16 &= (1 << bits) - 1;
  387. val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
  388. *val = val16;
  389. mutex_unlock(&indio_dev->mlock);
  390. return IIO_VAL_INT;
  391. }
  392. return -EINVAL;
  393. }
  394. static int adis16240_write_raw(struct iio_dev *indio_dev,
  395. struct iio_chan_spec const *chan,
  396. int val,
  397. int val2,
  398. long mask)
  399. {
  400. int bits = 10;
  401. s16 val16;
  402. u8 addr;
  403. switch (mask) {
  404. case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
  405. val16 = val & ((1 << bits) - 1);
  406. addr = adis16240_addresses[chan->address][1];
  407. return adis16240_spi_write_reg_16(indio_dev, addr, val16);
  408. }
  409. return -EINVAL;
  410. }
  411. static struct iio_chan_spec adis16240_channels[] = {
  412. IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
  413. (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
  414. in_supply, ADIS16240_SCAN_SUPPLY,
  415. IIO_ST('u', 10, 16, 0), 0),
  416. IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
  417. 0,
  418. in_aux, ADIS16240_SCAN_AUX_ADC,
  419. IIO_ST('u', 10, 16, 0), 0),
  420. IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
  421. (1 << IIO_CHAN_INFO_SCALE_SHARED) |
  422. (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
  423. accel_x, ADIS16240_SCAN_ACC_X,
  424. IIO_ST('s', 10, 16, 0), 0),
  425. IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
  426. (1 << IIO_CHAN_INFO_SCALE_SHARED) |
  427. (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
  428. accel_y, ADIS16240_SCAN_ACC_Y,
  429. IIO_ST('s', 10, 16, 0), 0),
  430. IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z,
  431. (1 << IIO_CHAN_INFO_SCALE_SHARED) |
  432. (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
  433. accel_z, ADIS16240_SCAN_ACC_Z,
  434. IIO_ST('s', 10, 16, 0), 0),
  435. IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
  436. (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
  437. temp, ADIS16240_SCAN_TEMP,
  438. IIO_ST('u', 10, 16, 0), 0),
  439. IIO_CHAN_SOFT_TIMESTAMP(6)
  440. };
  441. static struct attribute *adis16240_attributes[] = {
  442. &iio_dev_attr_accel_xyz_squared_peak_raw.dev_attr.attr,
  443. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  444. &iio_dev_attr_reset.dev_attr.attr,
  445. NULL
  446. };
  447. static const struct attribute_group adis16240_attribute_group = {
  448. .attrs = adis16240_attributes,
  449. };
  450. static const struct iio_info adis16240_info = {
  451. .attrs = &adis16240_attribute_group,
  452. .read_raw = &adis16240_read_raw,
  453. .write_raw = &adis16240_write_raw,
  454. .driver_module = THIS_MODULE,
  455. };
  456. static int __devinit adis16240_probe(struct spi_device *spi)
  457. {
  458. int ret, regdone = 0;
  459. struct adis16240_state *st = kzalloc(sizeof *st, GFP_KERNEL);
  460. if (!st) {
  461. ret = -ENOMEM;
  462. goto error_ret;
  463. }
  464. /* this is only used for removal purposes */
  465. spi_set_drvdata(spi, st);
  466. /* Allocate the comms buffers */
  467. st->rx = kzalloc(sizeof(*st->rx)*ADIS16240_MAX_RX, GFP_KERNEL);
  468. if (st->rx == NULL) {
  469. ret = -ENOMEM;
  470. goto error_free_st;
  471. }
  472. st->tx = kzalloc(sizeof(*st->tx)*ADIS16240_MAX_TX, GFP_KERNEL);
  473. if (st->tx == NULL) {
  474. ret = -ENOMEM;
  475. goto error_free_rx;
  476. }
  477. st->us = spi;
  478. mutex_init(&st->buf_lock);
  479. /* setup the industrialio driver allocated elements */
  480. st->indio_dev = iio_allocate_device(0);
  481. if (st->indio_dev == NULL) {
  482. ret = -ENOMEM;
  483. goto error_free_tx;
  484. }
  485. st->indio_dev->name = spi->dev.driver->name;
  486. st->indio_dev->dev.parent = &spi->dev;
  487. st->indio_dev->info = &adis16240_info;
  488. st->indio_dev->channels = adis16240_channels;
  489. st->indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
  490. st->indio_dev->dev_data = (void *)(st);
  491. st->indio_dev->modes = INDIO_DIRECT_MODE;
  492. ret = adis16240_configure_ring(st->indio_dev);
  493. if (ret)
  494. goto error_free_dev;
  495. ret = iio_device_register(st->indio_dev);
  496. if (ret)
  497. goto error_unreg_ring_funcs;
  498. regdone = 1;
  499. ret = iio_ring_buffer_register_ex(st->indio_dev->ring, 0,
  500. adis16240_channels,
  501. ARRAY_SIZE(adis16240_channels));
  502. if (ret) {
  503. printk(KERN_ERR "failed to initialize the ring\n");
  504. goto error_unreg_ring_funcs;
  505. }
  506. if (spi->irq) {
  507. ret = adis16240_probe_trigger(st->indio_dev);
  508. if (ret)
  509. goto error_uninitialize_ring;
  510. }
  511. /* Get the device into a sane initial state */
  512. ret = adis16240_initial_setup(st->indio_dev);
  513. if (ret)
  514. goto error_remove_trigger;
  515. return 0;
  516. error_remove_trigger:
  517. adis16240_remove_trigger(st->indio_dev);
  518. error_uninitialize_ring:
  519. iio_ring_buffer_unregister(st->indio_dev->ring);
  520. error_unreg_ring_funcs:
  521. adis16240_unconfigure_ring(st->indio_dev);
  522. error_free_dev:
  523. if (regdone)
  524. iio_device_unregister(st->indio_dev);
  525. else
  526. iio_free_device(st->indio_dev);
  527. error_free_tx:
  528. kfree(st->tx);
  529. error_free_rx:
  530. kfree(st->rx);
  531. error_free_st:
  532. kfree(st);
  533. error_ret:
  534. return ret;
  535. }
  536. static int adis16240_remove(struct spi_device *spi)
  537. {
  538. struct adis16240_state *st = spi_get_drvdata(spi);
  539. struct iio_dev *indio_dev = st->indio_dev;
  540. flush_scheduled_work();
  541. adis16240_remove_trigger(indio_dev);
  542. iio_ring_buffer_unregister(indio_dev->ring);
  543. iio_device_unregister(indio_dev);
  544. adis16240_unconfigure_ring(indio_dev);
  545. kfree(st->tx);
  546. kfree(st->rx);
  547. kfree(st);
  548. return 0;
  549. }
  550. static struct spi_driver adis16240_driver = {
  551. .driver = {
  552. .name = "adis16240",
  553. .owner = THIS_MODULE,
  554. },
  555. .probe = adis16240_probe,
  556. .remove = __devexit_p(adis16240_remove),
  557. };
  558. static __init int adis16240_init(void)
  559. {
  560. return spi_register_driver(&adis16240_driver);
  561. }
  562. module_init(adis16240_init);
  563. static __exit void adis16240_exit(void)
  564. {
  565. spi_unregister_driver(&adis16240_driver);
  566. }
  567. module_exit(adis16240_exit);
  568. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  569. MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
  570. MODULE_LICENSE("GPL v2");