vf610_adc.c 18 KB

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