at91_adc.c 17 KB

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