mxs-lradc-adc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * Freescale MXS LRADC ADC driver
  3. *
  4. * Copyright (c) 2012 DENX Software Engineering, GmbH.
  5. * Copyright (c) 2017 Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
  6. *
  7. * Authors:
  8. * Marek Vasut <marex@denx.de>
  9. * Ksenija Stanojevic <ksenija.stanojevic@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/completion.h>
  22. #include <linux/device.h>
  23. #include <linux/err.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mfd/mxs-lradc.h>
  27. #include <linux/module.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/iio/buffer.h>
  32. #include <linux/iio/iio.h>
  33. #include <linux/iio/trigger.h>
  34. #include <linux/iio/trigger_consumer.h>
  35. #include <linux/iio/triggered_buffer.h>
  36. #include <linux/iio/sysfs.h>
  37. /*
  38. * Make this runtime configurable if necessary. Currently, if the buffered mode
  39. * is enabled, the LRADC takes LRADC_DELAY_TIMER_LOOP samples of data before
  40. * triggering IRQ. The sampling happens every (LRADC_DELAY_TIMER_PER / 2000)
  41. * seconds. The result is that the samples arrive every 500mS.
  42. */
  43. #define LRADC_DELAY_TIMER_PER 200
  44. #define LRADC_DELAY_TIMER_LOOP 5
  45. #define VREF_MV_BASE 1850
  46. static const char *mx23_lradc_adc_irq_names[] = {
  47. "mxs-lradc-channel0",
  48. "mxs-lradc-channel1",
  49. "mxs-lradc-channel2",
  50. "mxs-lradc-channel3",
  51. "mxs-lradc-channel4",
  52. "mxs-lradc-channel5",
  53. };
  54. static const char *mx28_lradc_adc_irq_names[] = {
  55. "mxs-lradc-thresh0",
  56. "mxs-lradc-thresh1",
  57. "mxs-lradc-channel0",
  58. "mxs-lradc-channel1",
  59. "mxs-lradc-channel2",
  60. "mxs-lradc-channel3",
  61. "mxs-lradc-channel4",
  62. "mxs-lradc-channel5",
  63. "mxs-lradc-button0",
  64. "mxs-lradc-button1",
  65. };
  66. static const u32 mxs_lradc_adc_vref_mv[][LRADC_MAX_TOTAL_CHANS] = {
  67. [IMX23_LRADC] = {
  68. VREF_MV_BASE, /* CH0 */
  69. VREF_MV_BASE, /* CH1 */
  70. VREF_MV_BASE, /* CH2 */
  71. VREF_MV_BASE, /* CH3 */
  72. VREF_MV_BASE, /* CH4 */
  73. VREF_MV_BASE, /* CH5 */
  74. VREF_MV_BASE * 2, /* CH6 VDDIO */
  75. VREF_MV_BASE * 4, /* CH7 VBATT */
  76. VREF_MV_BASE, /* CH8 Temp sense 0 */
  77. VREF_MV_BASE, /* CH9 Temp sense 1 */
  78. VREF_MV_BASE, /* CH10 */
  79. VREF_MV_BASE, /* CH11 */
  80. VREF_MV_BASE, /* CH12 USB_DP */
  81. VREF_MV_BASE, /* CH13 USB_DN */
  82. VREF_MV_BASE, /* CH14 VBG */
  83. VREF_MV_BASE * 4, /* CH15 VDD5V */
  84. },
  85. [IMX28_LRADC] = {
  86. VREF_MV_BASE, /* CH0 */
  87. VREF_MV_BASE, /* CH1 */
  88. VREF_MV_BASE, /* CH2 */
  89. VREF_MV_BASE, /* CH3 */
  90. VREF_MV_BASE, /* CH4 */
  91. VREF_MV_BASE, /* CH5 */
  92. VREF_MV_BASE, /* CH6 */
  93. VREF_MV_BASE * 4, /* CH7 VBATT */
  94. VREF_MV_BASE, /* CH8 Temp sense 0 */
  95. VREF_MV_BASE, /* CH9 Temp sense 1 */
  96. VREF_MV_BASE * 2, /* CH10 VDDIO */
  97. VREF_MV_BASE, /* CH11 VTH */
  98. VREF_MV_BASE * 2, /* CH12 VDDA */
  99. VREF_MV_BASE, /* CH13 VDDD */
  100. VREF_MV_BASE, /* CH14 VBG */
  101. VREF_MV_BASE * 4, /* CH15 VDD5V */
  102. },
  103. };
  104. enum mxs_lradc_divbytwo {
  105. MXS_LRADC_DIV_DISABLED = 0,
  106. MXS_LRADC_DIV_ENABLED,
  107. };
  108. struct mxs_lradc_scale {
  109. unsigned int integer;
  110. unsigned int nano;
  111. };
  112. struct mxs_lradc_adc {
  113. struct mxs_lradc *lradc;
  114. struct device *dev;
  115. void __iomem *base;
  116. u32 buffer[10];
  117. struct iio_trigger *trig;
  118. struct completion completion;
  119. spinlock_t lock;
  120. const u32 *vref_mv;
  121. struct mxs_lradc_scale scale_avail[LRADC_MAX_TOTAL_CHANS][2];
  122. unsigned long is_divided;
  123. };
  124. /* Raw I/O operations */
  125. static int mxs_lradc_adc_read_single(struct iio_dev *iio_dev, int chan,
  126. int *val)
  127. {
  128. struct mxs_lradc_adc *adc = iio_priv(iio_dev);
  129. struct mxs_lradc *lradc = adc->lradc;
  130. int ret;
  131. /*
  132. * See if there is no buffered operation in progress. If there is simply
  133. * bail out. This can be improved to support both buffered and raw IO at
  134. * the same time, yet the code becomes horribly complicated. Therefore I
  135. * applied KISS principle here.
  136. */
  137. ret = iio_device_claim_direct_mode(iio_dev);
  138. if (ret)
  139. return ret;
  140. reinit_completion(&adc->completion);
  141. /*
  142. * No buffered operation in progress, map the channel and trigger it.
  143. * Virtual channel 0 is always used here as the others are always not
  144. * used if doing raw sampling.
  145. */
  146. if (lradc->soc == IMX28_LRADC)
  147. writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
  148. adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
  149. writel(0x1, adc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
  150. /* Enable / disable the divider per requirement */
  151. if (test_bit(chan, &adc->is_divided))
  152. writel(1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET,
  153. adc->base + LRADC_CTRL2 + STMP_OFFSET_REG_SET);
  154. else
  155. writel(1 << LRADC_CTRL2_DIVIDE_BY_TWO_OFFSET,
  156. adc->base + LRADC_CTRL2 + STMP_OFFSET_REG_CLR);
  157. /* Clean the slot's previous content, then set new one. */
  158. writel(LRADC_CTRL4_LRADCSELECT_MASK(0),
  159. adc->base + LRADC_CTRL4 + STMP_OFFSET_REG_CLR);
  160. writel(chan, adc->base + LRADC_CTRL4 + STMP_OFFSET_REG_SET);
  161. writel(0, adc->base + LRADC_CH(0));
  162. /* Enable the IRQ and start sampling the channel. */
  163. writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
  164. adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET);
  165. writel(BIT(0), adc->base + LRADC_CTRL0 + STMP_OFFSET_REG_SET);
  166. /* Wait for completion on the channel, 1 second max. */
  167. ret = wait_for_completion_killable_timeout(&adc->completion, HZ);
  168. if (!ret)
  169. ret = -ETIMEDOUT;
  170. if (ret < 0)
  171. goto err;
  172. /* Read the data. */
  173. *val = readl(adc->base + LRADC_CH(0)) & LRADC_CH_VALUE_MASK;
  174. ret = IIO_VAL_INT;
  175. err:
  176. writel(LRADC_CTRL1_LRADC_IRQ_EN(0),
  177. adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
  178. iio_device_release_direct_mode(iio_dev);
  179. return ret;
  180. }
  181. static int mxs_lradc_adc_read_temp(struct iio_dev *iio_dev, int *val)
  182. {
  183. int ret, min, max;
  184. ret = mxs_lradc_adc_read_single(iio_dev, 8, &min);
  185. if (ret != IIO_VAL_INT)
  186. return ret;
  187. ret = mxs_lradc_adc_read_single(iio_dev, 9, &max);
  188. if (ret != IIO_VAL_INT)
  189. return ret;
  190. *val = max - min;
  191. return IIO_VAL_INT;
  192. }
  193. static int mxs_lradc_adc_read_raw(struct iio_dev *iio_dev,
  194. const struct iio_chan_spec *chan,
  195. int *val, int *val2, long m)
  196. {
  197. struct mxs_lradc_adc *adc = iio_priv(iio_dev);
  198. switch (m) {
  199. case IIO_CHAN_INFO_RAW:
  200. if (chan->type == IIO_TEMP)
  201. return mxs_lradc_adc_read_temp(iio_dev, val);
  202. return mxs_lradc_adc_read_single(iio_dev, chan->channel, val);
  203. case IIO_CHAN_INFO_SCALE:
  204. if (chan->type == IIO_TEMP) {
  205. /*
  206. * From the datasheet, we have to multiply by 1.012 and
  207. * divide by 4
  208. */
  209. *val = 0;
  210. *val2 = 253000;
  211. return IIO_VAL_INT_PLUS_MICRO;
  212. }
  213. *val = adc->vref_mv[chan->channel];
  214. *val2 = chan->scan_type.realbits -
  215. test_bit(chan->channel, &adc->is_divided);
  216. return IIO_VAL_FRACTIONAL_LOG2;
  217. case IIO_CHAN_INFO_OFFSET:
  218. if (chan->type == IIO_TEMP) {
  219. /*
  220. * The calculated value from the ADC is in Kelvin, we
  221. * want Celsius for hwmon so the offset is -273.15
  222. * The offset is applied before scaling so it is
  223. * actually -213.15 * 4 / 1.012 = -1079.644268
  224. */
  225. *val = -1079;
  226. *val2 = 644268;
  227. return IIO_VAL_INT_PLUS_MICRO;
  228. }
  229. return -EINVAL;
  230. default:
  231. break;
  232. }
  233. return -EINVAL;
  234. }
  235. static int mxs_lradc_adc_write_raw(struct iio_dev *iio_dev,
  236. const struct iio_chan_spec *chan,
  237. int val, int val2, long m)
  238. {
  239. struct mxs_lradc_adc *adc = iio_priv(iio_dev);
  240. struct mxs_lradc_scale *scale_avail =
  241. adc->scale_avail[chan->channel];
  242. int ret;
  243. ret = iio_device_claim_direct_mode(iio_dev);
  244. if (ret)
  245. return ret;
  246. switch (m) {
  247. case IIO_CHAN_INFO_SCALE:
  248. ret = -EINVAL;
  249. if (val == scale_avail[MXS_LRADC_DIV_DISABLED].integer &&
  250. val2 == scale_avail[MXS_LRADC_DIV_DISABLED].nano) {
  251. /* divider by two disabled */
  252. clear_bit(chan->channel, &adc->is_divided);
  253. ret = 0;
  254. } else if (val == scale_avail[MXS_LRADC_DIV_ENABLED].integer &&
  255. val2 == scale_avail[MXS_LRADC_DIV_ENABLED].nano) {
  256. /* divider by two enabled */
  257. set_bit(chan->channel, &adc->is_divided);
  258. ret = 0;
  259. }
  260. break;
  261. default:
  262. ret = -EINVAL;
  263. break;
  264. }
  265. iio_device_release_direct_mode(iio_dev);
  266. return ret;
  267. }
  268. static int mxs_lradc_adc_write_raw_get_fmt(struct iio_dev *iio_dev,
  269. const struct iio_chan_spec *chan,
  270. long m)
  271. {
  272. return IIO_VAL_INT_PLUS_NANO;
  273. }
  274. static ssize_t mxs_lradc_adc_show_scale_avail(struct device *dev,
  275. struct device_attribute *attr,
  276. char *buf)
  277. {
  278. struct iio_dev *iio = dev_to_iio_dev(dev);
  279. struct mxs_lradc_adc *adc = iio_priv(iio);
  280. struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
  281. int i, ch, len = 0;
  282. ch = iio_attr->address;
  283. for (i = 0; i < ARRAY_SIZE(adc->scale_avail[ch]); i++)
  284. len += sprintf(buf + len, "%u.%09u ",
  285. adc->scale_avail[ch][i].integer,
  286. adc->scale_avail[ch][i].nano);
  287. len += sprintf(buf + len, "\n");
  288. return len;
  289. }
  290. #define SHOW_SCALE_AVAILABLE_ATTR(ch)\
  291. IIO_DEVICE_ATTR(in_voltage##ch##_scale_available, 0444,\
  292. mxs_lradc_adc_show_scale_avail, NULL, ch)
  293. static SHOW_SCALE_AVAILABLE_ATTR(0);
  294. static SHOW_SCALE_AVAILABLE_ATTR(1);
  295. static SHOW_SCALE_AVAILABLE_ATTR(2);
  296. static SHOW_SCALE_AVAILABLE_ATTR(3);
  297. static SHOW_SCALE_AVAILABLE_ATTR(4);
  298. static SHOW_SCALE_AVAILABLE_ATTR(5);
  299. static SHOW_SCALE_AVAILABLE_ATTR(6);
  300. static SHOW_SCALE_AVAILABLE_ATTR(7);
  301. static SHOW_SCALE_AVAILABLE_ATTR(10);
  302. static SHOW_SCALE_AVAILABLE_ATTR(11);
  303. static SHOW_SCALE_AVAILABLE_ATTR(12);
  304. static SHOW_SCALE_AVAILABLE_ATTR(13);
  305. static SHOW_SCALE_AVAILABLE_ATTR(14);
  306. static SHOW_SCALE_AVAILABLE_ATTR(15);
  307. static struct attribute *mxs_lradc_adc_attributes[] = {
  308. &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
  309. &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
  310. &iio_dev_attr_in_voltage2_scale_available.dev_attr.attr,
  311. &iio_dev_attr_in_voltage3_scale_available.dev_attr.attr,
  312. &iio_dev_attr_in_voltage4_scale_available.dev_attr.attr,
  313. &iio_dev_attr_in_voltage5_scale_available.dev_attr.attr,
  314. &iio_dev_attr_in_voltage6_scale_available.dev_attr.attr,
  315. &iio_dev_attr_in_voltage7_scale_available.dev_attr.attr,
  316. &iio_dev_attr_in_voltage10_scale_available.dev_attr.attr,
  317. &iio_dev_attr_in_voltage11_scale_available.dev_attr.attr,
  318. &iio_dev_attr_in_voltage12_scale_available.dev_attr.attr,
  319. &iio_dev_attr_in_voltage13_scale_available.dev_attr.attr,
  320. &iio_dev_attr_in_voltage14_scale_available.dev_attr.attr,
  321. &iio_dev_attr_in_voltage15_scale_available.dev_attr.attr,
  322. NULL
  323. };
  324. static const struct attribute_group mxs_lradc_adc_attribute_group = {
  325. .attrs = mxs_lradc_adc_attributes,
  326. };
  327. static const struct iio_info mxs_lradc_adc_iio_info = {
  328. .read_raw = mxs_lradc_adc_read_raw,
  329. .write_raw = mxs_lradc_adc_write_raw,
  330. .write_raw_get_fmt = mxs_lradc_adc_write_raw_get_fmt,
  331. .attrs = &mxs_lradc_adc_attribute_group,
  332. };
  333. /* IRQ Handling */
  334. static irqreturn_t mxs_lradc_adc_handle_irq(int irq, void *data)
  335. {
  336. struct iio_dev *iio = data;
  337. struct mxs_lradc_adc *adc = iio_priv(iio);
  338. struct mxs_lradc *lradc = adc->lradc;
  339. unsigned long reg = readl(adc->base + LRADC_CTRL1);
  340. unsigned long flags;
  341. if (!(reg & mxs_lradc_irq_mask(lradc)))
  342. return IRQ_NONE;
  343. if (iio_buffer_enabled(iio)) {
  344. if (reg & lradc->buffer_vchans) {
  345. spin_lock_irqsave(&adc->lock, flags);
  346. iio_trigger_poll(iio->trig);
  347. spin_unlock_irqrestore(&adc->lock, flags);
  348. }
  349. } else if (reg & LRADC_CTRL1_LRADC_IRQ(0)) {
  350. complete(&adc->completion);
  351. }
  352. writel(reg & mxs_lradc_irq_mask(lradc),
  353. adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
  354. return IRQ_HANDLED;
  355. }
  356. /* Trigger handling */
  357. static irqreturn_t mxs_lradc_adc_trigger_handler(int irq, void *p)
  358. {
  359. struct iio_poll_func *pf = p;
  360. struct iio_dev *iio = pf->indio_dev;
  361. struct mxs_lradc_adc *adc = iio_priv(iio);
  362. const u32 chan_value = LRADC_CH_ACCUMULATE |
  363. ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
  364. unsigned int i, j = 0;
  365. for_each_set_bit(i, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
  366. adc->buffer[j] = readl(adc->base + LRADC_CH(j));
  367. writel(chan_value, adc->base + LRADC_CH(j));
  368. adc->buffer[j] &= LRADC_CH_VALUE_MASK;
  369. adc->buffer[j] /= LRADC_DELAY_TIMER_LOOP;
  370. j++;
  371. }
  372. iio_push_to_buffers_with_timestamp(iio, adc->buffer, pf->timestamp);
  373. iio_trigger_notify_done(iio->trig);
  374. return IRQ_HANDLED;
  375. }
  376. static int mxs_lradc_adc_configure_trigger(struct iio_trigger *trig, bool state)
  377. {
  378. struct iio_dev *iio = iio_trigger_get_drvdata(trig);
  379. struct mxs_lradc_adc *adc = iio_priv(iio);
  380. const u32 st = state ? STMP_OFFSET_REG_SET : STMP_OFFSET_REG_CLR;
  381. writel(LRADC_DELAY_KICK, adc->base + (LRADC_DELAY(0) + st));
  382. return 0;
  383. }
  384. static const struct iio_trigger_ops mxs_lradc_adc_trigger_ops = {
  385. .set_trigger_state = &mxs_lradc_adc_configure_trigger,
  386. };
  387. static int mxs_lradc_adc_trigger_init(struct iio_dev *iio)
  388. {
  389. int ret;
  390. struct iio_trigger *trig;
  391. struct mxs_lradc_adc *adc = iio_priv(iio);
  392. trig = devm_iio_trigger_alloc(&iio->dev, "%s-dev%i", iio->name,
  393. iio->id);
  394. trig->dev.parent = adc->dev;
  395. iio_trigger_set_drvdata(trig, iio);
  396. trig->ops = &mxs_lradc_adc_trigger_ops;
  397. ret = iio_trigger_register(trig);
  398. if (ret)
  399. return ret;
  400. adc->trig = trig;
  401. return 0;
  402. }
  403. static void mxs_lradc_adc_trigger_remove(struct iio_dev *iio)
  404. {
  405. struct mxs_lradc_adc *adc = iio_priv(iio);
  406. iio_trigger_unregister(adc->trig);
  407. }
  408. static int mxs_lradc_adc_buffer_preenable(struct iio_dev *iio)
  409. {
  410. struct mxs_lradc_adc *adc = iio_priv(iio);
  411. struct mxs_lradc *lradc = adc->lradc;
  412. int chan, ofs = 0;
  413. unsigned long enable = 0;
  414. u32 ctrl4_set = 0;
  415. u32 ctrl4_clr = 0;
  416. u32 ctrl1_irq = 0;
  417. const u32 chan_value = LRADC_CH_ACCUMULATE |
  418. ((LRADC_DELAY_TIMER_LOOP - 1) << LRADC_CH_NUM_SAMPLES_OFFSET);
  419. if (lradc->soc == IMX28_LRADC)
  420. writel(lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
  421. adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
  422. writel(lradc->buffer_vchans,
  423. adc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
  424. for_each_set_bit(chan, iio->active_scan_mask, LRADC_MAX_TOTAL_CHANS) {
  425. ctrl4_set |= chan << LRADC_CTRL4_LRADCSELECT_OFFSET(ofs);
  426. ctrl4_clr |= LRADC_CTRL4_LRADCSELECT_MASK(ofs);
  427. ctrl1_irq |= LRADC_CTRL1_LRADC_IRQ_EN(ofs);
  428. writel(chan_value, adc->base + LRADC_CH(ofs));
  429. bitmap_set(&enable, ofs, 1);
  430. ofs++;
  431. }
  432. writel(LRADC_DELAY_TRIGGER_LRADCS_MASK | LRADC_DELAY_KICK,
  433. adc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_CLR);
  434. writel(ctrl4_clr, adc->base + LRADC_CTRL4 + STMP_OFFSET_REG_CLR);
  435. writel(ctrl4_set, adc->base + LRADC_CTRL4 + STMP_OFFSET_REG_SET);
  436. writel(ctrl1_irq, adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_SET);
  437. writel(enable << LRADC_DELAY_TRIGGER_LRADCS_OFFSET,
  438. adc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_SET);
  439. return 0;
  440. }
  441. static int mxs_lradc_adc_buffer_postdisable(struct iio_dev *iio)
  442. {
  443. struct mxs_lradc_adc *adc = iio_priv(iio);
  444. struct mxs_lradc *lradc = adc->lradc;
  445. writel(LRADC_DELAY_TRIGGER_LRADCS_MASK | LRADC_DELAY_KICK,
  446. adc->base + LRADC_DELAY(0) + STMP_OFFSET_REG_CLR);
  447. writel(lradc->buffer_vchans,
  448. adc->base + LRADC_CTRL0 + STMP_OFFSET_REG_CLR);
  449. if (lradc->soc == IMX28_LRADC)
  450. writel(lradc->buffer_vchans << LRADC_CTRL1_LRADC_IRQ_EN_OFFSET,
  451. adc->base + LRADC_CTRL1 + STMP_OFFSET_REG_CLR);
  452. return 0;
  453. }
  454. static bool mxs_lradc_adc_validate_scan_mask(struct iio_dev *iio,
  455. const unsigned long *mask)
  456. {
  457. struct mxs_lradc_adc *adc = iio_priv(iio);
  458. struct mxs_lradc *lradc = adc->lradc;
  459. const int map_chans = bitmap_weight(mask, LRADC_MAX_TOTAL_CHANS);
  460. int rsvd_chans = 0;
  461. unsigned long rsvd_mask = 0;
  462. if (lradc->use_touchbutton)
  463. rsvd_mask |= CHAN_MASK_TOUCHBUTTON;
  464. if (lradc->touchscreen_wire == MXS_LRADC_TOUCHSCREEN_4WIRE)
  465. rsvd_mask |= CHAN_MASK_TOUCHSCREEN_4WIRE;
  466. if (lradc->touchscreen_wire == MXS_LRADC_TOUCHSCREEN_5WIRE)
  467. rsvd_mask |= CHAN_MASK_TOUCHSCREEN_5WIRE;
  468. if (lradc->use_touchbutton)
  469. rsvd_chans++;
  470. if (lradc->touchscreen_wire)
  471. rsvd_chans += 2;
  472. /* Test for attempts to map channels with special mode of operation. */
  473. if (bitmap_intersects(mask, &rsvd_mask, LRADC_MAX_TOTAL_CHANS))
  474. return false;
  475. /* Test for attempts to map more channels then available slots. */
  476. if (map_chans + rsvd_chans > LRADC_MAX_MAPPED_CHANS)
  477. return false;
  478. return true;
  479. }
  480. static const struct iio_buffer_setup_ops mxs_lradc_adc_buffer_ops = {
  481. .preenable = &mxs_lradc_adc_buffer_preenable,
  482. .postenable = &iio_triggered_buffer_postenable,
  483. .predisable = &iio_triggered_buffer_predisable,
  484. .postdisable = &mxs_lradc_adc_buffer_postdisable,
  485. .validate_scan_mask = &mxs_lradc_adc_validate_scan_mask,
  486. };
  487. /* Driver initialization */
  488. #define MXS_ADC_CHAN(idx, chan_type, name) { \
  489. .type = (chan_type), \
  490. .indexed = 1, \
  491. .scan_index = (idx), \
  492. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  493. BIT(IIO_CHAN_INFO_SCALE), \
  494. .channel = (idx), \
  495. .address = (idx), \
  496. .scan_type = { \
  497. .sign = 'u', \
  498. .realbits = LRADC_RESOLUTION, \
  499. .storagebits = 32, \
  500. }, \
  501. .datasheet_name = (name), \
  502. }
  503. static const struct iio_chan_spec mx23_lradc_chan_spec[] = {
  504. MXS_ADC_CHAN(0, IIO_VOLTAGE, "LRADC0"),
  505. MXS_ADC_CHAN(1, IIO_VOLTAGE, "LRADC1"),
  506. MXS_ADC_CHAN(2, IIO_VOLTAGE, "LRADC2"),
  507. MXS_ADC_CHAN(3, IIO_VOLTAGE, "LRADC3"),
  508. MXS_ADC_CHAN(4, IIO_VOLTAGE, "LRADC4"),
  509. MXS_ADC_CHAN(5, IIO_VOLTAGE, "LRADC5"),
  510. MXS_ADC_CHAN(6, IIO_VOLTAGE, "VDDIO"),
  511. MXS_ADC_CHAN(7, IIO_VOLTAGE, "VBATT"),
  512. /* Combined Temperature sensors */
  513. {
  514. .type = IIO_TEMP,
  515. .indexed = 1,
  516. .scan_index = 8,
  517. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  518. BIT(IIO_CHAN_INFO_OFFSET) |
  519. BIT(IIO_CHAN_INFO_SCALE),
  520. .channel = 8,
  521. .scan_type = {.sign = 'u', .realbits = 18, .storagebits = 32,},
  522. .datasheet_name = "TEMP_DIE",
  523. },
  524. /* Hidden channel to keep indexes */
  525. {
  526. .type = IIO_TEMP,
  527. .indexed = 1,
  528. .scan_index = -1,
  529. .channel = 9,
  530. },
  531. MXS_ADC_CHAN(10, IIO_VOLTAGE, NULL),
  532. MXS_ADC_CHAN(11, IIO_VOLTAGE, NULL),
  533. MXS_ADC_CHAN(12, IIO_VOLTAGE, "USB_DP"),
  534. MXS_ADC_CHAN(13, IIO_VOLTAGE, "USB_DN"),
  535. MXS_ADC_CHAN(14, IIO_VOLTAGE, "VBG"),
  536. MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"),
  537. };
  538. static const struct iio_chan_spec mx28_lradc_chan_spec[] = {
  539. MXS_ADC_CHAN(0, IIO_VOLTAGE, "LRADC0"),
  540. MXS_ADC_CHAN(1, IIO_VOLTAGE, "LRADC1"),
  541. MXS_ADC_CHAN(2, IIO_VOLTAGE, "LRADC2"),
  542. MXS_ADC_CHAN(3, IIO_VOLTAGE, "LRADC3"),
  543. MXS_ADC_CHAN(4, IIO_VOLTAGE, "LRADC4"),
  544. MXS_ADC_CHAN(5, IIO_VOLTAGE, "LRADC5"),
  545. MXS_ADC_CHAN(6, IIO_VOLTAGE, "LRADC6"),
  546. MXS_ADC_CHAN(7, IIO_VOLTAGE, "VBATT"),
  547. /* Combined Temperature sensors */
  548. {
  549. .type = IIO_TEMP,
  550. .indexed = 1,
  551. .scan_index = 8,
  552. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  553. BIT(IIO_CHAN_INFO_OFFSET) |
  554. BIT(IIO_CHAN_INFO_SCALE),
  555. .channel = 8,
  556. .scan_type = {.sign = 'u', .realbits = 18, .storagebits = 32,},
  557. .datasheet_name = "TEMP_DIE",
  558. },
  559. /* Hidden channel to keep indexes */
  560. {
  561. .type = IIO_TEMP,
  562. .indexed = 1,
  563. .scan_index = -1,
  564. .channel = 9,
  565. },
  566. MXS_ADC_CHAN(10, IIO_VOLTAGE, "VDDIO"),
  567. MXS_ADC_CHAN(11, IIO_VOLTAGE, "VTH"),
  568. MXS_ADC_CHAN(12, IIO_VOLTAGE, "VDDA"),
  569. MXS_ADC_CHAN(13, IIO_VOLTAGE, "VDDD"),
  570. MXS_ADC_CHAN(14, IIO_VOLTAGE, "VBG"),
  571. MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"),
  572. };
  573. static void mxs_lradc_adc_hw_init(struct mxs_lradc_adc *adc)
  574. {
  575. /* The ADC always uses DELAY CHANNEL 0. */
  576. const u32 adc_cfg =
  577. (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) |
  578. (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET);
  579. /* Configure DELAY CHANNEL 0 for generic ADC sampling. */
  580. writel(adc_cfg, adc->base + LRADC_DELAY(0));
  581. /*
  582. * Start internal temperature sensing by clearing bit
  583. * HW_LRADC_CTRL2_TEMPSENSE_PWD. This bit can be left cleared
  584. * after power up.
  585. */
  586. writel(0, adc->base + LRADC_CTRL2);
  587. }
  588. static void mxs_lradc_adc_hw_stop(struct mxs_lradc_adc *adc)
  589. {
  590. writel(0, adc->base + LRADC_DELAY(0));
  591. }
  592. static int mxs_lradc_adc_probe(struct platform_device *pdev)
  593. {
  594. struct device *dev = &pdev->dev;
  595. struct mxs_lradc *lradc = dev_get_drvdata(dev->parent);
  596. struct mxs_lradc_adc *adc;
  597. struct iio_dev *iio;
  598. struct resource *iores;
  599. int ret, irq, virq, i, s, n;
  600. u64 scale_uv;
  601. const char **irq_name;
  602. /* Allocate the IIO device. */
  603. iio = devm_iio_device_alloc(dev, sizeof(*adc));
  604. if (!iio) {
  605. dev_err(dev, "Failed to allocate IIO device\n");
  606. return -ENOMEM;
  607. }
  608. adc = iio_priv(iio);
  609. adc->lradc = lradc;
  610. adc->dev = dev;
  611. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  612. if (!iores)
  613. return -EINVAL;
  614. adc->base = devm_ioremap(dev, iores->start, resource_size(iores));
  615. if (!adc->base)
  616. return -ENOMEM;
  617. init_completion(&adc->completion);
  618. spin_lock_init(&adc->lock);
  619. platform_set_drvdata(pdev, iio);
  620. iio->name = pdev->name;
  621. iio->dev.parent = dev;
  622. iio->dev.of_node = dev->parent->of_node;
  623. iio->info = &mxs_lradc_adc_iio_info;
  624. iio->modes = INDIO_DIRECT_MODE;
  625. iio->masklength = LRADC_MAX_TOTAL_CHANS;
  626. if (lradc->soc == IMX23_LRADC) {
  627. iio->channels = mx23_lradc_chan_spec;
  628. iio->num_channels = ARRAY_SIZE(mx23_lradc_chan_spec);
  629. irq_name = mx23_lradc_adc_irq_names;
  630. n = ARRAY_SIZE(mx23_lradc_adc_irq_names);
  631. } else {
  632. iio->channels = mx28_lradc_chan_spec;
  633. iio->num_channels = ARRAY_SIZE(mx28_lradc_chan_spec);
  634. irq_name = mx28_lradc_adc_irq_names;
  635. n = ARRAY_SIZE(mx28_lradc_adc_irq_names);
  636. }
  637. ret = stmp_reset_block(adc->base);
  638. if (ret)
  639. return ret;
  640. for (i = 0; i < n; i++) {
  641. irq = platform_get_irq_byname(pdev, irq_name[i]);
  642. if (irq < 0)
  643. return irq;
  644. virq = irq_of_parse_and_map(dev->parent->of_node, irq);
  645. ret = devm_request_irq(dev, virq, mxs_lradc_adc_handle_irq,
  646. 0, irq_name[i], iio);
  647. if (ret)
  648. return ret;
  649. }
  650. ret = mxs_lradc_adc_trigger_init(iio);
  651. if (ret)
  652. goto err_trig;
  653. ret = iio_triggered_buffer_setup(iio, &iio_pollfunc_store_time,
  654. &mxs_lradc_adc_trigger_handler,
  655. &mxs_lradc_adc_buffer_ops);
  656. if (ret)
  657. return ret;
  658. adc->vref_mv = mxs_lradc_adc_vref_mv[lradc->soc];
  659. /* Populate available ADC input ranges */
  660. for (i = 0; i < LRADC_MAX_TOTAL_CHANS; i++) {
  661. for (s = 0; s < ARRAY_SIZE(adc->scale_avail[i]); s++) {
  662. /*
  663. * [s=0] = optional divider by two disabled (default)
  664. * [s=1] = optional divider by two enabled
  665. *
  666. * The scale is calculated by doing:
  667. * Vref >> (realbits - s)
  668. * which multiplies by two on the second component
  669. * of the array.
  670. */
  671. scale_uv = ((u64)adc->vref_mv[i] * 100000000) >>
  672. (LRADC_RESOLUTION - s);
  673. adc->scale_avail[i][s].nano =
  674. do_div(scale_uv, 100000000) * 10;
  675. adc->scale_avail[i][s].integer = scale_uv;
  676. }
  677. }
  678. /* Configure the hardware. */
  679. mxs_lradc_adc_hw_init(adc);
  680. /* Register IIO device. */
  681. ret = iio_device_register(iio);
  682. if (ret) {
  683. dev_err(dev, "Failed to register IIO device\n");
  684. goto err_dev;
  685. }
  686. return 0;
  687. err_dev:
  688. mxs_lradc_adc_hw_stop(adc);
  689. mxs_lradc_adc_trigger_remove(iio);
  690. err_trig:
  691. iio_triggered_buffer_cleanup(iio);
  692. return ret;
  693. }
  694. static int mxs_lradc_adc_remove(struct platform_device *pdev)
  695. {
  696. struct iio_dev *iio = platform_get_drvdata(pdev);
  697. struct mxs_lradc_adc *adc = iio_priv(iio);
  698. iio_device_unregister(iio);
  699. mxs_lradc_adc_hw_stop(adc);
  700. mxs_lradc_adc_trigger_remove(iio);
  701. iio_triggered_buffer_cleanup(iio);
  702. return 0;
  703. }
  704. static struct platform_driver mxs_lradc_adc_driver = {
  705. .driver = {
  706. .name = "mxs-lradc-adc",
  707. },
  708. .probe = mxs_lradc_adc_probe,
  709. .remove = mxs_lradc_adc_remove,
  710. };
  711. module_platform_driver(mxs_lradc_adc_driver);
  712. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  713. MODULE_DESCRIPTION("Freescale MXS LRADC driver general purpose ADC driver");
  714. MODULE_LICENSE("GPL");
  715. MODULE_ALIAS("platform:mxs-lradc-adc");