PageRenderTime 164ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/sc7731_kernel/drivers/iio/adc/ad_sigma_delta.c

https://gitlab.com/envieidoc/sprd_project
C | 558 lines | 383 code | 93 blank | 82 comment | 35 complexity | 728f77497a0dcdc0eaf40c1b7d2a96a4 MD5 | raw file
  1. /*
  2. * Support code for Analog Devices Sigma-Delta ADCs
  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/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/iio/trigger.h>
  20. #include <linux/iio/trigger_consumer.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/iio/adc/ad_sigma_delta.h>
  23. #include <asm/unaligned.h>
  24. #define AD_SD_COMM_CHAN_MASK 0x3
  25. #define AD_SD_REG_COMM 0x00
  26. #define AD_SD_REG_DATA 0x03
  27. /**
  28. * ad_sd_set_comm() - Set communications register
  29. *
  30. * @sigma_delta: The sigma delta device
  31. * @comm: New value for the communications register
  32. */
  33. void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
  34. {
  35. /* Some variants use the lower two bits of the communications register
  36. * to select the channel */
  37. sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
  38. }
  39. EXPORT_SYMBOL_GPL(ad_sd_set_comm);
  40. /**
  41. * ad_sd_write_reg() - Write a register
  42. *
  43. * @sigma_delta: The sigma delta device
  44. * @reg: Address of the register
  45. * @size: Size of the register (0-3)
  46. * @val: Value to write to the register
  47. *
  48. * Returns 0 on success, an error code otherwise.
  49. **/
  50. int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
  51. unsigned int size, unsigned int val)
  52. {
  53. uint8_t *data = sigma_delta->data;
  54. struct spi_transfer t = {
  55. .tx_buf = data,
  56. .len = size + 1,
  57. .cs_change = sigma_delta->bus_locked,
  58. };
  59. struct spi_message m;
  60. int ret;
  61. data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
  62. switch (size) {
  63. case 3:
  64. data[1] = val >> 16;
  65. data[2] = val >> 8;
  66. data[3] = val;
  67. break;
  68. case 2:
  69. put_unaligned_be16(val, &data[1]);
  70. break;
  71. case 1:
  72. data[1] = val;
  73. break;
  74. case 0:
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. spi_message_init(&m);
  80. spi_message_add_tail(&t, &m);
  81. if (sigma_delta->bus_locked)
  82. ret = spi_sync_locked(sigma_delta->spi, &m);
  83. else
  84. ret = spi_sync(sigma_delta->spi, &m);
  85. return ret;
  86. }
  87. EXPORT_SYMBOL_GPL(ad_sd_write_reg);
  88. static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
  89. unsigned int reg, unsigned int size, uint8_t *val)
  90. {
  91. uint8_t *data = sigma_delta->data;
  92. int ret;
  93. struct spi_transfer t[] = {
  94. {
  95. .tx_buf = data,
  96. .len = 1,
  97. }, {
  98. .rx_buf = val,
  99. .len = size,
  100. .cs_change = sigma_delta->bus_locked,
  101. },
  102. };
  103. struct spi_message m;
  104. spi_message_init(&m);
  105. if (sigma_delta->info->has_registers) {
  106. data[0] = reg << sigma_delta->info->addr_shift;
  107. data[0] |= sigma_delta->info->read_mask;
  108. spi_message_add_tail(&t[0], &m);
  109. }
  110. spi_message_add_tail(&t[1], &m);
  111. if (sigma_delta->bus_locked)
  112. ret = spi_sync_locked(sigma_delta->spi, &m);
  113. else
  114. ret = spi_sync(sigma_delta->spi, &m);
  115. return ret;
  116. }
  117. /**
  118. * ad_sd_read_reg() - Read a register
  119. *
  120. * @sigma_delta: The sigma delta device
  121. * @reg: Address of the register
  122. * @size: Size of the register (1-4)
  123. * @val: Read value
  124. *
  125. * Returns 0 on success, an error code otherwise.
  126. **/
  127. int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
  128. unsigned int reg, unsigned int size, unsigned int *val)
  129. {
  130. int ret;
  131. ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
  132. if (ret < 0)
  133. goto out;
  134. switch (size) {
  135. case 4:
  136. *val = get_unaligned_be32(sigma_delta->data);
  137. break;
  138. case 3:
  139. *val = (sigma_delta->data[0] << 16) |
  140. (sigma_delta->data[1] << 8) |
  141. sigma_delta->data[2];
  142. break;
  143. case 2:
  144. *val = get_unaligned_be16(sigma_delta->data);
  145. break;
  146. case 1:
  147. *val = sigma_delta->data[0];
  148. break;
  149. default:
  150. ret = -EINVAL;
  151. break;
  152. }
  153. out:
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(ad_sd_read_reg);
  157. static int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
  158. unsigned int mode, unsigned int channel)
  159. {
  160. int ret;
  161. ret = ad_sigma_delta_set_channel(sigma_delta, channel);
  162. if (ret)
  163. return ret;
  164. spi_bus_lock(sigma_delta->spi->master);
  165. sigma_delta->bus_locked = true;
  166. INIT_COMPLETION(sigma_delta->completion);
  167. ret = ad_sigma_delta_set_mode(sigma_delta, mode);
  168. if (ret < 0)
  169. goto out;
  170. sigma_delta->irq_dis = false;
  171. enable_irq(sigma_delta->spi->irq);
  172. ret = wait_for_completion_timeout(&sigma_delta->completion, 2*HZ);
  173. if (ret == 0) {
  174. sigma_delta->irq_dis = true;
  175. disable_irq_nosync(sigma_delta->spi->irq);
  176. ret = -EIO;
  177. } else {
  178. ret = 0;
  179. }
  180. out:
  181. sigma_delta->bus_locked = false;
  182. spi_bus_unlock(sigma_delta->spi->master);
  183. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
  184. return ret;
  185. }
  186. /**
  187. * ad_sd_calibrate_all() - Performs channel calibration
  188. * @sigma_delta: The sigma delta device
  189. * @cb: Array of channels and calibration type to perform
  190. * @n: Number of items in cb
  191. *
  192. * Returns 0 on success, an error code otherwise.
  193. **/
  194. int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
  195. const struct ad_sd_calib_data *cb, unsigned int n)
  196. {
  197. unsigned int i;
  198. int ret;
  199. for (i = 0; i < n; i++) {
  200. ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
  201. if (ret)
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(ad_sd_calibrate_all);
  207. /**
  208. * ad_sigma_delta_single_conversion() - Performs a single data conversion
  209. * @indio_dev: The IIO device
  210. * @chan: The conversion is done for this channel
  211. * @val: Pointer to the location where to store the read value
  212. *
  213. * Returns: 0 on success, an error value otherwise.
  214. */
  215. int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
  216. const struct iio_chan_spec *chan, int *val)
  217. {
  218. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  219. unsigned int sample, raw_sample;
  220. int ret = 0;
  221. if (iio_buffer_enabled(indio_dev))
  222. return -EBUSY;
  223. mutex_lock(&indio_dev->mlock);
  224. ad_sigma_delta_set_channel(sigma_delta, chan->address);
  225. spi_bus_lock(sigma_delta->spi->master);
  226. sigma_delta->bus_locked = true;
  227. INIT_COMPLETION(sigma_delta->completion);
  228. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
  229. sigma_delta->irq_dis = false;
  230. enable_irq(sigma_delta->spi->irq);
  231. ret = wait_for_completion_interruptible_timeout(
  232. &sigma_delta->completion, HZ);
  233. sigma_delta->bus_locked = false;
  234. spi_bus_unlock(sigma_delta->spi->master);
  235. if (ret == 0)
  236. ret = -EIO;
  237. if (ret < 0)
  238. goto out;
  239. ret = ad_sd_read_reg(sigma_delta, AD_SD_REG_DATA,
  240. DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
  241. &raw_sample);
  242. out:
  243. if (!sigma_delta->irq_dis) {
  244. disable_irq_nosync(sigma_delta->spi->irq);
  245. sigma_delta->irq_dis = true;
  246. }
  247. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
  248. mutex_unlock(&indio_dev->mlock);
  249. if (ret)
  250. return ret;
  251. sample = raw_sample >> chan->scan_type.shift;
  252. sample &= (1 << chan->scan_type.realbits) - 1;
  253. *val = sample;
  254. ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
  255. if (ret)
  256. return ret;
  257. return IIO_VAL_INT;
  258. }
  259. EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion);
  260. static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
  261. {
  262. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  263. unsigned int channel;
  264. int ret;
  265. ret = iio_triggered_buffer_postenable(indio_dev);
  266. if (ret < 0)
  267. return ret;
  268. channel = find_first_bit(indio_dev->active_scan_mask,
  269. indio_dev->masklength);
  270. ret = ad_sigma_delta_set_channel(sigma_delta,
  271. indio_dev->channels[channel].address);
  272. if (ret)
  273. goto err_predisable;
  274. spi_bus_lock(sigma_delta->spi->master);
  275. sigma_delta->bus_locked = true;
  276. ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
  277. if (ret)
  278. goto err_unlock;
  279. sigma_delta->irq_dis = false;
  280. enable_irq(sigma_delta->spi->irq);
  281. return 0;
  282. err_unlock:
  283. spi_bus_unlock(sigma_delta->spi->master);
  284. err_predisable:
  285. return ret;
  286. }
  287. static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
  288. {
  289. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  290. INIT_COMPLETION(sigma_delta->completion);
  291. wait_for_completion_timeout(&sigma_delta->completion, HZ);
  292. if (!sigma_delta->irq_dis) {
  293. disable_irq_nosync(sigma_delta->spi->irq);
  294. sigma_delta->irq_dis = true;
  295. }
  296. ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
  297. sigma_delta->bus_locked = false;
  298. return spi_bus_unlock(sigma_delta->spi->master);
  299. }
  300. static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
  301. {
  302. struct iio_poll_func *pf = p;
  303. struct iio_dev *indio_dev = pf->indio_dev;
  304. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  305. unsigned int reg_size;
  306. uint8_t data[16];
  307. int ret;
  308. memset(data, 0x00, 16);
  309. /* Guaranteed to be aligned with 8 byte boundary */
  310. if (indio_dev->scan_timestamp)
  311. ((s64 *)data)[1] = pf->timestamp;
  312. reg_size = indio_dev->channels[0].scan_type.realbits +
  313. indio_dev->channels[0].scan_type.shift;
  314. reg_size = DIV_ROUND_UP(reg_size, 8);
  315. switch (reg_size) {
  316. case 4:
  317. case 2:
  318. case 1:
  319. ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
  320. reg_size, &data[0]);
  321. break;
  322. case 3:
  323. /* We store 24 bit samples in a 32 bit word. Keep the upper
  324. * byte set to zero. */
  325. ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
  326. reg_size, &data[1]);
  327. break;
  328. }
  329. iio_push_to_buffers(indio_dev, (uint8_t *)data);
  330. iio_trigger_notify_done(indio_dev->trig);
  331. sigma_delta->irq_dis = false;
  332. enable_irq(sigma_delta->spi->irq);
  333. return IRQ_HANDLED;
  334. }
  335. static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
  336. .preenable = &iio_sw_buffer_preenable,
  337. .postenable = &ad_sd_buffer_postenable,
  338. .predisable = &iio_triggered_buffer_predisable,
  339. .postdisable = &ad_sd_buffer_postdisable,
  340. .validate_scan_mask = &iio_validate_scan_mask_onehot,
  341. };
  342. static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
  343. {
  344. struct ad_sigma_delta *sigma_delta = private;
  345. complete(&sigma_delta->completion);
  346. disable_irq_nosync(irq);
  347. sigma_delta->irq_dis = true;
  348. iio_trigger_poll(sigma_delta->trig, iio_get_time_ns());
  349. return IRQ_HANDLED;
  350. }
  351. /**
  352. * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
  353. * @indio_dev: The IIO device
  354. * @trig: The new trigger
  355. *
  356. * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
  357. * device, -EINVAL otherwise.
  358. */
  359. int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
  360. {
  361. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  362. if (sigma_delta->trig != trig)
  363. return -EINVAL;
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(ad_sd_validate_trigger);
  367. static const struct iio_trigger_ops ad_sd_trigger_ops = {
  368. .owner = THIS_MODULE,
  369. };
  370. static int ad_sd_probe_trigger(struct iio_dev *indio_dev)
  371. {
  372. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  373. int ret;
  374. sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
  375. indio_dev->id);
  376. if (sigma_delta->trig == NULL) {
  377. ret = -ENOMEM;
  378. goto error_ret;
  379. }
  380. sigma_delta->trig->ops = &ad_sd_trigger_ops;
  381. init_completion(&sigma_delta->completion);
  382. ret = request_irq(sigma_delta->spi->irq,
  383. ad_sd_data_rdy_trig_poll,
  384. IRQF_TRIGGER_LOW,
  385. indio_dev->name,
  386. sigma_delta);
  387. if (ret)
  388. goto error_free_trig;
  389. if (!sigma_delta->irq_dis) {
  390. sigma_delta->irq_dis = true;
  391. disable_irq_nosync(sigma_delta->spi->irq);
  392. }
  393. sigma_delta->trig->dev.parent = &sigma_delta->spi->dev;
  394. iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
  395. ret = iio_trigger_register(sigma_delta->trig);
  396. if (ret)
  397. goto error_free_irq;
  398. /* select default trigger */
  399. indio_dev->trig = sigma_delta->trig;
  400. return 0;
  401. error_free_irq:
  402. free_irq(sigma_delta->spi->irq, sigma_delta);
  403. error_free_trig:
  404. iio_trigger_free(sigma_delta->trig);
  405. error_ret:
  406. return ret;
  407. }
  408. static void ad_sd_remove_trigger(struct iio_dev *indio_dev)
  409. {
  410. struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
  411. iio_trigger_unregister(sigma_delta->trig);
  412. free_irq(sigma_delta->spi->irq, sigma_delta);
  413. iio_trigger_free(sigma_delta->trig);
  414. }
  415. /**
  416. * ad_sd_setup_buffer_and_trigger() -
  417. * @indio_dev: The IIO device
  418. */
  419. int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev)
  420. {
  421. int ret;
  422. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  423. &ad_sd_trigger_handler, &ad_sd_buffer_setup_ops);
  424. if (ret)
  425. return ret;
  426. ret = ad_sd_probe_trigger(indio_dev);
  427. if (ret) {
  428. iio_triggered_buffer_cleanup(indio_dev);
  429. return ret;
  430. }
  431. return 0;
  432. }
  433. EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger);
  434. /**
  435. * ad_sd_cleanup_buffer_and_trigger() -
  436. * @indio_dev: The IIO device
  437. */
  438. void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev)
  439. {
  440. ad_sd_remove_trigger(indio_dev);
  441. iio_triggered_buffer_cleanup(indio_dev);
  442. }
  443. EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger);
  444. /**
  445. * ad_sd_init() - Initializes a ad_sigma_delta struct
  446. * @sigma_delta: The ad_sigma_delta device
  447. * @indio_dev: The IIO device which the Sigma Delta device is used for
  448. * @spi: The SPI device for the ad_sigma_delta device
  449. * @info: Device specific callbacks and options
  450. *
  451. * This function needs to be called before any other operations are performed on
  452. * the ad_sigma_delta struct.
  453. */
  454. int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
  455. struct spi_device *spi, const struct ad_sigma_delta_info *info)
  456. {
  457. sigma_delta->spi = spi;
  458. sigma_delta->info = info;
  459. iio_device_set_drvdata(indio_dev, sigma_delta);
  460. return 0;
  461. }
  462. EXPORT_SYMBOL_GPL(ad_sd_init);
  463. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  464. MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
  465. MODULE_LICENSE("GPL v2");