at91_adc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Driver for the ADC present in the Atmel AT91 evaluation boards.
  3. *
  4. * Copyright 2011 Free Electrons
  5. *
  6. * Licensed under the GPLv2 or later.
  7. */
  8. #include <linux/bitmap.h>
  9. #include <linux/bitops.h>
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/wait.h>
  23. #include <linux/platform_data/at91_adc.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/buffer.h>
  26. #include <linux/iio/trigger.h>
  27. #include <linux/iio/trigger_consumer.h>
  28. #include <linux/iio/triggered_buffer.h>
  29. #include <mach/at91_adc.h>
  30. #define AT91_ADC_CHAN(st, ch) \
  31. (st->registers->channel_base + (ch * 4))
  32. #define at91_adc_readl(st, reg) \
  33. (readl_relaxed(st->reg_base + reg))
  34. #define at91_adc_writel(st, reg, val) \
  35. (writel_relaxed(val, st->reg_base + reg))
  36. struct at91_adc_state {
  37. struct clk *adc_clk;
  38. u16 *buffer;
  39. unsigned long channels_mask;
  40. struct clk *clk;
  41. bool done;
  42. int irq;
  43. u16 last_value;
  44. struct mutex lock;
  45. u8 num_channels;
  46. void __iomem *reg_base;
  47. struct at91_adc_reg_desc *registers;
  48. u8 startup_time;
  49. struct iio_trigger **trig;
  50. struct at91_adc_trigger *trigger_list;
  51. u32 trigger_number;
  52. bool use_external;
  53. u32 vref_mv;
  54. wait_queue_head_t wq_data_avail;
  55. };
  56. static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
  57. {
  58. struct iio_poll_func *pf = p;
  59. struct iio_dev *idev = pf->indio_dev;
  60. struct at91_adc_state *st = iio_priv(idev);
  61. int i, j = 0;
  62. for (i = 0; i < idev->masklength; i++) {
  63. if (!test_bit(i, idev->active_scan_mask))
  64. continue;
  65. st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
  66. j++;
  67. }
  68. if (idev->scan_timestamp) {
  69. s64 *timestamp = (s64 *)((u8 *)st->buffer +
  70. ALIGN(j, sizeof(s64)));
  71. *timestamp = pf->timestamp;
  72. }
  73. iio_push_to_buffers(idev, (u8 *)st->buffer);
  74. iio_trigger_notify_done(idev->trig);
  75. /* Needed to ACK the DRDY interruption */
  76. at91_adc_readl(st, AT91_ADC_LCDR);
  77. enable_irq(st->irq);
  78. return IRQ_HANDLED;
  79. }
  80. static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
  81. {
  82. struct iio_dev *idev = private;
  83. struct at91_adc_state *st = iio_priv(idev);
  84. u32 status = at91_adc_readl(st, st->registers->status_register);
  85. if (!(status & st->registers->drdy_mask))
  86. return IRQ_HANDLED;
  87. if (iio_buffer_enabled(idev)) {
  88. disable_irq_nosync(irq);
  89. iio_trigger_poll(idev->trig, iio_get_time_ns());
  90. } else {
  91. st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
  92. st->done = true;
  93. wake_up_interruptible(&st->wq_data_avail);
  94. }
  95. return IRQ_HANDLED;
  96. }
  97. static int at91_adc_channel_init(struct iio_dev *idev)
  98. {
  99. struct at91_adc_state *st = iio_priv(idev);
  100. struct iio_chan_spec *chan_array, *timestamp;
  101. int bit, idx = 0;
  102. idev->num_channels = bitmap_weight(&st->channels_mask,
  103. st->num_channels) + 1;
  104. chan_array = devm_kzalloc(&idev->dev,
  105. ((idev->num_channels + 1) *
  106. sizeof(struct iio_chan_spec)),
  107. GFP_KERNEL);
  108. if (!chan_array)
  109. return -ENOMEM;
  110. for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
  111. struct iio_chan_spec *chan = chan_array + idx;
  112. chan->type = IIO_VOLTAGE;
  113. chan->indexed = 1;
  114. chan->channel = bit;
  115. chan->scan_index = idx;
  116. chan->scan_type.sign = 'u';
  117. chan->scan_type.realbits = 10;
  118. chan->scan_type.storagebits = 16;
  119. chan->info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT |
  120. IIO_CHAN_INFO_RAW_SEPARATE_BIT;
  121. idx++;
  122. }
  123. timestamp = chan_array + idx;
  124. timestamp->type = IIO_TIMESTAMP;
  125. timestamp->channel = -1;
  126. timestamp->scan_index = idx;
  127. timestamp->scan_type.sign = 's';
  128. timestamp->scan_type.realbits = 64;
  129. timestamp->scan_type.storagebits = 64;
  130. idev->channels = chan_array;
  131. return idev->num_channels;
  132. }
  133. static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
  134. struct at91_adc_trigger *triggers,
  135. const char *trigger_name)
  136. {
  137. struct at91_adc_state *st = iio_priv(idev);
  138. u8 value = 0;
  139. int i;
  140. for (i = 0; i < st->trigger_number; i++) {
  141. char *name = kasprintf(GFP_KERNEL,
  142. "%s-dev%d-%s",
  143. idev->name,
  144. idev->id,
  145. triggers[i].name);
  146. if (!name)
  147. return -ENOMEM;
  148. if (strcmp(trigger_name, name) == 0) {
  149. value = triggers[i].value;
  150. kfree(name);
  151. break;
  152. }
  153. kfree(name);
  154. }
  155. return value;
  156. }
  157. static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
  158. {
  159. struct iio_dev *idev = trig->private_data;
  160. struct at91_adc_state *st = iio_priv(idev);
  161. struct iio_buffer *buffer = idev->buffer;
  162. struct at91_adc_reg_desc *reg = st->registers;
  163. u32 status = at91_adc_readl(st, reg->trigger_register);
  164. u8 value;
  165. u8 bit;
  166. value = at91_adc_get_trigger_value_by_name(idev,
  167. st->trigger_list,
  168. idev->trig->name);
  169. if (value == 0)
  170. return -EINVAL;
  171. if (state) {
  172. st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
  173. if (st->buffer == NULL)
  174. return -ENOMEM;
  175. at91_adc_writel(st, reg->trigger_register,
  176. status | value);
  177. for_each_set_bit(bit, buffer->scan_mask,
  178. st->num_channels) {
  179. struct iio_chan_spec const *chan = idev->channels + bit;
  180. at91_adc_writel(st, AT91_ADC_CHER,
  181. AT91_ADC_CH(chan->channel));
  182. }
  183. at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
  184. } else {
  185. at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
  186. at91_adc_writel(st, reg->trigger_register,
  187. status & ~value);
  188. for_each_set_bit(bit, buffer->scan_mask,
  189. st->num_channels) {
  190. struct iio_chan_spec const *chan = idev->channels + bit;
  191. at91_adc_writel(st, AT91_ADC_CHDR,
  192. AT91_ADC_CH(chan->channel));
  193. }
  194. kfree(st->buffer);
  195. }
  196. return 0;
  197. }
  198. static const struct iio_trigger_ops at91_adc_trigger_ops = {
  199. .owner = THIS_MODULE,
  200. .set_trigger_state = &at91_adc_configure_trigger,
  201. };
  202. static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
  203. struct at91_adc_trigger *trigger)
  204. {
  205. struct iio_trigger *trig;
  206. int ret;
  207. trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
  208. idev->id, trigger->name);
  209. if (trig == NULL)
  210. return NULL;
  211. trig->dev.parent = idev->dev.parent;
  212. trig->private_data = idev;
  213. trig->ops = &at91_adc_trigger_ops;
  214. ret = iio_trigger_register(trig);
  215. if (ret)
  216. return NULL;
  217. return trig;
  218. }
  219. static int at91_adc_trigger_init(struct iio_dev *idev)
  220. {
  221. struct at91_adc_state *st = iio_priv(idev);
  222. int i, ret;
  223. st->trig = devm_kzalloc(&idev->dev,
  224. st->trigger_number * sizeof(st->trig),
  225. GFP_KERNEL);
  226. if (st->trig == NULL) {
  227. ret = -ENOMEM;
  228. goto error_ret;
  229. }
  230. for (i = 0; i < st->trigger_number; i++) {
  231. if (st->trigger_list[i].is_external && !(st->use_external))
  232. continue;
  233. st->trig[i] = at91_adc_allocate_trigger(idev,
  234. st->trigger_list + i);
  235. if (st->trig[i] == NULL) {
  236. dev_err(&idev->dev,
  237. "Could not allocate trigger %d\n", i);
  238. ret = -ENOMEM;
  239. goto error_trigger;
  240. }
  241. }
  242. return 0;
  243. error_trigger:
  244. for (i--; i >= 0; i--) {
  245. iio_trigger_unregister(st->trig[i]);
  246. iio_trigger_free(st->trig[i]);
  247. }
  248. error_ret:
  249. return ret;
  250. }
  251. static void at91_adc_trigger_remove(struct iio_dev *idev)
  252. {
  253. struct at91_adc_state *st = iio_priv(idev);
  254. int i;
  255. for (i = 0; i < st->trigger_number; i++) {
  256. iio_trigger_unregister(st->trig[i]);
  257. iio_trigger_free(st->trig[i]);
  258. }
  259. }
  260. static int at91_adc_buffer_init(struct iio_dev *idev)
  261. {
  262. return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
  263. &at91_adc_trigger_handler, NULL);
  264. }
  265. static void at91_adc_buffer_remove(struct iio_dev *idev)
  266. {
  267. iio_triggered_buffer_cleanup(idev);
  268. }
  269. static int at91_adc_read_raw(struct iio_dev *idev,
  270. struct iio_chan_spec const *chan,
  271. int *val, int *val2, long mask)
  272. {
  273. struct at91_adc_state *st = iio_priv(idev);
  274. int ret;
  275. switch (mask) {
  276. case IIO_CHAN_INFO_RAW:
  277. mutex_lock(&st->lock);
  278. at91_adc_writel(st, AT91_ADC_CHER,
  279. AT91_ADC_CH(chan->channel));
  280. at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
  281. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
  282. ret = wait_event_interruptible_timeout(st->wq_data_avail,
  283. st->done,
  284. msecs_to_jiffies(1000));
  285. if (ret == 0)
  286. ret = -ETIMEDOUT;
  287. if (ret < 0) {
  288. mutex_unlock(&st->lock);
  289. return ret;
  290. }
  291. *val = st->last_value;
  292. at91_adc_writel(st, AT91_ADC_CHDR,
  293. AT91_ADC_CH(chan->channel));
  294. at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
  295. st->last_value = 0;
  296. st->done = false;
  297. mutex_unlock(&st->lock);
  298. return IIO_VAL_INT;
  299. case IIO_CHAN_INFO_SCALE:
  300. *val = (st->vref_mv * 1000) >> chan->scan_type.realbits;
  301. *val2 = 0;
  302. return IIO_VAL_INT_PLUS_MICRO;
  303. default:
  304. break;
  305. }
  306. return -EINVAL;
  307. }
  308. static int at91_adc_probe_dt(struct at91_adc_state *st,
  309. struct platform_device *pdev)
  310. {
  311. struct iio_dev *idev = iio_priv_to_dev(st);
  312. struct device_node *node = pdev->dev.of_node;
  313. struct device_node *trig_node;
  314. int i = 0, ret;
  315. u32 prop;
  316. if (!node)
  317. return -EINVAL;
  318. st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
  319. if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
  320. dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
  321. ret = -EINVAL;
  322. goto error_ret;
  323. }
  324. st->channels_mask = prop;
  325. if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) {
  326. dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n");
  327. ret = -EINVAL;
  328. goto error_ret;
  329. }
  330. st->num_channels = prop;
  331. if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
  332. dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
  333. ret = -EINVAL;
  334. goto error_ret;
  335. }
  336. st->startup_time = prop;
  337. if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
  338. dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
  339. ret = -EINVAL;
  340. goto error_ret;
  341. }
  342. st->vref_mv = prop;
  343. st->registers = devm_kzalloc(&idev->dev,
  344. sizeof(struct at91_adc_reg_desc),
  345. GFP_KERNEL);
  346. if (!st->registers) {
  347. dev_err(&idev->dev, "Could not allocate register memory.\n");
  348. ret = -ENOMEM;
  349. goto error_ret;
  350. }
  351. if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) {
  352. dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n");
  353. ret = -EINVAL;
  354. goto error_ret;
  355. }
  356. st->registers->channel_base = prop;
  357. if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) {
  358. dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n");
  359. ret = -EINVAL;
  360. goto error_ret;
  361. }
  362. st->registers->drdy_mask = prop;
  363. if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) {
  364. dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n");
  365. ret = -EINVAL;
  366. goto error_ret;
  367. }
  368. st->registers->status_register = prop;
  369. if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) {
  370. dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n");
  371. ret = -EINVAL;
  372. goto error_ret;
  373. }
  374. st->registers->trigger_register = prop;
  375. st->trigger_number = of_get_child_count(node);
  376. st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
  377. sizeof(struct at91_adc_trigger),
  378. GFP_KERNEL);
  379. if (!st->trigger_list) {
  380. dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
  381. ret = -ENOMEM;
  382. goto error_ret;
  383. }
  384. for_each_child_of_node(node, trig_node) {
  385. struct at91_adc_trigger *trig = st->trigger_list + i;
  386. const char *name;
  387. if (of_property_read_string(trig_node, "trigger-name", &name)) {
  388. dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
  389. ret = -EINVAL;
  390. goto error_ret;
  391. }
  392. trig->name = name;
  393. if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
  394. dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
  395. ret = -EINVAL;
  396. goto error_ret;
  397. }
  398. trig->value = prop;
  399. trig->is_external = of_property_read_bool(trig_node, "trigger-external");
  400. i++;
  401. }
  402. return 0;
  403. error_ret:
  404. return ret;
  405. }
  406. static int at91_adc_probe_pdata(struct at91_adc_state *st,
  407. struct platform_device *pdev)
  408. {
  409. struct at91_adc_data *pdata = pdev->dev.platform_data;
  410. if (!pdata)
  411. return -EINVAL;
  412. st->use_external = pdata->use_external_triggers;
  413. st->vref_mv = pdata->vref;
  414. st->channels_mask = pdata->channels_used;
  415. st->num_channels = pdata->num_channels;
  416. st->startup_time = pdata->startup_time;
  417. st->trigger_number = pdata->trigger_number;
  418. st->trigger_list = pdata->trigger_list;
  419. st->registers = pdata->registers;
  420. return 0;
  421. }
  422. static const struct iio_info at91_adc_info = {
  423. .driver_module = THIS_MODULE,
  424. .read_raw = &at91_adc_read_raw,
  425. };
  426. static int at91_adc_probe(struct platform_device *pdev)
  427. {
  428. unsigned int prsc, mstrclk, ticks, adc_clk;
  429. int ret;
  430. struct iio_dev *idev;
  431. struct at91_adc_state *st;
  432. struct resource *res;
  433. idev = iio_device_alloc(sizeof(struct at91_adc_state));
  434. if (idev == NULL) {
  435. ret = -ENOMEM;
  436. goto error_ret;
  437. }
  438. st = iio_priv(idev);
  439. if (pdev->dev.of_node)
  440. ret = at91_adc_probe_dt(st, pdev);
  441. else
  442. ret = at91_adc_probe_pdata(st, pdev);
  443. if (ret) {
  444. dev_err(&pdev->dev, "No platform data available.\n");
  445. ret = -EINVAL;
  446. goto error_free_device;
  447. }
  448. platform_set_drvdata(pdev, idev);
  449. idev->dev.parent = &pdev->dev;
  450. idev->name = dev_name(&pdev->dev);
  451. idev->modes = INDIO_DIRECT_MODE;
  452. idev->info = &at91_adc_info;
  453. st->irq = platform_get_irq(pdev, 0);
  454. if (st->irq < 0) {
  455. dev_err(&pdev->dev, "No IRQ ID is designated\n");
  456. ret = -ENODEV;
  457. goto error_free_device;
  458. }
  459. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  460. st->reg_base = devm_ioremap_resource(&pdev->dev, res);
  461. if (IS_ERR(st->reg_base)) {
  462. ret = PTR_ERR(st->reg_base);
  463. goto error_free_device;
  464. }
  465. /*
  466. * Disable all IRQs before setting up the handler
  467. */
  468. at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
  469. at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
  470. ret = request_irq(st->irq,
  471. at91_adc_eoc_trigger,
  472. 0,
  473. pdev->dev.driver->name,
  474. idev);
  475. if (ret) {
  476. dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
  477. goto error_free_device;
  478. }
  479. st->clk = devm_clk_get(&pdev->dev, "adc_clk");
  480. if (IS_ERR(st->clk)) {
  481. dev_err(&pdev->dev, "Failed to get the clock.\n");
  482. ret = PTR_ERR(st->clk);
  483. goto error_free_irq;
  484. }
  485. ret = clk_prepare_enable(st->clk);
  486. if (ret) {
  487. dev_err(&pdev->dev,
  488. "Could not prepare or enable the clock.\n");
  489. goto error_free_irq;
  490. }
  491. st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
  492. if (IS_ERR(st->adc_clk)) {
  493. dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
  494. ret = PTR_ERR(st->adc_clk);
  495. goto error_disable_clk;
  496. }
  497. ret = clk_prepare_enable(st->adc_clk);
  498. if (ret) {
  499. dev_err(&pdev->dev,
  500. "Could not prepare or enable the ADC clock.\n");
  501. goto error_disable_clk;
  502. }
  503. /*
  504. * Prescaler rate computation using the formula from the Atmel's
  505. * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
  506. * specified by the electrical characteristics of the board.
  507. */
  508. mstrclk = clk_get_rate(st->clk);
  509. adc_clk = clk_get_rate(st->adc_clk);
  510. prsc = (mstrclk / (2 * adc_clk)) - 1;
  511. if (!st->startup_time) {
  512. dev_err(&pdev->dev, "No startup time available.\n");
  513. ret = -EINVAL;
  514. goto error_disable_adc_clk;
  515. }
  516. /*
  517. * Number of ticks needed to cover the startup time of the ADC as
  518. * defined in the electrical characteristics of the board, divided by 8.
  519. * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
  520. */
  521. ticks = round_up((st->startup_time * adc_clk /
  522. 1000000) - 1, 8) / 8;
  523. at91_adc_writel(st, AT91_ADC_MR,
  524. (AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL) |
  525. (AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP));
  526. /* Setup the ADC channels available on the board */
  527. ret = at91_adc_channel_init(idev);
  528. if (ret < 0) {
  529. dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
  530. goto error_disable_adc_clk;
  531. }
  532. init_waitqueue_head(&st->wq_data_avail);
  533. mutex_init(&st->lock);
  534. ret = at91_adc_buffer_init(idev);
  535. if (ret < 0) {
  536. dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
  537. goto error_disable_adc_clk;
  538. }
  539. ret = at91_adc_trigger_init(idev);
  540. if (ret < 0) {
  541. dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
  542. goto error_unregister_buffer;
  543. }
  544. ret = iio_device_register(idev);
  545. if (ret < 0) {
  546. dev_err(&pdev->dev, "Couldn't register the device.\n");
  547. goto error_remove_triggers;
  548. }
  549. return 0;
  550. error_remove_triggers:
  551. at91_adc_trigger_remove(idev);
  552. error_unregister_buffer:
  553. at91_adc_buffer_remove(idev);
  554. error_disable_adc_clk:
  555. clk_disable_unprepare(st->adc_clk);
  556. error_disable_clk:
  557. clk_disable_unprepare(st->clk);
  558. error_free_irq:
  559. free_irq(st->irq, idev);
  560. error_free_device:
  561. iio_device_free(idev);
  562. error_ret:
  563. return ret;
  564. }
  565. static int at91_adc_remove(struct platform_device *pdev)
  566. {
  567. struct iio_dev *idev = platform_get_drvdata(pdev);
  568. struct at91_adc_state *st = iio_priv(idev);
  569. iio_device_unregister(idev);
  570. at91_adc_trigger_remove(idev);
  571. at91_adc_buffer_remove(idev);
  572. clk_disable_unprepare(st->adc_clk);
  573. clk_disable_unprepare(st->clk);
  574. free_irq(st->irq, idev);
  575. iio_device_free(idev);
  576. return 0;
  577. }
  578. static const struct of_device_id at91_adc_dt_ids[] = {
  579. { .compatible = "atmel,at91sam9260-adc" },
  580. {},
  581. };
  582. MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
  583. static struct platform_driver at91_adc_driver = {
  584. .probe = at91_adc_probe,
  585. .remove = at91_adc_remove,
  586. .driver = {
  587. .name = "at91_adc",
  588. .of_match_table = of_match_ptr(at91_adc_dt_ids),
  589. },
  590. };
  591. module_platform_driver(at91_adc_driver);
  592. MODULE_LICENSE("GPL");
  593. MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
  594. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");