at91_adc.c 19 KB

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