vf610_adc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Freescale Vybrid vf610 ADC driver
  3. *
  4. * Copyright 2013 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/io.h>
  27. #include <linux/clk.h>
  28. #include <linux/completion.h>
  29. #include <linux/of.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/err.h>
  34. #include <linux/iio/iio.h>
  35. #include <linux/iio/sysfs.h>
  36. #include <linux/iio/driver.h>
  37. /* This will be the driver name the kernel reports */
  38. #define DRIVER_NAME "vf610-adc"
  39. /* Vybrid/IMX ADC registers */
  40. #define VF610_REG_ADC_HC0 0x00
  41. #define VF610_REG_ADC_HC1 0x04
  42. #define VF610_REG_ADC_HS 0x08
  43. #define VF610_REG_ADC_R0 0x0c
  44. #define VF610_REG_ADC_R1 0x10
  45. #define VF610_REG_ADC_CFG 0x14
  46. #define VF610_REG_ADC_GC 0x18
  47. #define VF610_REG_ADC_GS 0x1c
  48. #define VF610_REG_ADC_CV 0x20
  49. #define VF610_REG_ADC_OFS 0x24
  50. #define VF610_REG_ADC_CAL 0x28
  51. #define VF610_REG_ADC_PCTL 0x30
  52. /* Configuration register field define */
  53. #define VF610_ADC_MODE_BIT8 0x00
  54. #define VF610_ADC_MODE_BIT10 0x04
  55. #define VF610_ADC_MODE_BIT12 0x08
  56. #define VF610_ADC_MODE_MASK 0x0c
  57. #define VF610_ADC_BUSCLK2_SEL 0x01
  58. #define VF610_ADC_ALTCLK_SEL 0x02
  59. #define VF610_ADC_ADACK_SEL 0x03
  60. #define VF610_ADC_ADCCLK_MASK 0x03
  61. #define VF610_ADC_CLK_DIV2 0x20
  62. #define VF610_ADC_CLK_DIV4 0x40
  63. #define VF610_ADC_CLK_DIV8 0x60
  64. #define VF610_ADC_CLK_MASK 0x60
  65. #define VF610_ADC_ADLSMP_LONG 0x10
  66. #define VF610_ADC_ADSTS_MASK 0x300
  67. #define VF610_ADC_ADLPC_EN 0x80
  68. #define VF610_ADC_ADHSC_EN 0x400
  69. #define VF610_ADC_REFSEL_VALT 0x100
  70. #define VF610_ADC_REFSEL_VBG 0x1000
  71. #define VF610_ADC_ADTRG_HARD 0x2000
  72. #define VF610_ADC_AVGS_8 0x4000
  73. #define VF610_ADC_AVGS_16 0x8000
  74. #define VF610_ADC_AVGS_32 0xC000
  75. #define VF610_ADC_AVGS_MASK 0xC000
  76. #define VF610_ADC_OVWREN 0x10000
  77. /* General control register field define */
  78. #define VF610_ADC_ADACKEN 0x1
  79. #define VF610_ADC_DMAEN 0x2
  80. #define VF610_ADC_ACREN 0x4
  81. #define VF610_ADC_ACFGT 0x8
  82. #define VF610_ADC_ACFE 0x10
  83. #define VF610_ADC_AVGEN 0x20
  84. #define VF610_ADC_ADCON 0x40
  85. #define VF610_ADC_CAL 0x80
  86. /* Other field define */
  87. #define VF610_ADC_ADCHC(x) ((x) & 0xF)
  88. #define VF610_ADC_AIEN (0x1 << 7)
  89. #define VF610_ADC_CONV_DISABLE 0x1F
  90. #define VF610_ADC_HS_COCO0 0x1
  91. #define VF610_ADC_CALF 0x2
  92. #define VF610_ADC_TIMEOUT msecs_to_jiffies(100)
  93. enum clk_sel {
  94. VF610_ADCIOC_BUSCLK_SET,
  95. VF610_ADCIOC_ALTCLK_SET,
  96. VF610_ADCIOC_ADACK_SET,
  97. };
  98. enum vol_ref {
  99. VF610_ADCIOC_VR_VREF_SET,
  100. VF610_ADCIOC_VR_VALT_SET,
  101. VF610_ADCIOC_VR_VBG_SET,
  102. };
  103. enum average_sel {
  104. VF610_ADC_SAMPLE_1,
  105. VF610_ADC_SAMPLE_4,
  106. VF610_ADC_SAMPLE_8,
  107. VF610_ADC_SAMPLE_16,
  108. VF610_ADC_SAMPLE_32,
  109. };
  110. struct vf610_adc_feature {
  111. enum clk_sel clk_sel;
  112. enum vol_ref vol_ref;
  113. int clk_div;
  114. int sample_rate;
  115. int res_mode;
  116. bool lpm;
  117. bool calibration;
  118. bool ovwren;
  119. };
  120. struct vf610_adc {
  121. struct device *dev;
  122. void __iomem *regs;
  123. struct clk *clk;
  124. u32 vref_uv;
  125. u32 value;
  126. struct regulator *vref;
  127. struct vf610_adc_feature adc_feature;
  128. u32 sample_freq_avail[5];
  129. struct completion completion;
  130. };
  131. static const u32 vf610_hw_avgs[] = { 1, 4, 8, 16, 32 };
  132. #define VF610_ADC_CHAN(_idx, _chan_type) { \
  133. .type = (_chan_type), \
  134. .indexed = 1, \
  135. .channel = (_idx), \
  136. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  137. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  138. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  139. }
  140. static const struct iio_chan_spec vf610_adc_iio_channels[] = {
  141. VF610_ADC_CHAN(0, IIO_VOLTAGE),
  142. VF610_ADC_CHAN(1, IIO_VOLTAGE),
  143. VF610_ADC_CHAN(2, IIO_VOLTAGE),
  144. VF610_ADC_CHAN(3, IIO_VOLTAGE),
  145. VF610_ADC_CHAN(4, IIO_VOLTAGE),
  146. VF610_ADC_CHAN(5, IIO_VOLTAGE),
  147. VF610_ADC_CHAN(6, IIO_VOLTAGE),
  148. VF610_ADC_CHAN(7, IIO_VOLTAGE),
  149. VF610_ADC_CHAN(8, IIO_VOLTAGE),
  150. VF610_ADC_CHAN(9, IIO_VOLTAGE),
  151. VF610_ADC_CHAN(10, IIO_VOLTAGE),
  152. VF610_ADC_CHAN(11, IIO_VOLTAGE),
  153. VF610_ADC_CHAN(12, IIO_VOLTAGE),
  154. VF610_ADC_CHAN(13, IIO_VOLTAGE),
  155. VF610_ADC_CHAN(14, IIO_VOLTAGE),
  156. VF610_ADC_CHAN(15, IIO_VOLTAGE),
  157. /* sentinel */
  158. };
  159. static inline void vf610_adc_calculate_rates(struct vf610_adc *info)
  160. {
  161. unsigned long adck_rate, ipg_rate = clk_get_rate(info->clk);
  162. int i;
  163. /*
  164. * Calculate ADC sample frequencies
  165. * Sample time unit is ADCK cycles. ADCK clk source is ipg clock,
  166. * which is the same as bus clock.
  167. *
  168. * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
  169. * SFCAdder: fixed to 6 ADCK cycles
  170. * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
  171. * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
  172. * LSTAdder(Long Sample Time): fixed to 3 ADCK cycles
  173. */
  174. adck_rate = ipg_rate / info->adc_feature.clk_div;
  175. for (i = 0; i < ARRAY_SIZE(vf610_hw_avgs); i++)
  176. info->sample_freq_avail[i] =
  177. adck_rate / (6 + vf610_hw_avgs[i] * (25 + 3));
  178. }
  179. static inline void vf610_adc_cfg_init(struct vf610_adc *info)
  180. {
  181. struct vf610_adc_feature *adc_feature = &info->adc_feature;
  182. /* set default Configuration for ADC controller */
  183. adc_feature->clk_sel = VF610_ADCIOC_BUSCLK_SET;
  184. adc_feature->vol_ref = VF610_ADCIOC_VR_VREF_SET;
  185. adc_feature->calibration = true;
  186. adc_feature->ovwren = true;
  187. adc_feature->res_mode = 12;
  188. adc_feature->sample_rate = 1;
  189. adc_feature->lpm = true;
  190. /* Use a save ADCK which is below 20MHz on all devices */
  191. adc_feature->clk_div = 8;
  192. vf610_adc_calculate_rates(info);
  193. }
  194. static void vf610_adc_cfg_post_set(struct vf610_adc *info)
  195. {
  196. struct vf610_adc_feature *adc_feature = &info->adc_feature;
  197. int cfg_data = 0;
  198. int gc_data = 0;
  199. switch (adc_feature->clk_sel) {
  200. case VF610_ADCIOC_ALTCLK_SET:
  201. cfg_data |= VF610_ADC_ALTCLK_SEL;
  202. break;
  203. case VF610_ADCIOC_ADACK_SET:
  204. cfg_data |= VF610_ADC_ADACK_SEL;
  205. break;
  206. default:
  207. break;
  208. }
  209. /* low power set for calibration */
  210. cfg_data |= VF610_ADC_ADLPC_EN;
  211. /* enable high speed for calibration */
  212. cfg_data |= VF610_ADC_ADHSC_EN;
  213. /* voltage reference */
  214. switch (adc_feature->vol_ref) {
  215. case VF610_ADCIOC_VR_VREF_SET:
  216. break;
  217. case VF610_ADCIOC_VR_VALT_SET:
  218. cfg_data |= VF610_ADC_REFSEL_VALT;
  219. break;
  220. case VF610_ADCIOC_VR_VBG_SET:
  221. cfg_data |= VF610_ADC_REFSEL_VBG;
  222. break;
  223. default:
  224. dev_err(info->dev, "error voltage reference\n");
  225. }
  226. /* data overwrite enable */
  227. if (adc_feature->ovwren)
  228. cfg_data |= VF610_ADC_OVWREN;
  229. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  230. writel(gc_data, info->regs + VF610_REG_ADC_GC);
  231. }
  232. static void vf610_adc_calibration(struct vf610_adc *info)
  233. {
  234. int adc_gc, hc_cfg;
  235. int timeout;
  236. if (!info->adc_feature.calibration)
  237. return;
  238. /* enable calibration interrupt */
  239. hc_cfg = VF610_ADC_AIEN | VF610_ADC_CONV_DISABLE;
  240. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  241. adc_gc = readl(info->regs + VF610_REG_ADC_GC);
  242. writel(adc_gc | VF610_ADC_CAL, info->regs + VF610_REG_ADC_GC);
  243. timeout = wait_for_completion_timeout
  244. (&info->completion, VF610_ADC_TIMEOUT);
  245. if (timeout == 0)
  246. dev_err(info->dev, "Timeout for adc calibration\n");
  247. adc_gc = readl(info->regs + VF610_REG_ADC_GS);
  248. if (adc_gc & VF610_ADC_CALF)
  249. dev_err(info->dev, "ADC calibration failed\n");
  250. info->adc_feature.calibration = false;
  251. }
  252. static void vf610_adc_cfg_set(struct vf610_adc *info)
  253. {
  254. struct vf610_adc_feature *adc_feature = &(info->adc_feature);
  255. int cfg_data;
  256. cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
  257. cfg_data &= ~VF610_ADC_ADLPC_EN;
  258. if (adc_feature->lpm)
  259. cfg_data |= VF610_ADC_ADLPC_EN;
  260. cfg_data &= ~VF610_ADC_ADHSC_EN;
  261. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  262. }
  263. static void vf610_adc_sample_set(struct vf610_adc *info)
  264. {
  265. struct vf610_adc_feature *adc_feature = &(info->adc_feature);
  266. int cfg_data, gc_data;
  267. cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
  268. gc_data = readl(info->regs + VF610_REG_ADC_GC);
  269. /* resolution mode */
  270. cfg_data &= ~VF610_ADC_MODE_MASK;
  271. switch (adc_feature->res_mode) {
  272. case 8:
  273. cfg_data |= VF610_ADC_MODE_BIT8;
  274. break;
  275. case 10:
  276. cfg_data |= VF610_ADC_MODE_BIT10;
  277. break;
  278. case 12:
  279. cfg_data |= VF610_ADC_MODE_BIT12;
  280. break;
  281. default:
  282. dev_err(info->dev, "error resolution mode\n");
  283. break;
  284. }
  285. /* clock select and clock divider */
  286. cfg_data &= ~(VF610_ADC_CLK_MASK | VF610_ADC_ADCCLK_MASK);
  287. switch (adc_feature->clk_div) {
  288. case 1:
  289. break;
  290. case 2:
  291. cfg_data |= VF610_ADC_CLK_DIV2;
  292. break;
  293. case 4:
  294. cfg_data |= VF610_ADC_CLK_DIV4;
  295. break;
  296. case 8:
  297. cfg_data |= VF610_ADC_CLK_DIV8;
  298. break;
  299. case 16:
  300. switch (adc_feature->clk_sel) {
  301. case VF610_ADCIOC_BUSCLK_SET:
  302. cfg_data |= VF610_ADC_BUSCLK2_SEL | VF610_ADC_CLK_DIV8;
  303. break;
  304. default:
  305. dev_err(info->dev, "error clk divider\n");
  306. break;
  307. }
  308. break;
  309. }
  310. /* Use the short sample mode */
  311. cfg_data &= ~(VF610_ADC_ADLSMP_LONG | VF610_ADC_ADSTS_MASK);
  312. /* update hardware average selection */
  313. cfg_data &= ~VF610_ADC_AVGS_MASK;
  314. gc_data &= ~VF610_ADC_AVGEN;
  315. switch (adc_feature->sample_rate) {
  316. case VF610_ADC_SAMPLE_1:
  317. break;
  318. case VF610_ADC_SAMPLE_4:
  319. gc_data |= VF610_ADC_AVGEN;
  320. break;
  321. case VF610_ADC_SAMPLE_8:
  322. gc_data |= VF610_ADC_AVGEN;
  323. cfg_data |= VF610_ADC_AVGS_8;
  324. break;
  325. case VF610_ADC_SAMPLE_16:
  326. gc_data |= VF610_ADC_AVGEN;
  327. cfg_data |= VF610_ADC_AVGS_16;
  328. break;
  329. case VF610_ADC_SAMPLE_32:
  330. gc_data |= VF610_ADC_AVGEN;
  331. cfg_data |= VF610_ADC_AVGS_32;
  332. break;
  333. default:
  334. dev_err(info->dev,
  335. "error hardware sample average select\n");
  336. }
  337. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  338. writel(gc_data, info->regs + VF610_REG_ADC_GC);
  339. }
  340. static void vf610_adc_hw_init(struct vf610_adc *info)
  341. {
  342. /* CFG: Feature set */
  343. vf610_adc_cfg_post_set(info);
  344. vf610_adc_sample_set(info);
  345. /* adc calibration */
  346. vf610_adc_calibration(info);
  347. /* CFG: power and speed set */
  348. vf610_adc_cfg_set(info);
  349. }
  350. static int vf610_adc_read_data(struct vf610_adc *info)
  351. {
  352. int result;
  353. result = readl(info->regs + VF610_REG_ADC_R0);
  354. switch (info->adc_feature.res_mode) {
  355. case 8:
  356. result &= 0xFF;
  357. break;
  358. case 10:
  359. result &= 0x3FF;
  360. break;
  361. case 12:
  362. result &= 0xFFF;
  363. break;
  364. default:
  365. break;
  366. }
  367. return result;
  368. }
  369. static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
  370. {
  371. struct vf610_adc *info = (struct vf610_adc *)dev_id;
  372. int coco;
  373. coco = readl(info->regs + VF610_REG_ADC_HS);
  374. if (coco & VF610_ADC_HS_COCO0) {
  375. info->value = vf610_adc_read_data(info);
  376. complete(&info->completion);
  377. }
  378. return IRQ_HANDLED;
  379. }
  380. static ssize_t vf610_show_samp_freq_avail(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. struct vf610_adc *info = iio_priv(dev_to_iio_dev(dev));
  384. size_t len = 0;
  385. int i;
  386. for (i = 0; i < ARRAY_SIZE(info->sample_freq_avail); i++)
  387. len += scnprintf(buf + len, PAGE_SIZE - len,
  388. "%u ", info->sample_freq_avail[i]);
  389. /* replace trailing space by newline */
  390. buf[len - 1] = '\n';
  391. return len;
  392. }
  393. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail);
  394. static struct attribute *vf610_attributes[] = {
  395. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  396. NULL
  397. };
  398. static const struct attribute_group vf610_attribute_group = {
  399. .attrs = vf610_attributes,
  400. };
  401. static int vf610_read_raw(struct iio_dev *indio_dev,
  402. struct iio_chan_spec const *chan,
  403. int *val,
  404. int *val2,
  405. long mask)
  406. {
  407. struct vf610_adc *info = iio_priv(indio_dev);
  408. unsigned int hc_cfg;
  409. long ret;
  410. switch (mask) {
  411. case IIO_CHAN_INFO_RAW:
  412. mutex_lock(&indio_dev->mlock);
  413. reinit_completion(&info->completion);
  414. hc_cfg = VF610_ADC_ADCHC(chan->channel);
  415. hc_cfg |= VF610_ADC_AIEN;
  416. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  417. ret = wait_for_completion_interruptible_timeout
  418. (&info->completion, VF610_ADC_TIMEOUT);
  419. if (ret == 0) {
  420. mutex_unlock(&indio_dev->mlock);
  421. return -ETIMEDOUT;
  422. }
  423. if (ret < 0) {
  424. mutex_unlock(&indio_dev->mlock);
  425. return ret;
  426. }
  427. *val = info->value;
  428. mutex_unlock(&indio_dev->mlock);
  429. return IIO_VAL_INT;
  430. case IIO_CHAN_INFO_SCALE:
  431. *val = info->vref_uv / 1000;
  432. *val2 = info->adc_feature.res_mode;
  433. return IIO_VAL_FRACTIONAL_LOG2;
  434. case IIO_CHAN_INFO_SAMP_FREQ:
  435. *val = info->sample_freq_avail[info->adc_feature.sample_rate];
  436. *val2 = 0;
  437. return IIO_VAL_INT;
  438. default:
  439. break;
  440. }
  441. return -EINVAL;
  442. }
  443. static int vf610_write_raw(struct iio_dev *indio_dev,
  444. struct iio_chan_spec const *chan,
  445. int val,
  446. int val2,
  447. long mask)
  448. {
  449. struct vf610_adc *info = iio_priv(indio_dev);
  450. int i;
  451. switch (mask) {
  452. case IIO_CHAN_INFO_SAMP_FREQ:
  453. for (i = 0;
  454. i < ARRAY_SIZE(info->sample_freq_avail);
  455. i++)
  456. if (val == info->sample_freq_avail[i]) {
  457. info->adc_feature.sample_rate = i;
  458. vf610_adc_sample_set(info);
  459. return 0;
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. return -EINVAL;
  466. }
  467. static int vf610_adc_reg_access(struct iio_dev *indio_dev,
  468. unsigned reg, unsigned writeval,
  469. unsigned *readval)
  470. {
  471. struct vf610_adc *info = iio_priv(indio_dev);
  472. if ((readval == NULL) ||
  473. (!(reg % 4) || (reg > VF610_REG_ADC_PCTL)))
  474. return -EINVAL;
  475. *readval = readl(info->regs + reg);
  476. return 0;
  477. }
  478. static const struct iio_info vf610_adc_iio_info = {
  479. .driver_module = THIS_MODULE,
  480. .read_raw = &vf610_read_raw,
  481. .write_raw = &vf610_write_raw,
  482. .debugfs_reg_access = &vf610_adc_reg_access,
  483. .attrs = &vf610_attribute_group,
  484. };
  485. static const struct of_device_id vf610_adc_match[] = {
  486. { .compatible = "fsl,vf610-adc", },
  487. { /* sentinel */ }
  488. };
  489. MODULE_DEVICE_TABLE(of, vf610_adc_match);
  490. static int vf610_adc_probe(struct platform_device *pdev)
  491. {
  492. struct vf610_adc *info;
  493. struct iio_dev *indio_dev;
  494. struct resource *mem;
  495. int irq;
  496. int ret;
  497. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct vf610_adc));
  498. if (!indio_dev) {
  499. dev_err(&pdev->dev, "Failed allocating iio device\n");
  500. return -ENOMEM;
  501. }
  502. info = iio_priv(indio_dev);
  503. info->dev = &pdev->dev;
  504. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  505. info->regs = devm_ioremap_resource(&pdev->dev, mem);
  506. if (IS_ERR(info->regs))
  507. return PTR_ERR(info->regs);
  508. irq = platform_get_irq(pdev, 0);
  509. if (irq <= 0) {
  510. dev_err(&pdev->dev, "no irq resource?\n");
  511. return -EINVAL;
  512. }
  513. ret = devm_request_irq(info->dev, irq,
  514. vf610_adc_isr, 0,
  515. dev_name(&pdev->dev), info);
  516. if (ret < 0) {
  517. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", irq);
  518. return ret;
  519. }
  520. info->clk = devm_clk_get(&pdev->dev, "adc");
  521. if (IS_ERR(info->clk)) {
  522. dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
  523. PTR_ERR(info->clk));
  524. ret = PTR_ERR(info->clk);
  525. return ret;
  526. }
  527. info->vref = devm_regulator_get(&pdev->dev, "vref");
  528. if (IS_ERR(info->vref))
  529. return PTR_ERR(info->vref);
  530. ret = regulator_enable(info->vref);
  531. if (ret)
  532. return ret;
  533. info->vref_uv = regulator_get_voltage(info->vref);
  534. platform_set_drvdata(pdev, indio_dev);
  535. init_completion(&info->completion);
  536. indio_dev->name = dev_name(&pdev->dev);
  537. indio_dev->dev.parent = &pdev->dev;
  538. indio_dev->dev.of_node = pdev->dev.of_node;
  539. indio_dev->info = &vf610_adc_iio_info;
  540. indio_dev->modes = INDIO_DIRECT_MODE;
  541. indio_dev->channels = vf610_adc_iio_channels;
  542. indio_dev->num_channels = ARRAY_SIZE(vf610_adc_iio_channels);
  543. ret = clk_prepare_enable(info->clk);
  544. if (ret) {
  545. dev_err(&pdev->dev,
  546. "Could not prepare or enable the clock.\n");
  547. goto error_adc_clk_enable;
  548. }
  549. vf610_adc_cfg_init(info);
  550. vf610_adc_hw_init(info);
  551. ret = iio_device_register(indio_dev);
  552. if (ret) {
  553. dev_err(&pdev->dev, "Couldn't register the device.\n");
  554. goto error_iio_device_register;
  555. }
  556. return 0;
  557. error_iio_device_register:
  558. clk_disable_unprepare(info->clk);
  559. error_adc_clk_enable:
  560. regulator_disable(info->vref);
  561. return ret;
  562. }
  563. static int vf610_adc_remove(struct platform_device *pdev)
  564. {
  565. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  566. struct vf610_adc *info = iio_priv(indio_dev);
  567. iio_device_unregister(indio_dev);
  568. regulator_disable(info->vref);
  569. clk_disable_unprepare(info->clk);
  570. return 0;
  571. }
  572. #ifdef CONFIG_PM_SLEEP
  573. static int vf610_adc_suspend(struct device *dev)
  574. {
  575. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  576. struct vf610_adc *info = iio_priv(indio_dev);
  577. int hc_cfg;
  578. /* ADC controller enters to stop mode */
  579. hc_cfg = readl(info->regs + VF610_REG_ADC_HC0);
  580. hc_cfg |= VF610_ADC_CONV_DISABLE;
  581. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  582. clk_disable_unprepare(info->clk);
  583. regulator_disable(info->vref);
  584. return 0;
  585. }
  586. static int vf610_adc_resume(struct device *dev)
  587. {
  588. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  589. struct vf610_adc *info = iio_priv(indio_dev);
  590. int ret;
  591. ret = regulator_enable(info->vref);
  592. if (ret)
  593. return ret;
  594. ret = clk_prepare_enable(info->clk);
  595. if (ret)
  596. return ret;
  597. vf610_adc_hw_init(info);
  598. return 0;
  599. }
  600. #endif
  601. static SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops,
  602. vf610_adc_suspend,
  603. vf610_adc_resume);
  604. static struct platform_driver vf610_adc_driver = {
  605. .probe = vf610_adc_probe,
  606. .remove = vf610_adc_remove,
  607. .driver = {
  608. .name = DRIVER_NAME,
  609. .of_match_table = vf610_adc_match,
  610. .pm = &vf610_adc_pm_ops,
  611. },
  612. };
  613. module_platform_driver(vf610_adc_driver);
  614. MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
  615. MODULE_DESCRIPTION("Freescale VF610 ADC driver");
  616. MODULE_LICENSE("GPL v2");