vf610_adc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. struct completion completion;
  129. };
  130. #define VF610_ADC_CHAN(_idx, _chan_type) { \
  131. .type = (_chan_type), \
  132. .indexed = 1, \
  133. .channel = (_idx), \
  134. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  135. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  136. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  137. }
  138. static const struct iio_chan_spec vf610_adc_iio_channels[] = {
  139. VF610_ADC_CHAN(0, IIO_VOLTAGE),
  140. VF610_ADC_CHAN(1, IIO_VOLTAGE),
  141. VF610_ADC_CHAN(2, IIO_VOLTAGE),
  142. VF610_ADC_CHAN(3, IIO_VOLTAGE),
  143. VF610_ADC_CHAN(4, IIO_VOLTAGE),
  144. VF610_ADC_CHAN(5, IIO_VOLTAGE),
  145. VF610_ADC_CHAN(6, IIO_VOLTAGE),
  146. VF610_ADC_CHAN(7, IIO_VOLTAGE),
  147. VF610_ADC_CHAN(8, IIO_VOLTAGE),
  148. VF610_ADC_CHAN(9, IIO_VOLTAGE),
  149. VF610_ADC_CHAN(10, IIO_VOLTAGE),
  150. VF610_ADC_CHAN(11, IIO_VOLTAGE),
  151. VF610_ADC_CHAN(12, IIO_VOLTAGE),
  152. VF610_ADC_CHAN(13, IIO_VOLTAGE),
  153. VF610_ADC_CHAN(14, IIO_VOLTAGE),
  154. VF610_ADC_CHAN(15, IIO_VOLTAGE),
  155. /* sentinel */
  156. };
  157. /*
  158. * ADC sample frequency, unit is ADCK cycles.
  159. * ADC clk source is ipg clock, which is the same as bus clock.
  160. *
  161. * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
  162. * SFCAdder: fixed to 6 ADCK cycles
  163. * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
  164. * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
  165. * LSTAdder(Long Sample Time): fixed to 3 ADCK cycles
  166. *
  167. * By default, enable 12 bit resolution mode, clock source
  168. * set to ipg clock, So get below frequency group:
  169. */
  170. static const u32 vf610_sample_freq_avail[5] =
  171. {1941176, 559332, 286957, 145374, 73171};
  172. static inline void vf610_adc_cfg_init(struct vf610_adc *info)
  173. {
  174. /* set default Configuration for ADC controller */
  175. info->adc_feature.clk_sel = VF610_ADCIOC_BUSCLK_SET;
  176. info->adc_feature.vol_ref = VF610_ADCIOC_VR_VREF_SET;
  177. info->adc_feature.calibration = true;
  178. info->adc_feature.ovwren = true;
  179. info->adc_feature.clk_div = 1;
  180. info->adc_feature.res_mode = 12;
  181. info->adc_feature.sample_rate = 1;
  182. info->adc_feature.lpm = true;
  183. }
  184. static void vf610_adc_cfg_post_set(struct vf610_adc *info)
  185. {
  186. struct vf610_adc_feature *adc_feature = &info->adc_feature;
  187. int cfg_data = 0;
  188. int gc_data = 0;
  189. switch (adc_feature->clk_sel) {
  190. case VF610_ADCIOC_ALTCLK_SET:
  191. cfg_data |= VF610_ADC_ALTCLK_SEL;
  192. break;
  193. case VF610_ADCIOC_ADACK_SET:
  194. cfg_data |= VF610_ADC_ADACK_SEL;
  195. break;
  196. default:
  197. break;
  198. }
  199. /* low power set for calibration */
  200. cfg_data |= VF610_ADC_ADLPC_EN;
  201. /* enable high speed for calibration */
  202. cfg_data |= VF610_ADC_ADHSC_EN;
  203. /* voltage reference */
  204. switch (adc_feature->vol_ref) {
  205. case VF610_ADCIOC_VR_VREF_SET:
  206. break;
  207. case VF610_ADCIOC_VR_VALT_SET:
  208. cfg_data |= VF610_ADC_REFSEL_VALT;
  209. break;
  210. case VF610_ADCIOC_VR_VBG_SET:
  211. cfg_data |= VF610_ADC_REFSEL_VBG;
  212. break;
  213. default:
  214. dev_err(info->dev, "error voltage reference\n");
  215. }
  216. /* data overwrite enable */
  217. if (adc_feature->ovwren)
  218. cfg_data |= VF610_ADC_OVWREN;
  219. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  220. writel(gc_data, info->regs + VF610_REG_ADC_GC);
  221. }
  222. static void vf610_adc_calibration(struct vf610_adc *info)
  223. {
  224. int adc_gc, hc_cfg;
  225. int timeout;
  226. if (!info->adc_feature.calibration)
  227. return;
  228. /* enable calibration interrupt */
  229. hc_cfg = VF610_ADC_AIEN | VF610_ADC_CONV_DISABLE;
  230. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  231. adc_gc = readl(info->regs + VF610_REG_ADC_GC);
  232. writel(adc_gc | VF610_ADC_CAL, info->regs + VF610_REG_ADC_GC);
  233. timeout = wait_for_completion_timeout
  234. (&info->completion, VF610_ADC_TIMEOUT);
  235. if (timeout == 0)
  236. dev_err(info->dev, "Timeout for adc calibration\n");
  237. adc_gc = readl(info->regs + VF610_REG_ADC_GS);
  238. if (adc_gc & VF610_ADC_CALF)
  239. dev_err(info->dev, "ADC calibration failed\n");
  240. info->adc_feature.calibration = false;
  241. }
  242. static void vf610_adc_cfg_set(struct vf610_adc *info)
  243. {
  244. struct vf610_adc_feature *adc_feature = &(info->adc_feature);
  245. int cfg_data;
  246. cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
  247. /* low power configuration */
  248. cfg_data &= ~VF610_ADC_ADLPC_EN;
  249. if (adc_feature->lpm)
  250. cfg_data |= VF610_ADC_ADLPC_EN;
  251. /* disable high speed */
  252. cfg_data &= ~VF610_ADC_ADHSC_EN;
  253. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  254. }
  255. static void vf610_adc_sample_set(struct vf610_adc *info)
  256. {
  257. struct vf610_adc_feature *adc_feature = &(info->adc_feature);
  258. int cfg_data, gc_data;
  259. cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
  260. gc_data = readl(info->regs + VF610_REG_ADC_GC);
  261. /* resolution mode */
  262. cfg_data &= ~VF610_ADC_MODE_MASK;
  263. switch (adc_feature->res_mode) {
  264. case 8:
  265. cfg_data |= VF610_ADC_MODE_BIT8;
  266. break;
  267. case 10:
  268. cfg_data |= VF610_ADC_MODE_BIT10;
  269. break;
  270. case 12:
  271. cfg_data |= VF610_ADC_MODE_BIT12;
  272. break;
  273. default:
  274. dev_err(info->dev, "error resolution mode\n");
  275. break;
  276. }
  277. /* clock select and clock divider */
  278. cfg_data &= ~(VF610_ADC_CLK_MASK | VF610_ADC_ADCCLK_MASK);
  279. switch (adc_feature->clk_div) {
  280. case 1:
  281. break;
  282. case 2:
  283. cfg_data |= VF610_ADC_CLK_DIV2;
  284. break;
  285. case 4:
  286. cfg_data |= VF610_ADC_CLK_DIV4;
  287. break;
  288. case 8:
  289. cfg_data |= VF610_ADC_CLK_DIV8;
  290. break;
  291. case 16:
  292. switch (adc_feature->clk_sel) {
  293. case VF610_ADCIOC_BUSCLK_SET:
  294. cfg_data |= VF610_ADC_BUSCLK2_SEL | VF610_ADC_CLK_DIV8;
  295. break;
  296. default:
  297. dev_err(info->dev, "error clk divider\n");
  298. break;
  299. }
  300. break;
  301. }
  302. /* Use the short sample mode */
  303. cfg_data &= ~(VF610_ADC_ADLSMP_LONG | VF610_ADC_ADSTS_MASK);
  304. /* update hardware average selection */
  305. cfg_data &= ~VF610_ADC_AVGS_MASK;
  306. gc_data &= ~VF610_ADC_AVGEN;
  307. switch (adc_feature->sample_rate) {
  308. case VF610_ADC_SAMPLE_1:
  309. break;
  310. case VF610_ADC_SAMPLE_4:
  311. gc_data |= VF610_ADC_AVGEN;
  312. break;
  313. case VF610_ADC_SAMPLE_8:
  314. gc_data |= VF610_ADC_AVGEN;
  315. cfg_data |= VF610_ADC_AVGS_8;
  316. break;
  317. case VF610_ADC_SAMPLE_16:
  318. gc_data |= VF610_ADC_AVGEN;
  319. cfg_data |= VF610_ADC_AVGS_16;
  320. break;
  321. case VF610_ADC_SAMPLE_32:
  322. gc_data |= VF610_ADC_AVGEN;
  323. cfg_data |= VF610_ADC_AVGS_32;
  324. break;
  325. default:
  326. dev_err(info->dev,
  327. "error hardware sample average select\n");
  328. }
  329. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  330. writel(gc_data, info->regs + VF610_REG_ADC_GC);
  331. }
  332. static void vf610_adc_hw_init(struct vf610_adc *info)
  333. {
  334. /* CFG: Feature set */
  335. vf610_adc_cfg_post_set(info);
  336. vf610_adc_sample_set(info);
  337. /* adc calibration */
  338. vf610_adc_calibration(info);
  339. /* CFG: power and speed set */
  340. vf610_adc_cfg_set(info);
  341. }
  342. static int vf610_adc_read_data(struct vf610_adc *info)
  343. {
  344. int result;
  345. result = readl(info->regs + VF610_REG_ADC_R0);
  346. switch (info->adc_feature.res_mode) {
  347. case 8:
  348. result &= 0xFF;
  349. break;
  350. case 10:
  351. result &= 0x3FF;
  352. break;
  353. case 12:
  354. result &= 0xFFF;
  355. break;
  356. default:
  357. break;
  358. }
  359. return result;
  360. }
  361. static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
  362. {
  363. struct vf610_adc *info = (struct vf610_adc *)dev_id;
  364. int coco;
  365. coco = readl(info->regs + VF610_REG_ADC_HS);
  366. if (coco & VF610_ADC_HS_COCO0) {
  367. info->value = vf610_adc_read_data(info);
  368. complete(&info->completion);
  369. }
  370. return IRQ_HANDLED;
  371. }
  372. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1941176, 559332, 286957, 145374, 73171");
  373. static struct attribute *vf610_attributes[] = {
  374. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  375. NULL
  376. };
  377. static const struct attribute_group vf610_attribute_group = {
  378. .attrs = vf610_attributes,
  379. };
  380. static int vf610_read_raw(struct iio_dev *indio_dev,
  381. struct iio_chan_spec const *chan,
  382. int *val,
  383. int *val2,
  384. long mask)
  385. {
  386. struct vf610_adc *info = iio_priv(indio_dev);
  387. unsigned int hc_cfg;
  388. long ret;
  389. switch (mask) {
  390. case IIO_CHAN_INFO_RAW:
  391. mutex_lock(&indio_dev->mlock);
  392. reinit_completion(&info->completion);
  393. hc_cfg = VF610_ADC_ADCHC(chan->channel);
  394. hc_cfg |= VF610_ADC_AIEN;
  395. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  396. ret = wait_for_completion_interruptible_timeout
  397. (&info->completion, VF610_ADC_TIMEOUT);
  398. if (ret == 0) {
  399. mutex_unlock(&indio_dev->mlock);
  400. return -ETIMEDOUT;
  401. }
  402. if (ret < 0) {
  403. mutex_unlock(&indio_dev->mlock);
  404. return ret;
  405. }
  406. *val = info->value;
  407. mutex_unlock(&indio_dev->mlock);
  408. return IIO_VAL_INT;
  409. case IIO_CHAN_INFO_SCALE:
  410. *val = info->vref_uv / 1000;
  411. *val2 = info->adc_feature.res_mode;
  412. return IIO_VAL_FRACTIONAL_LOG2;
  413. case IIO_CHAN_INFO_SAMP_FREQ:
  414. *val = vf610_sample_freq_avail[info->adc_feature.sample_rate];
  415. *val2 = 0;
  416. return IIO_VAL_INT;
  417. default:
  418. break;
  419. }
  420. return -EINVAL;
  421. }
  422. static int vf610_write_raw(struct iio_dev *indio_dev,
  423. struct iio_chan_spec const *chan,
  424. int val,
  425. int val2,
  426. long mask)
  427. {
  428. struct vf610_adc *info = iio_priv(indio_dev);
  429. int i;
  430. switch (mask) {
  431. case IIO_CHAN_INFO_SAMP_FREQ:
  432. for (i = 0;
  433. i < ARRAY_SIZE(vf610_sample_freq_avail);
  434. i++)
  435. if (val == vf610_sample_freq_avail[i]) {
  436. info->adc_feature.sample_rate = i;
  437. vf610_adc_sample_set(info);
  438. return 0;
  439. }
  440. break;
  441. default:
  442. break;
  443. }
  444. return -EINVAL;
  445. }
  446. static int vf610_adc_reg_access(struct iio_dev *indio_dev,
  447. unsigned reg, unsigned writeval,
  448. unsigned *readval)
  449. {
  450. struct vf610_adc *info = iio_priv(indio_dev);
  451. if ((readval == NULL) ||
  452. (!(reg % 4) || (reg > VF610_REG_ADC_PCTL)))
  453. return -EINVAL;
  454. *readval = readl(info->regs + reg);
  455. return 0;
  456. }
  457. static const struct iio_info vf610_adc_iio_info = {
  458. .driver_module = THIS_MODULE,
  459. .read_raw = &vf610_read_raw,
  460. .write_raw = &vf610_write_raw,
  461. .debugfs_reg_access = &vf610_adc_reg_access,
  462. .attrs = &vf610_attribute_group,
  463. };
  464. static const struct of_device_id vf610_adc_match[] = {
  465. { .compatible = "fsl,vf610-adc", },
  466. { /* sentinel */ }
  467. };
  468. MODULE_DEVICE_TABLE(of, vf610_adc_match);
  469. static int vf610_adc_probe(struct platform_device *pdev)
  470. {
  471. struct vf610_adc *info;
  472. struct iio_dev *indio_dev;
  473. struct resource *mem;
  474. int irq;
  475. int ret;
  476. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct vf610_adc));
  477. if (!indio_dev) {
  478. dev_err(&pdev->dev, "Failed allocating iio device\n");
  479. return -ENOMEM;
  480. }
  481. info = iio_priv(indio_dev);
  482. info->dev = &pdev->dev;
  483. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  484. info->regs = devm_ioremap_resource(&pdev->dev, mem);
  485. if (IS_ERR(info->regs))
  486. return PTR_ERR(info->regs);
  487. irq = platform_get_irq(pdev, 0);
  488. if (irq <= 0) {
  489. dev_err(&pdev->dev, "no irq resource?\n");
  490. return -EINVAL;
  491. }
  492. ret = devm_request_irq(info->dev, irq,
  493. vf610_adc_isr, 0,
  494. dev_name(&pdev->dev), info);
  495. if (ret < 0) {
  496. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", irq);
  497. return ret;
  498. }
  499. info->clk = devm_clk_get(&pdev->dev, "adc");
  500. if (IS_ERR(info->clk)) {
  501. dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
  502. PTR_ERR(info->clk));
  503. ret = PTR_ERR(info->clk);
  504. return ret;
  505. }
  506. info->vref = devm_regulator_get(&pdev->dev, "vref");
  507. if (IS_ERR(info->vref))
  508. return PTR_ERR(info->vref);
  509. ret = regulator_enable(info->vref);
  510. if (ret)
  511. return ret;
  512. info->vref_uv = regulator_get_voltage(info->vref);
  513. platform_set_drvdata(pdev, indio_dev);
  514. init_completion(&info->completion);
  515. indio_dev->name = dev_name(&pdev->dev);
  516. indio_dev->dev.parent = &pdev->dev;
  517. indio_dev->dev.of_node = pdev->dev.of_node;
  518. indio_dev->info = &vf610_adc_iio_info;
  519. indio_dev->modes = INDIO_DIRECT_MODE;
  520. indio_dev->channels = vf610_adc_iio_channels;
  521. indio_dev->num_channels = ARRAY_SIZE(vf610_adc_iio_channels);
  522. ret = clk_prepare_enable(info->clk);
  523. if (ret) {
  524. dev_err(&pdev->dev,
  525. "Could not prepare or enable the clock.\n");
  526. goto error_adc_clk_enable;
  527. }
  528. vf610_adc_cfg_init(info);
  529. vf610_adc_hw_init(info);
  530. ret = iio_device_register(indio_dev);
  531. if (ret) {
  532. dev_err(&pdev->dev, "Couldn't register the device.\n");
  533. goto error_iio_device_register;
  534. }
  535. return 0;
  536. error_iio_device_register:
  537. clk_disable_unprepare(info->clk);
  538. error_adc_clk_enable:
  539. regulator_disable(info->vref);
  540. return ret;
  541. }
  542. static int vf610_adc_remove(struct platform_device *pdev)
  543. {
  544. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  545. struct vf610_adc *info = iio_priv(indio_dev);
  546. iio_device_unregister(indio_dev);
  547. regulator_disable(info->vref);
  548. clk_disable_unprepare(info->clk);
  549. return 0;
  550. }
  551. #ifdef CONFIG_PM_SLEEP
  552. static int vf610_adc_suspend(struct device *dev)
  553. {
  554. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  555. struct vf610_adc *info = iio_priv(indio_dev);
  556. int hc_cfg;
  557. /* ADC controller enters to stop mode */
  558. hc_cfg = readl(info->regs + VF610_REG_ADC_HC0);
  559. hc_cfg |= VF610_ADC_CONV_DISABLE;
  560. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  561. clk_disable_unprepare(info->clk);
  562. regulator_disable(info->vref);
  563. return 0;
  564. }
  565. static int vf610_adc_resume(struct device *dev)
  566. {
  567. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  568. struct vf610_adc *info = iio_priv(indio_dev);
  569. int ret;
  570. ret = regulator_enable(info->vref);
  571. if (ret)
  572. return ret;
  573. ret = clk_prepare_enable(info->clk);
  574. if (ret)
  575. return ret;
  576. vf610_adc_hw_init(info);
  577. return 0;
  578. }
  579. #endif
  580. static SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops,
  581. vf610_adc_suspend,
  582. vf610_adc_resume);
  583. static struct platform_driver vf610_adc_driver = {
  584. .probe = vf610_adc_probe,
  585. .remove = vf610_adc_remove,
  586. .driver = {
  587. .name = DRIVER_NAME,
  588. .owner = THIS_MODULE,
  589. .of_match_table = vf610_adc_match,
  590. .pm = &vf610_adc_pm_ops,
  591. },
  592. };
  593. module_platform_driver(vf610_adc_driver);
  594. MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
  595. MODULE_DESCRIPTION("Freescale VF610 ADC driver");
  596. MODULE_LICENSE("GPL v2");