at91_adc.c 20 KB

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