at91-sama5d2_adc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Atmel ADC driver for SAMA5D2 devices and compatible.
  3. *
  4. * Copyright (C) 2015 Atmel,
  5. * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/clk.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/sched.h>
  24. #include <linux/wait.h>
  25. #include <linux/iio/iio.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/regulator/consumer.h>
  28. /* Control Register */
  29. #define AT91_SAMA5D2_CR 0x00
  30. /* Software Reset */
  31. #define AT91_SAMA5D2_CR_SWRST BIT(0)
  32. /* Start Conversion */
  33. #define AT91_SAMA5D2_CR_START BIT(1)
  34. /* Touchscreen Calibration */
  35. #define AT91_SAMA5D2_CR_TSCALIB BIT(2)
  36. /* Comparison Restart */
  37. #define AT91_SAMA5D2_CR_CMPRST BIT(4)
  38. /* Mode Register */
  39. #define AT91_SAMA5D2_MR 0x04
  40. /* Trigger Selection */
  41. #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1)
  42. /* ADTRG */
  43. #define AT91_SAMA5D2_MR_TRGSEL_TRIG0 0
  44. /* TIOA0 */
  45. #define AT91_SAMA5D2_MR_TRGSEL_TRIG1 1
  46. /* TIOA1 */
  47. #define AT91_SAMA5D2_MR_TRGSEL_TRIG2 2
  48. /* TIOA2 */
  49. #define AT91_SAMA5D2_MR_TRGSEL_TRIG3 3
  50. /* PWM event line 0 */
  51. #define AT91_SAMA5D2_MR_TRGSEL_TRIG4 4
  52. /* PWM event line 1 */
  53. #define AT91_SAMA5D2_MR_TRGSEL_TRIG5 5
  54. /* TIOA3 */
  55. #define AT91_SAMA5D2_MR_TRGSEL_TRIG6 6
  56. /* RTCOUT0 */
  57. #define AT91_SAMA5D2_MR_TRGSEL_TRIG7 7
  58. /* Sleep Mode */
  59. #define AT91_SAMA5D2_MR_SLEEP BIT(5)
  60. /* Fast Wake Up */
  61. #define AT91_SAMA5D2_MR_FWUP BIT(6)
  62. /* Prescaler Rate Selection */
  63. #define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET)
  64. #define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8
  65. #define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff
  66. /* Startup Time */
  67. #define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
  68. /* Analog Change */
  69. #define AT91_SAMA5D2_MR_ANACH BIT(23)
  70. /* Tracking Time */
  71. #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24)
  72. #define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xff
  73. /* Transfer Time */
  74. #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28)
  75. #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3
  76. /* Use Sequence Enable */
  77. #define AT91_SAMA5D2_MR_USEQ BIT(31)
  78. /* Channel Sequence Register 1 */
  79. #define AT91_SAMA5D2_SEQR1 0x08
  80. /* Channel Sequence Register 2 */
  81. #define AT91_SAMA5D2_SEQR2 0x0c
  82. /* Channel Enable Register */
  83. #define AT91_SAMA5D2_CHER 0x10
  84. /* Channel Disable Register */
  85. #define AT91_SAMA5D2_CHDR 0x14
  86. /* Channel Status Register */
  87. #define AT91_SAMA5D2_CHSR 0x18
  88. /* Last Converted Data Register */
  89. #define AT91_SAMA5D2_LCDR 0x20
  90. /* Interrupt Enable Register */
  91. #define AT91_SAMA5D2_IER 0x24
  92. /* Interrupt Disable Register */
  93. #define AT91_SAMA5D2_IDR 0x28
  94. /* Interrupt Mask Register */
  95. #define AT91_SAMA5D2_IMR 0x2c
  96. /* Interrupt Status Register */
  97. #define AT91_SAMA5D2_ISR 0x30
  98. /* Last Channel Trigger Mode Register */
  99. #define AT91_SAMA5D2_LCTMR 0x34
  100. /* Last Channel Compare Window Register */
  101. #define AT91_SAMA5D2_LCCWR 0x38
  102. /* Overrun Status Register */
  103. #define AT91_SAMA5D2_OVER 0x3c
  104. /* Extended Mode Register */
  105. #define AT91_SAMA5D2_EMR 0x40
  106. /* Compare Window Register */
  107. #define AT91_SAMA5D2_CWR 0x44
  108. /* Channel Gain Register */
  109. #define AT91_SAMA5D2_CGR 0x48
  110. /* Channel Offset Register */
  111. #define AT91_SAMA5D2_COR 0x4c
  112. /* Channel Data Register 0 */
  113. #define AT91_SAMA5D2_CDR0 0x50
  114. /* Analog Control Register */
  115. #define AT91_SAMA5D2_ACR 0x94
  116. /* Touchscreen Mode Register */
  117. #define AT91_SAMA5D2_TSMR 0xb0
  118. /* Touchscreen X Position Register */
  119. #define AT91_SAMA5D2_XPOSR 0xb4
  120. /* Touchscreen Y Position Register */
  121. #define AT91_SAMA5D2_YPOSR 0xb8
  122. /* Touchscreen Pressure Register */
  123. #define AT91_SAMA5D2_PRESSR 0xbc
  124. /* Trigger Register */
  125. #define AT91_SAMA5D2_TRGR 0xc0
  126. /* Correction Select Register */
  127. #define AT91_SAMA5D2_COSR 0xd0
  128. /* Correction Value Register */
  129. #define AT91_SAMA5D2_CVR 0xd4
  130. /* Channel Error Correction Register */
  131. #define AT91_SAMA5D2_CECR 0xd8
  132. /* Write Protection Mode Register */
  133. #define AT91_SAMA5D2_WPMR 0xe4
  134. /* Write Protection Status Register */
  135. #define AT91_SAMA5D2_WPSR 0xe8
  136. /* Version Register */
  137. #define AT91_SAMA5D2_VERSION 0xfc
  138. #define AT91_AT91_SAMA5D2_CHAN(num, addr) \
  139. { \
  140. .type = IIO_VOLTAGE, \
  141. .channel = num, \
  142. .address = addr, \
  143. .scan_type = { \
  144. .sign = 'u', \
  145. .realbits = 12, \
  146. }, \
  147. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  148. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  149. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
  150. .datasheet_name = "CH"#num, \
  151. .indexed = 1, \
  152. }
  153. #define at91_adc_readl(st, reg) readl_relaxed(st->base + reg)
  154. #define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg)
  155. struct at91_adc_soc_info {
  156. unsigned startup_time;
  157. unsigned min_sample_rate;
  158. unsigned max_sample_rate;
  159. };
  160. struct at91_adc_state {
  161. void __iomem *base;
  162. int irq;
  163. struct clk *per_clk;
  164. struct regulator *reg;
  165. struct regulator *vref;
  166. int vref_uv;
  167. const struct iio_chan_spec *chan;
  168. bool conversion_done;
  169. u32 conversion_value;
  170. struct at91_adc_soc_info soc_info;
  171. wait_queue_head_t wq_data_available;
  172. /*
  173. * lock to prevent concurrent 'single conversion' requests through
  174. * sysfs.
  175. */
  176. struct mutex lock;
  177. };
  178. static const struct iio_chan_spec at91_adc_channels[] = {
  179. AT91_AT91_SAMA5D2_CHAN(0, 0x50),
  180. AT91_AT91_SAMA5D2_CHAN(1, 0x54),
  181. AT91_AT91_SAMA5D2_CHAN(2, 0x58),
  182. AT91_AT91_SAMA5D2_CHAN(3, 0x5c),
  183. AT91_AT91_SAMA5D2_CHAN(4, 0x60),
  184. AT91_AT91_SAMA5D2_CHAN(5, 0x64),
  185. AT91_AT91_SAMA5D2_CHAN(6, 0x68),
  186. AT91_AT91_SAMA5D2_CHAN(7, 0x6c),
  187. AT91_AT91_SAMA5D2_CHAN(8, 0x70),
  188. AT91_AT91_SAMA5D2_CHAN(9, 0x74),
  189. AT91_AT91_SAMA5D2_CHAN(10, 0x78),
  190. AT91_AT91_SAMA5D2_CHAN(11, 0x7c),
  191. };
  192. static unsigned at91_adc_startup_time(unsigned startup_time_min,
  193. unsigned adc_clk_khz)
  194. {
  195. const unsigned startup_lookup[] = {
  196. 0, 8, 16, 24,
  197. 64, 80, 96, 112,
  198. 512, 576, 640, 704,
  199. 768, 832, 896, 960
  200. };
  201. unsigned ticks_min, i;
  202. /*
  203. * Since the adc frequency is checked before, there is no reason
  204. * to not meet the startup time constraint.
  205. */
  206. ticks_min = startup_time_min * adc_clk_khz / 1000;
  207. for (i = 0; i < ARRAY_SIZE(startup_lookup); i++)
  208. if (startup_lookup[i] > ticks_min)
  209. break;
  210. return i;
  211. }
  212. static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
  213. {
  214. struct iio_dev *indio_dev = iio_priv_to_dev(st);
  215. unsigned f_per, prescal, startup;
  216. f_per = clk_get_rate(st->per_clk);
  217. prescal = (f_per / (2 * freq)) - 1;
  218. startup = at91_adc_startup_time(st->soc_info.startup_time,
  219. freq / 1000);
  220. at91_adc_writel(st, AT91_SAMA5D2_MR,
  221. AT91_SAMA5D2_MR_TRANSFER(2)
  222. | AT91_SAMA5D2_MR_STARTUP(startup)
  223. | AT91_SAMA5D2_MR_PRESCAL(prescal));
  224. dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
  225. freq, startup, prescal);
  226. }
  227. static unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
  228. {
  229. unsigned f_adc, f_per = clk_get_rate(st->per_clk);
  230. unsigned mr, prescal;
  231. mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
  232. prescal = (mr >> AT91_SAMA5D2_MR_PRESCAL_OFFSET)
  233. & AT91_SAMA5D2_MR_PRESCAL_MAX;
  234. f_adc = f_per / (2 * (prescal + 1));
  235. return f_adc;
  236. }
  237. static irqreturn_t at91_adc_interrupt(int irq, void *private)
  238. {
  239. struct iio_dev *indio = private;
  240. struct at91_adc_state *st = iio_priv(indio);
  241. u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
  242. u32 imr = at91_adc_readl(st, AT91_SAMA5D2_IMR);
  243. if (status & imr) {
  244. st->conversion_value = at91_adc_readl(st, st->chan->address);
  245. st->conversion_done = true;
  246. wake_up_interruptible(&st->wq_data_available);
  247. return IRQ_HANDLED;
  248. }
  249. return IRQ_NONE;
  250. }
  251. static int at91_adc_read_raw(struct iio_dev *indio_dev,
  252. struct iio_chan_spec const *chan,
  253. int *val, int *val2, long mask)
  254. {
  255. struct at91_adc_state *st = iio_priv(indio_dev);
  256. int ret;
  257. switch (mask) {
  258. case IIO_CHAN_INFO_RAW:
  259. mutex_lock(&st->lock);
  260. st->chan = chan;
  261. at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel));
  262. at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel));
  263. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START);
  264. ret = wait_event_interruptible_timeout(st->wq_data_available,
  265. st->conversion_done,
  266. msecs_to_jiffies(1000));
  267. if (ret == 0)
  268. ret = -ETIMEDOUT;
  269. if (ret > 0) {
  270. *val = st->conversion_value;
  271. ret = IIO_VAL_INT;
  272. st->conversion_done = false;
  273. }
  274. at91_adc_writel(st, AT91_SAMA5D2_IDR, BIT(chan->channel));
  275. at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel));
  276. mutex_unlock(&st->lock);
  277. return ret;
  278. case IIO_CHAN_INFO_SCALE:
  279. *val = st->vref_uv / 1000;
  280. *val2 = chan->scan_type.realbits;
  281. return IIO_VAL_FRACTIONAL_LOG2;
  282. case IIO_CHAN_INFO_SAMP_FREQ:
  283. *val = at91_adc_get_sample_freq(st);
  284. return IIO_VAL_INT;
  285. default:
  286. return -EINVAL;
  287. }
  288. }
  289. static int at91_adc_write_raw(struct iio_dev *indio_dev,
  290. struct iio_chan_spec const *chan,
  291. int val, int val2, long mask)
  292. {
  293. struct at91_adc_state *st = iio_priv(indio_dev);
  294. if (mask != IIO_CHAN_INFO_SAMP_FREQ)
  295. return -EINVAL;
  296. if (val < st->soc_info.min_sample_rate ||
  297. val > st->soc_info.max_sample_rate)
  298. return -EINVAL;
  299. at91_adc_setup_samp_freq(st, val);
  300. return 0;
  301. }
  302. static const struct iio_info at91_adc_info = {
  303. .read_raw = &at91_adc_read_raw,
  304. .write_raw = &at91_adc_write_raw,
  305. .driver_module = THIS_MODULE,
  306. };
  307. static int at91_adc_probe(struct platform_device *pdev)
  308. {
  309. struct iio_dev *indio_dev;
  310. struct at91_adc_state *st;
  311. struct resource *res;
  312. int ret;
  313. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
  314. if (!indio_dev)
  315. return -ENOMEM;
  316. indio_dev->dev.parent = &pdev->dev;
  317. indio_dev->name = dev_name(&pdev->dev);
  318. indio_dev->modes = INDIO_DIRECT_MODE;
  319. indio_dev->info = &at91_adc_info;
  320. indio_dev->channels = at91_adc_channels;
  321. indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels);
  322. st = iio_priv(indio_dev);
  323. ret = of_property_read_u32(pdev->dev.of_node,
  324. "atmel,min-sample-rate-hz",
  325. &st->soc_info.min_sample_rate);
  326. if (ret) {
  327. dev_err(&pdev->dev,
  328. "invalid or missing value for atmel,min-sample-rate-hz\n");
  329. return ret;
  330. }
  331. ret = of_property_read_u32(pdev->dev.of_node,
  332. "atmel,max-sample-rate-hz",
  333. &st->soc_info.max_sample_rate);
  334. if (ret) {
  335. dev_err(&pdev->dev,
  336. "invalid or missing value for atmel,max-sample-rate-hz\n");
  337. return ret;
  338. }
  339. ret = of_property_read_u32(pdev->dev.of_node, "atmel,startup-time-ms",
  340. &st->soc_info.startup_time);
  341. if (ret) {
  342. dev_err(&pdev->dev,
  343. "invalid or missing value for atmel,startup-time-ms\n");
  344. return ret;
  345. }
  346. init_waitqueue_head(&st->wq_data_available);
  347. mutex_init(&st->lock);
  348. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  349. if (!res)
  350. return -EINVAL;
  351. st->base = devm_ioremap_resource(&pdev->dev, res);
  352. if (IS_ERR(st->base))
  353. return PTR_ERR(st->base);
  354. st->irq = platform_get_irq(pdev, 0);
  355. if (st->irq <= 0) {
  356. if (!st->irq)
  357. st->irq = -ENXIO;
  358. return st->irq;
  359. }
  360. st->per_clk = devm_clk_get(&pdev->dev, "adc_clk");
  361. if (IS_ERR(st->per_clk))
  362. return PTR_ERR(st->per_clk);
  363. st->reg = devm_regulator_get(&pdev->dev, "vddana");
  364. if (IS_ERR(st->reg))
  365. return PTR_ERR(st->reg);
  366. st->vref = devm_regulator_get(&pdev->dev, "vref");
  367. if (IS_ERR(st->vref))
  368. return PTR_ERR(st->vref);
  369. ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0,
  370. pdev->dev.driver->name, indio_dev);
  371. if (ret)
  372. return ret;
  373. ret = regulator_enable(st->reg);
  374. if (ret)
  375. return ret;
  376. ret = regulator_enable(st->vref);
  377. if (ret)
  378. goto reg_disable;
  379. st->vref_uv = regulator_get_voltage(st->vref);
  380. if (st->vref_uv <= 0) {
  381. ret = -EINVAL;
  382. goto vref_disable;
  383. }
  384. at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
  385. at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff);
  386. at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate);
  387. ret = clk_prepare_enable(st->per_clk);
  388. if (ret)
  389. goto vref_disable;
  390. ret = iio_device_register(indio_dev);
  391. if (ret < 0)
  392. goto per_clk_disable_unprepare;
  393. dev_info(&pdev->dev, "version: %x\n",
  394. readl_relaxed(st->base + AT91_SAMA5D2_VERSION));
  395. return 0;
  396. per_clk_disable_unprepare:
  397. clk_disable_unprepare(st->per_clk);
  398. vref_disable:
  399. regulator_disable(st->vref);
  400. reg_disable:
  401. regulator_disable(st->reg);
  402. return ret;
  403. }
  404. static int at91_adc_remove(struct platform_device *pdev)
  405. {
  406. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  407. struct at91_adc_state *st = iio_priv(indio_dev);
  408. iio_device_unregister(indio_dev);
  409. clk_disable_unprepare(st->per_clk);
  410. regulator_disable(st->vref);
  411. regulator_disable(st->reg);
  412. return 0;
  413. }
  414. static const struct of_device_id at91_adc_dt_match[] = {
  415. {
  416. .compatible = "atmel,sama5d2-adc",
  417. }, {
  418. /* sentinel */
  419. }
  420. };
  421. MODULE_DEVICE_TABLE(of, at91_adc_dt_match);
  422. static struct platform_driver at91_adc_driver = {
  423. .probe = at91_adc_probe,
  424. .remove = at91_adc_remove,
  425. .driver = {
  426. .name = "at91-sama5d2_adc",
  427. .of_match_table = at91_adc_dt_match,
  428. },
  429. };
  430. module_platform_driver(at91_adc_driver)
  431. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  432. MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
  433. MODULE_LICENSE("GPL v2");