at91_adc.c 17 KB

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