cc10001_adc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (c) 2014-2015 Imagination Technologies Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/slab.h>
  19. #include <linux/iio/buffer.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #include <linux/iio/trigger.h>
  23. #include <linux/iio/trigger_consumer.h>
  24. #include <linux/iio/triggered_buffer.h>
  25. /* Registers */
  26. #define CC10001_ADC_CONFIG 0x00
  27. #define CC10001_ADC_START_CONV BIT(4)
  28. #define CC10001_ADC_MODE_SINGLE_CONV BIT(5)
  29. #define CC10001_ADC_DDATA_OUT 0x04
  30. #define CC10001_ADC_EOC 0x08
  31. #define CC10001_ADC_EOC_SET BIT(0)
  32. #define CC10001_ADC_CHSEL_SAMPLED 0x0c
  33. #define CC10001_ADC_POWER_DOWN 0x10
  34. #define CC10001_ADC_POWER_DOWN_SET BIT(0)
  35. #define CC10001_ADC_DEBUG 0x14
  36. #define CC10001_ADC_DATA_COUNT 0x20
  37. #define CC10001_ADC_DATA_MASK GENMASK(9, 0)
  38. #define CC10001_ADC_NUM_CHANNELS 8
  39. #define CC10001_ADC_CH_MASK GENMASK(2, 0)
  40. #define CC10001_INVALID_SAMPLED 0xffff
  41. #define CC10001_MAX_POLL_COUNT 20
  42. /*
  43. * As per device specification, wait six clock cycles after power-up to
  44. * activate START. Since adding two more clock cycles delay does not
  45. * impact the performance too much, we are adding two additional cycles delay
  46. * intentionally here.
  47. */
  48. #define CC10001_WAIT_CYCLES 8
  49. struct cc10001_adc_device {
  50. void __iomem *reg_base;
  51. struct clk *adc_clk;
  52. struct regulator *reg;
  53. u16 *buf;
  54. bool shared;
  55. struct mutex lock;
  56. unsigned int start_delay_ns;
  57. unsigned int eoc_delay_ns;
  58. };
  59. static inline void cc10001_adc_write_reg(struct cc10001_adc_device *adc_dev,
  60. u32 reg, u32 val)
  61. {
  62. writel(val, adc_dev->reg_base + reg);
  63. }
  64. static inline u32 cc10001_adc_read_reg(struct cc10001_adc_device *adc_dev,
  65. u32 reg)
  66. {
  67. return readl(adc_dev->reg_base + reg);
  68. }
  69. static void cc10001_adc_power_up(struct cc10001_adc_device *adc_dev)
  70. {
  71. cc10001_adc_write_reg(adc_dev, CC10001_ADC_POWER_DOWN, 0);
  72. ndelay(adc_dev->start_delay_ns);
  73. }
  74. static void cc10001_adc_power_down(struct cc10001_adc_device *adc_dev)
  75. {
  76. cc10001_adc_write_reg(adc_dev, CC10001_ADC_POWER_DOWN,
  77. CC10001_ADC_POWER_DOWN_SET);
  78. }
  79. static void cc10001_adc_start(struct cc10001_adc_device *adc_dev,
  80. unsigned int channel)
  81. {
  82. u32 val;
  83. /* Channel selection and mode of operation */
  84. val = (channel & CC10001_ADC_CH_MASK) | CC10001_ADC_MODE_SINGLE_CONV;
  85. cc10001_adc_write_reg(adc_dev, CC10001_ADC_CONFIG, val);
  86. udelay(1);
  87. val = cc10001_adc_read_reg(adc_dev, CC10001_ADC_CONFIG);
  88. val = val | CC10001_ADC_START_CONV;
  89. cc10001_adc_write_reg(adc_dev, CC10001_ADC_CONFIG, val);
  90. }
  91. static u16 cc10001_adc_poll_done(struct iio_dev *indio_dev,
  92. unsigned int channel,
  93. unsigned int delay)
  94. {
  95. struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
  96. unsigned int poll_count = 0;
  97. while (!(cc10001_adc_read_reg(adc_dev, CC10001_ADC_EOC) &
  98. CC10001_ADC_EOC_SET)) {
  99. ndelay(delay);
  100. if (poll_count++ == CC10001_MAX_POLL_COUNT)
  101. return CC10001_INVALID_SAMPLED;
  102. }
  103. poll_count = 0;
  104. while ((cc10001_adc_read_reg(adc_dev, CC10001_ADC_CHSEL_SAMPLED) &
  105. CC10001_ADC_CH_MASK) != channel) {
  106. ndelay(delay);
  107. if (poll_count++ == CC10001_MAX_POLL_COUNT)
  108. return CC10001_INVALID_SAMPLED;
  109. }
  110. /* Read the 10 bit output register */
  111. return cc10001_adc_read_reg(adc_dev, CC10001_ADC_DDATA_OUT) &
  112. CC10001_ADC_DATA_MASK;
  113. }
  114. static irqreturn_t cc10001_adc_trigger_h(int irq, void *p)
  115. {
  116. struct cc10001_adc_device *adc_dev;
  117. struct iio_poll_func *pf = p;
  118. struct iio_dev *indio_dev;
  119. unsigned int delay_ns;
  120. unsigned int channel;
  121. unsigned int scan_idx;
  122. bool sample_invalid;
  123. u16 *data;
  124. int i;
  125. indio_dev = pf->indio_dev;
  126. adc_dev = iio_priv(indio_dev);
  127. data = adc_dev->buf;
  128. mutex_lock(&adc_dev->lock);
  129. if (!adc_dev->shared)
  130. cc10001_adc_power_up(adc_dev);
  131. /* Calculate delay step for eoc and sampled data */
  132. delay_ns = adc_dev->eoc_delay_ns / CC10001_MAX_POLL_COUNT;
  133. i = 0;
  134. sample_invalid = false;
  135. for_each_set_bit(scan_idx, indio_dev->active_scan_mask,
  136. indio_dev->masklength) {
  137. channel = indio_dev->channels[scan_idx].channel;
  138. cc10001_adc_start(adc_dev, channel);
  139. data[i] = cc10001_adc_poll_done(indio_dev, channel, delay_ns);
  140. if (data[i] == CC10001_INVALID_SAMPLED) {
  141. dev_warn(&indio_dev->dev,
  142. "invalid sample on channel %d\n", channel);
  143. sample_invalid = true;
  144. goto done;
  145. }
  146. i++;
  147. }
  148. done:
  149. if (!adc_dev->shared)
  150. cc10001_adc_power_down(adc_dev);
  151. mutex_unlock(&adc_dev->lock);
  152. if (!sample_invalid)
  153. iio_push_to_buffers_with_timestamp(indio_dev, data,
  154. iio_get_time_ns(indio_dev));
  155. iio_trigger_notify_done(indio_dev->trig);
  156. return IRQ_HANDLED;
  157. }
  158. static u16 cc10001_adc_read_raw_voltage(struct iio_dev *indio_dev,
  159. struct iio_chan_spec const *chan)
  160. {
  161. struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
  162. unsigned int delay_ns;
  163. u16 val;
  164. if (!adc_dev->shared)
  165. cc10001_adc_power_up(adc_dev);
  166. /* Calculate delay step for eoc and sampled data */
  167. delay_ns = adc_dev->eoc_delay_ns / CC10001_MAX_POLL_COUNT;
  168. cc10001_adc_start(adc_dev, chan->channel);
  169. val = cc10001_adc_poll_done(indio_dev, chan->channel, delay_ns);
  170. if (!adc_dev->shared)
  171. cc10001_adc_power_down(adc_dev);
  172. return val;
  173. }
  174. static int cc10001_adc_read_raw(struct iio_dev *indio_dev,
  175. struct iio_chan_spec const *chan,
  176. int *val, int *val2, long mask)
  177. {
  178. struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
  179. int ret;
  180. switch (mask) {
  181. case IIO_CHAN_INFO_RAW:
  182. if (iio_buffer_enabled(indio_dev))
  183. return -EBUSY;
  184. mutex_lock(&adc_dev->lock);
  185. *val = cc10001_adc_read_raw_voltage(indio_dev, chan);
  186. mutex_unlock(&adc_dev->lock);
  187. if (*val == CC10001_INVALID_SAMPLED)
  188. return -EIO;
  189. return IIO_VAL_INT;
  190. case IIO_CHAN_INFO_SCALE:
  191. ret = regulator_get_voltage(adc_dev->reg);
  192. if (ret < 0)
  193. return ret;
  194. *val = ret / 1000;
  195. *val2 = chan->scan_type.realbits;
  196. return IIO_VAL_FRACTIONAL_LOG2;
  197. default:
  198. return -EINVAL;
  199. }
  200. }
  201. static int cc10001_update_scan_mode(struct iio_dev *indio_dev,
  202. const unsigned long *scan_mask)
  203. {
  204. struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
  205. kfree(adc_dev->buf);
  206. adc_dev->buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
  207. if (!adc_dev->buf)
  208. return -ENOMEM;
  209. return 0;
  210. }
  211. static const struct iio_info cc10001_adc_info = {
  212. .driver_module = THIS_MODULE,
  213. .read_raw = &cc10001_adc_read_raw,
  214. .update_scan_mode = &cc10001_update_scan_mode,
  215. };
  216. static int cc10001_adc_channel_init(struct iio_dev *indio_dev,
  217. unsigned long channel_map)
  218. {
  219. struct iio_chan_spec *chan_array, *timestamp;
  220. unsigned int bit, idx = 0;
  221. indio_dev->num_channels = bitmap_weight(&channel_map,
  222. CC10001_ADC_NUM_CHANNELS) + 1;
  223. chan_array = devm_kcalloc(&indio_dev->dev, indio_dev->num_channels,
  224. sizeof(struct iio_chan_spec),
  225. GFP_KERNEL);
  226. if (!chan_array)
  227. return -ENOMEM;
  228. for_each_set_bit(bit, &channel_map, CC10001_ADC_NUM_CHANNELS) {
  229. struct iio_chan_spec *chan = &chan_array[idx];
  230. chan->type = IIO_VOLTAGE;
  231. chan->indexed = 1;
  232. chan->channel = bit;
  233. chan->scan_index = idx;
  234. chan->scan_type.sign = 'u';
  235. chan->scan_type.realbits = 10;
  236. chan->scan_type.storagebits = 16;
  237. chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
  238. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  239. idx++;
  240. }
  241. timestamp = &chan_array[idx];
  242. timestamp->type = IIO_TIMESTAMP;
  243. timestamp->channel = -1;
  244. timestamp->scan_index = idx;
  245. timestamp->scan_type.sign = 's';
  246. timestamp->scan_type.realbits = 64;
  247. timestamp->scan_type.storagebits = 64;
  248. indio_dev->channels = chan_array;
  249. return 0;
  250. }
  251. static int cc10001_adc_probe(struct platform_device *pdev)
  252. {
  253. struct device_node *node = pdev->dev.of_node;
  254. struct cc10001_adc_device *adc_dev;
  255. unsigned long adc_clk_rate;
  256. struct resource *res;
  257. struct iio_dev *indio_dev;
  258. unsigned long channel_map;
  259. int ret;
  260. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev));
  261. if (indio_dev == NULL)
  262. return -ENOMEM;
  263. adc_dev = iio_priv(indio_dev);
  264. channel_map = GENMASK(CC10001_ADC_NUM_CHANNELS - 1, 0);
  265. if (!of_property_read_u32(node, "adc-reserved-channels", &ret)) {
  266. adc_dev->shared = true;
  267. channel_map &= ~ret;
  268. }
  269. adc_dev->reg = devm_regulator_get(&pdev->dev, "vref");
  270. if (IS_ERR(adc_dev->reg))
  271. return PTR_ERR(adc_dev->reg);
  272. ret = regulator_enable(adc_dev->reg);
  273. if (ret)
  274. return ret;
  275. indio_dev->dev.parent = &pdev->dev;
  276. indio_dev->name = dev_name(&pdev->dev);
  277. indio_dev->info = &cc10001_adc_info;
  278. indio_dev->modes = INDIO_DIRECT_MODE;
  279. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  280. adc_dev->reg_base = devm_ioremap_resource(&pdev->dev, res);
  281. if (IS_ERR(adc_dev->reg_base)) {
  282. ret = PTR_ERR(adc_dev->reg_base);
  283. goto err_disable_reg;
  284. }
  285. adc_dev->adc_clk = devm_clk_get(&pdev->dev, "adc");
  286. if (IS_ERR(adc_dev->adc_clk)) {
  287. dev_err(&pdev->dev, "failed to get the clock\n");
  288. ret = PTR_ERR(adc_dev->adc_clk);
  289. goto err_disable_reg;
  290. }
  291. ret = clk_prepare_enable(adc_dev->adc_clk);
  292. if (ret) {
  293. dev_err(&pdev->dev, "failed to enable the clock\n");
  294. goto err_disable_reg;
  295. }
  296. adc_clk_rate = clk_get_rate(adc_dev->adc_clk);
  297. if (!adc_clk_rate) {
  298. ret = -EINVAL;
  299. dev_err(&pdev->dev, "null clock rate!\n");
  300. goto err_disable_clk;
  301. }
  302. adc_dev->eoc_delay_ns = NSEC_PER_SEC / adc_clk_rate;
  303. adc_dev->start_delay_ns = adc_dev->eoc_delay_ns * CC10001_WAIT_CYCLES;
  304. /*
  305. * There is only one register to power-up/power-down the AUX ADC.
  306. * If the ADC is shared among multiple CPUs, always power it up here.
  307. * If the ADC is used only by the MIPS, power-up/power-down at runtime.
  308. */
  309. if (adc_dev->shared)
  310. cc10001_adc_power_up(adc_dev);
  311. /* Setup the ADC channels available on the device */
  312. ret = cc10001_adc_channel_init(indio_dev, channel_map);
  313. if (ret < 0)
  314. goto err_disable_clk;
  315. mutex_init(&adc_dev->lock);
  316. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  317. &cc10001_adc_trigger_h, NULL);
  318. if (ret < 0)
  319. goto err_disable_clk;
  320. ret = iio_device_register(indio_dev);
  321. if (ret < 0)
  322. goto err_cleanup_buffer;
  323. platform_set_drvdata(pdev, indio_dev);
  324. return 0;
  325. err_cleanup_buffer:
  326. iio_triggered_buffer_cleanup(indio_dev);
  327. err_disable_clk:
  328. clk_disable_unprepare(adc_dev->adc_clk);
  329. err_disable_reg:
  330. regulator_disable(adc_dev->reg);
  331. return ret;
  332. }
  333. static int cc10001_adc_remove(struct platform_device *pdev)
  334. {
  335. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  336. struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
  337. cc10001_adc_power_down(adc_dev);
  338. iio_device_unregister(indio_dev);
  339. iio_triggered_buffer_cleanup(indio_dev);
  340. clk_disable_unprepare(adc_dev->adc_clk);
  341. regulator_disable(adc_dev->reg);
  342. return 0;
  343. }
  344. static const struct of_device_id cc10001_adc_dt_ids[] = {
  345. { .compatible = "cosmic,10001-adc", },
  346. { }
  347. };
  348. MODULE_DEVICE_TABLE(of, cc10001_adc_dt_ids);
  349. static struct platform_driver cc10001_adc_driver = {
  350. .driver = {
  351. .name = "cc10001-adc",
  352. .of_match_table = cc10001_adc_dt_ids,
  353. },
  354. .probe = cc10001_adc_probe,
  355. .remove = cc10001_adc_remove,
  356. };
  357. module_platform_driver(cc10001_adc_driver);
  358. MODULE_AUTHOR("Phani Movva <Phani.Movva@imgtec.com>");
  359. MODULE_DESCRIPTION("Cosmic Circuits ADC driver");
  360. MODULE_LICENSE("GPL v2");