vf610_adc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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_SHORT 0x100
  67. #define VF610_ADC_ADSTS_NORMAL 0x200
  68. #define VF610_ADC_ADSTS_LONG 0x300
  69. #define VF610_ADC_ADSTS_MASK 0x300
  70. #define VF610_ADC_ADLPC_EN 0x80
  71. #define VF610_ADC_ADHSC_EN 0x400
  72. #define VF610_ADC_REFSEL_VALT 0x100
  73. #define VF610_ADC_REFSEL_VBG 0x1000
  74. #define VF610_ADC_ADTRG_HARD 0x2000
  75. #define VF610_ADC_AVGS_8 0x4000
  76. #define VF610_ADC_AVGS_16 0x8000
  77. #define VF610_ADC_AVGS_32 0xC000
  78. #define VF610_ADC_AVGS_MASK 0xC000
  79. #define VF610_ADC_OVWREN 0x10000
  80. /* General control register field define */
  81. #define VF610_ADC_ADACKEN 0x1
  82. #define VF610_ADC_DMAEN 0x2
  83. #define VF610_ADC_ACREN 0x4
  84. #define VF610_ADC_ACFGT 0x8
  85. #define VF610_ADC_ACFE 0x10
  86. #define VF610_ADC_AVGEN 0x20
  87. #define VF610_ADC_ADCON 0x40
  88. #define VF610_ADC_CAL 0x80
  89. /* Other field define */
  90. #define VF610_ADC_ADCHC(x) ((x) & 0x1F)
  91. #define VF610_ADC_AIEN (0x1 << 7)
  92. #define VF610_ADC_CONV_DISABLE 0x1F
  93. #define VF610_ADC_HS_COCO0 0x1
  94. #define VF610_ADC_CALF 0x2
  95. #define VF610_ADC_TIMEOUT msecs_to_jiffies(100)
  96. #define DEFAULT_SAMPLE_TIME 1000
  97. enum clk_sel {
  98. VF610_ADCIOC_BUSCLK_SET,
  99. VF610_ADCIOC_ALTCLK_SET,
  100. VF610_ADCIOC_ADACK_SET,
  101. };
  102. enum vol_ref {
  103. VF610_ADCIOC_VR_VREF_SET,
  104. VF610_ADCIOC_VR_VALT_SET,
  105. VF610_ADCIOC_VR_VBG_SET,
  106. };
  107. enum average_sel {
  108. VF610_ADC_SAMPLE_1,
  109. VF610_ADC_SAMPLE_4,
  110. VF610_ADC_SAMPLE_8,
  111. VF610_ADC_SAMPLE_16,
  112. VF610_ADC_SAMPLE_32,
  113. };
  114. enum conversion_mode_sel {
  115. VF610_ADC_CONV_NORMAL,
  116. VF610_ADC_CONV_HIGH_SPEED,
  117. VF610_ADC_CONV_LOW_POWER,
  118. };
  119. enum lst_adder_sel {
  120. VF610_ADCK_CYCLES_3,
  121. VF610_ADCK_CYCLES_5,
  122. VF610_ADCK_CYCLES_7,
  123. VF610_ADCK_CYCLES_9,
  124. VF610_ADCK_CYCLES_13,
  125. VF610_ADCK_CYCLES_17,
  126. VF610_ADCK_CYCLES_21,
  127. VF610_ADCK_CYCLES_25,
  128. };
  129. struct vf610_adc_feature {
  130. enum clk_sel clk_sel;
  131. enum vol_ref vol_ref;
  132. enum conversion_mode_sel conv_mode;
  133. int clk_div;
  134. int sample_rate;
  135. int res_mode;
  136. u32 lst_adder_index;
  137. u32 default_sample_time;
  138. bool calibration;
  139. bool ovwren;
  140. };
  141. struct vf610_adc {
  142. struct device *dev;
  143. void __iomem *regs;
  144. struct clk *clk;
  145. u32 vref_uv;
  146. u32 value;
  147. struct regulator *vref;
  148. u32 max_adck_rate[3];
  149. struct vf610_adc_feature adc_feature;
  150. u32 sample_freq_avail[5];
  151. struct completion completion;
  152. };
  153. static const u32 vf610_hw_avgs[] = { 1, 4, 8, 16, 32 };
  154. static const u32 vf610_lst_adder[] = { 3, 5, 7, 9, 13, 17, 21, 25 };
  155. static inline void vf610_adc_calculate_rates(struct vf610_adc *info)
  156. {
  157. struct vf610_adc_feature *adc_feature = &info->adc_feature;
  158. unsigned long adck_rate, ipg_rate = clk_get_rate(info->clk);
  159. u32 adck_period, lst_addr_min;
  160. int divisor, i;
  161. adck_rate = info->max_adck_rate[adc_feature->conv_mode];
  162. if (adck_rate) {
  163. /* calculate clk divider which is within specification */
  164. divisor = ipg_rate / adck_rate;
  165. adc_feature->clk_div = 1 << fls(divisor + 1);
  166. } else {
  167. /* fall-back value using a safe divisor */
  168. adc_feature->clk_div = 8;
  169. }
  170. /*
  171. * Determine the long sample time adder value to be used based
  172. * on the default minimum sample time provided.
  173. */
  174. adck_period = NSEC_PER_SEC / adck_rate;
  175. lst_addr_min = adc_feature->default_sample_time / adck_period;
  176. for (i = 0; i < ARRAY_SIZE(vf610_lst_adder); i++) {
  177. if (vf610_lst_adder[i] > lst_addr_min) {
  178. adc_feature->lst_adder_index = i;
  179. break;
  180. }
  181. }
  182. /*
  183. * Calculate ADC sample frequencies
  184. * Sample time unit is ADCK cycles. ADCK clk source is ipg clock,
  185. * which is the same as bus clock.
  186. *
  187. * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
  188. * SFCAdder: fixed to 6 ADCK cycles
  189. * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
  190. * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
  191. * LSTAdder(Long Sample Time): 3, 5, 7, 9, 13, 17, 21, 25 ADCK cycles
  192. */
  193. adck_rate = ipg_rate / info->adc_feature.clk_div;
  194. for (i = 0; i < ARRAY_SIZE(vf610_hw_avgs); i++)
  195. info->sample_freq_avail[i] =
  196. adck_rate / (6 + vf610_hw_avgs[i] *
  197. (25 + vf610_lst_adder[adc_feature->lst_adder_index]));
  198. }
  199. static inline void vf610_adc_cfg_init(struct vf610_adc *info)
  200. {
  201. struct vf610_adc_feature *adc_feature = &info->adc_feature;
  202. /* set default Configuration for ADC controller */
  203. adc_feature->clk_sel = VF610_ADCIOC_BUSCLK_SET;
  204. adc_feature->vol_ref = VF610_ADCIOC_VR_VREF_SET;
  205. adc_feature->calibration = true;
  206. adc_feature->ovwren = true;
  207. adc_feature->res_mode = 12;
  208. adc_feature->sample_rate = 1;
  209. adc_feature->conv_mode = VF610_ADC_CONV_LOW_POWER;
  210. vf610_adc_calculate_rates(info);
  211. }
  212. static void vf610_adc_cfg_post_set(struct vf610_adc *info)
  213. {
  214. struct vf610_adc_feature *adc_feature = &info->adc_feature;
  215. int cfg_data = 0;
  216. int gc_data = 0;
  217. switch (adc_feature->clk_sel) {
  218. case VF610_ADCIOC_ALTCLK_SET:
  219. cfg_data |= VF610_ADC_ALTCLK_SEL;
  220. break;
  221. case VF610_ADCIOC_ADACK_SET:
  222. cfg_data |= VF610_ADC_ADACK_SEL;
  223. break;
  224. default:
  225. break;
  226. }
  227. /* low power set for calibration */
  228. cfg_data |= VF610_ADC_ADLPC_EN;
  229. /* enable high speed for calibration */
  230. cfg_data |= VF610_ADC_ADHSC_EN;
  231. /* voltage reference */
  232. switch (adc_feature->vol_ref) {
  233. case VF610_ADCIOC_VR_VREF_SET:
  234. break;
  235. case VF610_ADCIOC_VR_VALT_SET:
  236. cfg_data |= VF610_ADC_REFSEL_VALT;
  237. break;
  238. case VF610_ADCIOC_VR_VBG_SET:
  239. cfg_data |= VF610_ADC_REFSEL_VBG;
  240. break;
  241. default:
  242. dev_err(info->dev, "error voltage reference\n");
  243. }
  244. /* data overwrite enable */
  245. if (adc_feature->ovwren)
  246. cfg_data |= VF610_ADC_OVWREN;
  247. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  248. writel(gc_data, info->regs + VF610_REG_ADC_GC);
  249. }
  250. static void vf610_adc_calibration(struct vf610_adc *info)
  251. {
  252. int adc_gc, hc_cfg;
  253. if (!info->adc_feature.calibration)
  254. return;
  255. /* enable calibration interrupt */
  256. hc_cfg = VF610_ADC_AIEN | VF610_ADC_CONV_DISABLE;
  257. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  258. adc_gc = readl(info->regs + VF610_REG_ADC_GC);
  259. writel(adc_gc | VF610_ADC_CAL, info->regs + VF610_REG_ADC_GC);
  260. if (!wait_for_completion_timeout(&info->completion, VF610_ADC_TIMEOUT))
  261. dev_err(info->dev, "Timeout for adc calibration\n");
  262. adc_gc = readl(info->regs + VF610_REG_ADC_GS);
  263. if (adc_gc & VF610_ADC_CALF)
  264. dev_err(info->dev, "ADC calibration failed\n");
  265. info->adc_feature.calibration = false;
  266. }
  267. static void vf610_adc_cfg_set(struct vf610_adc *info)
  268. {
  269. struct vf610_adc_feature *adc_feature = &(info->adc_feature);
  270. int cfg_data;
  271. cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
  272. cfg_data &= ~VF610_ADC_ADLPC_EN;
  273. if (adc_feature->conv_mode == VF610_ADC_CONV_LOW_POWER)
  274. cfg_data |= VF610_ADC_ADLPC_EN;
  275. cfg_data &= ~VF610_ADC_ADHSC_EN;
  276. if (adc_feature->conv_mode == VF610_ADC_CONV_HIGH_SPEED)
  277. cfg_data |= VF610_ADC_ADHSC_EN;
  278. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  279. }
  280. static void vf610_adc_sample_set(struct vf610_adc *info)
  281. {
  282. struct vf610_adc_feature *adc_feature = &(info->adc_feature);
  283. int cfg_data, gc_data;
  284. cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
  285. gc_data = readl(info->regs + VF610_REG_ADC_GC);
  286. /* resolution mode */
  287. cfg_data &= ~VF610_ADC_MODE_MASK;
  288. switch (adc_feature->res_mode) {
  289. case 8:
  290. cfg_data |= VF610_ADC_MODE_BIT8;
  291. break;
  292. case 10:
  293. cfg_data |= VF610_ADC_MODE_BIT10;
  294. break;
  295. case 12:
  296. cfg_data |= VF610_ADC_MODE_BIT12;
  297. break;
  298. default:
  299. dev_err(info->dev, "error resolution mode\n");
  300. break;
  301. }
  302. /* clock select and clock divider */
  303. cfg_data &= ~(VF610_ADC_CLK_MASK | VF610_ADC_ADCCLK_MASK);
  304. switch (adc_feature->clk_div) {
  305. case 1:
  306. break;
  307. case 2:
  308. cfg_data |= VF610_ADC_CLK_DIV2;
  309. break;
  310. case 4:
  311. cfg_data |= VF610_ADC_CLK_DIV4;
  312. break;
  313. case 8:
  314. cfg_data |= VF610_ADC_CLK_DIV8;
  315. break;
  316. case 16:
  317. switch (adc_feature->clk_sel) {
  318. case VF610_ADCIOC_BUSCLK_SET:
  319. cfg_data |= VF610_ADC_BUSCLK2_SEL | VF610_ADC_CLK_DIV8;
  320. break;
  321. default:
  322. dev_err(info->dev, "error clk divider\n");
  323. break;
  324. }
  325. break;
  326. }
  327. /*
  328. * Set ADLSMP and ADSTS based on the Long Sample Time Adder value
  329. * determined.
  330. */
  331. switch (adc_feature->lst_adder_index) {
  332. case VF610_ADCK_CYCLES_3:
  333. break;
  334. case VF610_ADCK_CYCLES_5:
  335. cfg_data |= VF610_ADC_ADSTS_SHORT;
  336. break;
  337. case VF610_ADCK_CYCLES_7:
  338. cfg_data |= VF610_ADC_ADSTS_NORMAL;
  339. break;
  340. case VF610_ADCK_CYCLES_9:
  341. cfg_data |= VF610_ADC_ADSTS_LONG;
  342. break;
  343. case VF610_ADCK_CYCLES_13:
  344. cfg_data |= VF610_ADC_ADLSMP_LONG;
  345. break;
  346. case VF610_ADCK_CYCLES_17:
  347. cfg_data |= VF610_ADC_ADLSMP_LONG;
  348. cfg_data |= VF610_ADC_ADSTS_SHORT;
  349. break;
  350. case VF610_ADCK_CYCLES_21:
  351. cfg_data |= VF610_ADC_ADLSMP_LONG;
  352. cfg_data |= VF610_ADC_ADSTS_NORMAL;
  353. break;
  354. case VF610_ADCK_CYCLES_25:
  355. cfg_data |= VF610_ADC_ADLSMP_LONG;
  356. cfg_data |= VF610_ADC_ADSTS_NORMAL;
  357. break;
  358. default:
  359. dev_err(info->dev, "error in sample time select\n");
  360. }
  361. /* update hardware average selection */
  362. cfg_data &= ~VF610_ADC_AVGS_MASK;
  363. gc_data &= ~VF610_ADC_AVGEN;
  364. switch (adc_feature->sample_rate) {
  365. case VF610_ADC_SAMPLE_1:
  366. break;
  367. case VF610_ADC_SAMPLE_4:
  368. gc_data |= VF610_ADC_AVGEN;
  369. break;
  370. case VF610_ADC_SAMPLE_8:
  371. gc_data |= VF610_ADC_AVGEN;
  372. cfg_data |= VF610_ADC_AVGS_8;
  373. break;
  374. case VF610_ADC_SAMPLE_16:
  375. gc_data |= VF610_ADC_AVGEN;
  376. cfg_data |= VF610_ADC_AVGS_16;
  377. break;
  378. case VF610_ADC_SAMPLE_32:
  379. gc_data |= VF610_ADC_AVGEN;
  380. cfg_data |= VF610_ADC_AVGS_32;
  381. break;
  382. default:
  383. dev_err(info->dev,
  384. "error hardware sample average select\n");
  385. }
  386. writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
  387. writel(gc_data, info->regs + VF610_REG_ADC_GC);
  388. }
  389. static void vf610_adc_hw_init(struct vf610_adc *info)
  390. {
  391. /* CFG: Feature set */
  392. vf610_adc_cfg_post_set(info);
  393. vf610_adc_sample_set(info);
  394. /* adc calibration */
  395. vf610_adc_calibration(info);
  396. /* CFG: power and speed set */
  397. vf610_adc_cfg_set(info);
  398. }
  399. static int vf610_set_conversion_mode(struct iio_dev *indio_dev,
  400. const struct iio_chan_spec *chan,
  401. unsigned int mode)
  402. {
  403. struct vf610_adc *info = iio_priv(indio_dev);
  404. mutex_lock(&indio_dev->mlock);
  405. info->adc_feature.conv_mode = mode;
  406. vf610_adc_calculate_rates(info);
  407. vf610_adc_hw_init(info);
  408. mutex_unlock(&indio_dev->mlock);
  409. return 0;
  410. }
  411. static int vf610_get_conversion_mode(struct iio_dev *indio_dev,
  412. const struct iio_chan_spec *chan)
  413. {
  414. struct vf610_adc *info = iio_priv(indio_dev);
  415. return info->adc_feature.conv_mode;
  416. }
  417. static const char * const vf610_conv_modes[] = { "normal", "high-speed",
  418. "low-power" };
  419. static const struct iio_enum vf610_conversion_mode = {
  420. .items = vf610_conv_modes,
  421. .num_items = ARRAY_SIZE(vf610_conv_modes),
  422. .get = vf610_get_conversion_mode,
  423. .set = vf610_set_conversion_mode,
  424. };
  425. static const struct iio_chan_spec_ext_info vf610_ext_info[] = {
  426. IIO_ENUM("conversion_mode", IIO_SHARED_BY_DIR, &vf610_conversion_mode),
  427. {},
  428. };
  429. #define VF610_ADC_CHAN(_idx, _chan_type) { \
  430. .type = (_chan_type), \
  431. .indexed = 1, \
  432. .channel = (_idx), \
  433. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  434. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  435. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  436. .ext_info = vf610_ext_info, \
  437. }
  438. #define VF610_ADC_TEMPERATURE_CHAN(_idx, _chan_type) { \
  439. .type = (_chan_type), \
  440. .channel = (_idx), \
  441. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
  442. }
  443. static const struct iio_chan_spec vf610_adc_iio_channels[] = {
  444. VF610_ADC_CHAN(0, IIO_VOLTAGE),
  445. VF610_ADC_CHAN(1, IIO_VOLTAGE),
  446. VF610_ADC_CHAN(2, IIO_VOLTAGE),
  447. VF610_ADC_CHAN(3, IIO_VOLTAGE),
  448. VF610_ADC_CHAN(4, IIO_VOLTAGE),
  449. VF610_ADC_CHAN(5, IIO_VOLTAGE),
  450. VF610_ADC_CHAN(6, IIO_VOLTAGE),
  451. VF610_ADC_CHAN(7, IIO_VOLTAGE),
  452. VF610_ADC_CHAN(8, IIO_VOLTAGE),
  453. VF610_ADC_CHAN(9, IIO_VOLTAGE),
  454. VF610_ADC_CHAN(10, IIO_VOLTAGE),
  455. VF610_ADC_CHAN(11, IIO_VOLTAGE),
  456. VF610_ADC_CHAN(12, IIO_VOLTAGE),
  457. VF610_ADC_CHAN(13, IIO_VOLTAGE),
  458. VF610_ADC_CHAN(14, IIO_VOLTAGE),
  459. VF610_ADC_CHAN(15, IIO_VOLTAGE),
  460. VF610_ADC_TEMPERATURE_CHAN(26, IIO_TEMP),
  461. /* sentinel */
  462. };
  463. static int vf610_adc_read_data(struct vf610_adc *info)
  464. {
  465. int result;
  466. result = readl(info->regs + VF610_REG_ADC_R0);
  467. switch (info->adc_feature.res_mode) {
  468. case 8:
  469. result &= 0xFF;
  470. break;
  471. case 10:
  472. result &= 0x3FF;
  473. break;
  474. case 12:
  475. result &= 0xFFF;
  476. break;
  477. default:
  478. break;
  479. }
  480. return result;
  481. }
  482. static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
  483. {
  484. struct vf610_adc *info = (struct vf610_adc *)dev_id;
  485. int coco;
  486. coco = readl(info->regs + VF610_REG_ADC_HS);
  487. if (coco & VF610_ADC_HS_COCO0) {
  488. info->value = vf610_adc_read_data(info);
  489. complete(&info->completion);
  490. }
  491. return IRQ_HANDLED;
  492. }
  493. static ssize_t vf610_show_samp_freq_avail(struct device *dev,
  494. struct device_attribute *attr, char *buf)
  495. {
  496. struct vf610_adc *info = iio_priv(dev_to_iio_dev(dev));
  497. size_t len = 0;
  498. int i;
  499. for (i = 0; i < ARRAY_SIZE(info->sample_freq_avail); i++)
  500. len += scnprintf(buf + len, PAGE_SIZE - len,
  501. "%u ", info->sample_freq_avail[i]);
  502. /* replace trailing space by newline */
  503. buf[len - 1] = '\n';
  504. return len;
  505. }
  506. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail);
  507. static struct attribute *vf610_attributes[] = {
  508. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  509. NULL
  510. };
  511. static const struct attribute_group vf610_attribute_group = {
  512. .attrs = vf610_attributes,
  513. };
  514. static int vf610_read_raw(struct iio_dev *indio_dev,
  515. struct iio_chan_spec const *chan,
  516. int *val,
  517. int *val2,
  518. long mask)
  519. {
  520. struct vf610_adc *info = iio_priv(indio_dev);
  521. unsigned int hc_cfg;
  522. long ret;
  523. switch (mask) {
  524. case IIO_CHAN_INFO_RAW:
  525. case IIO_CHAN_INFO_PROCESSED:
  526. mutex_lock(&indio_dev->mlock);
  527. reinit_completion(&info->completion);
  528. hc_cfg = VF610_ADC_ADCHC(chan->channel);
  529. hc_cfg |= VF610_ADC_AIEN;
  530. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  531. ret = wait_for_completion_interruptible_timeout
  532. (&info->completion, VF610_ADC_TIMEOUT);
  533. if (ret == 0) {
  534. mutex_unlock(&indio_dev->mlock);
  535. return -ETIMEDOUT;
  536. }
  537. if (ret < 0) {
  538. mutex_unlock(&indio_dev->mlock);
  539. return ret;
  540. }
  541. switch (chan->type) {
  542. case IIO_VOLTAGE:
  543. *val = info->value;
  544. break;
  545. case IIO_TEMP:
  546. /*
  547. * Calculate in degree Celsius times 1000
  548. * Using sensor slope of 1.84 mV/°C and
  549. * V at 25°C of 696 mV
  550. */
  551. *val = 25000 - ((int)info->value - 864) * 1000000 / 1840;
  552. break;
  553. default:
  554. mutex_unlock(&indio_dev->mlock);
  555. return -EINVAL;
  556. }
  557. mutex_unlock(&indio_dev->mlock);
  558. return IIO_VAL_INT;
  559. case IIO_CHAN_INFO_SCALE:
  560. *val = info->vref_uv / 1000;
  561. *val2 = info->adc_feature.res_mode;
  562. return IIO_VAL_FRACTIONAL_LOG2;
  563. case IIO_CHAN_INFO_SAMP_FREQ:
  564. *val = info->sample_freq_avail[info->adc_feature.sample_rate];
  565. *val2 = 0;
  566. return IIO_VAL_INT;
  567. default:
  568. break;
  569. }
  570. return -EINVAL;
  571. }
  572. static int vf610_write_raw(struct iio_dev *indio_dev,
  573. struct iio_chan_spec const *chan,
  574. int val,
  575. int val2,
  576. long mask)
  577. {
  578. struct vf610_adc *info = iio_priv(indio_dev);
  579. int i;
  580. switch (mask) {
  581. case IIO_CHAN_INFO_SAMP_FREQ:
  582. for (i = 0;
  583. i < ARRAY_SIZE(info->sample_freq_avail);
  584. i++)
  585. if (val == info->sample_freq_avail[i]) {
  586. info->adc_feature.sample_rate = i;
  587. vf610_adc_sample_set(info);
  588. return 0;
  589. }
  590. break;
  591. default:
  592. break;
  593. }
  594. return -EINVAL;
  595. }
  596. static int vf610_adc_reg_access(struct iio_dev *indio_dev,
  597. unsigned reg, unsigned writeval,
  598. unsigned *readval)
  599. {
  600. struct vf610_adc *info = iio_priv(indio_dev);
  601. if ((readval == NULL) ||
  602. ((reg % 4) || (reg > VF610_REG_ADC_PCTL)))
  603. return -EINVAL;
  604. *readval = readl(info->regs + reg);
  605. return 0;
  606. }
  607. static const struct iio_info vf610_adc_iio_info = {
  608. .driver_module = THIS_MODULE,
  609. .read_raw = &vf610_read_raw,
  610. .write_raw = &vf610_write_raw,
  611. .debugfs_reg_access = &vf610_adc_reg_access,
  612. .attrs = &vf610_attribute_group,
  613. };
  614. static const struct of_device_id vf610_adc_match[] = {
  615. { .compatible = "fsl,vf610-adc", },
  616. { /* sentinel */ }
  617. };
  618. MODULE_DEVICE_TABLE(of, vf610_adc_match);
  619. static int vf610_adc_probe(struct platform_device *pdev)
  620. {
  621. struct vf610_adc *info;
  622. struct iio_dev *indio_dev;
  623. struct resource *mem;
  624. int irq;
  625. int ret;
  626. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct vf610_adc));
  627. if (!indio_dev) {
  628. dev_err(&pdev->dev, "Failed allocating iio device\n");
  629. return -ENOMEM;
  630. }
  631. info = iio_priv(indio_dev);
  632. info->dev = &pdev->dev;
  633. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  634. info->regs = devm_ioremap_resource(&pdev->dev, mem);
  635. if (IS_ERR(info->regs))
  636. return PTR_ERR(info->regs);
  637. irq = platform_get_irq(pdev, 0);
  638. if (irq < 0) {
  639. dev_err(&pdev->dev, "no irq resource?\n");
  640. return irq;
  641. }
  642. ret = devm_request_irq(info->dev, irq,
  643. vf610_adc_isr, 0,
  644. dev_name(&pdev->dev), info);
  645. if (ret < 0) {
  646. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", irq);
  647. return ret;
  648. }
  649. info->clk = devm_clk_get(&pdev->dev, "adc");
  650. if (IS_ERR(info->clk)) {
  651. dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
  652. PTR_ERR(info->clk));
  653. return PTR_ERR(info->clk);
  654. }
  655. info->vref = devm_regulator_get(&pdev->dev, "vref");
  656. if (IS_ERR(info->vref))
  657. return PTR_ERR(info->vref);
  658. ret = regulator_enable(info->vref);
  659. if (ret)
  660. return ret;
  661. info->vref_uv = regulator_get_voltage(info->vref);
  662. of_property_read_u32_array(pdev->dev.of_node, "fsl,adck-max-frequency",
  663. info->max_adck_rate, 3);
  664. ret = of_property_read_u32(pdev->dev.of_node, "min-sample-time",
  665. &info->adc_feature.default_sample_time);
  666. if (ret)
  667. info->adc_feature.default_sample_time = DEFAULT_SAMPLE_TIME;
  668. platform_set_drvdata(pdev, indio_dev);
  669. init_completion(&info->completion);
  670. indio_dev->name = dev_name(&pdev->dev);
  671. indio_dev->dev.parent = &pdev->dev;
  672. indio_dev->dev.of_node = pdev->dev.of_node;
  673. indio_dev->info = &vf610_adc_iio_info;
  674. indio_dev->modes = INDIO_DIRECT_MODE;
  675. indio_dev->channels = vf610_adc_iio_channels;
  676. indio_dev->num_channels = ARRAY_SIZE(vf610_adc_iio_channels);
  677. ret = clk_prepare_enable(info->clk);
  678. if (ret) {
  679. dev_err(&pdev->dev,
  680. "Could not prepare or enable the clock.\n");
  681. goto error_adc_clk_enable;
  682. }
  683. vf610_adc_cfg_init(info);
  684. vf610_adc_hw_init(info);
  685. ret = iio_device_register(indio_dev);
  686. if (ret) {
  687. dev_err(&pdev->dev, "Couldn't register the device.\n");
  688. goto error_iio_device_register;
  689. }
  690. return 0;
  691. error_iio_device_register:
  692. clk_disable_unprepare(info->clk);
  693. error_adc_clk_enable:
  694. regulator_disable(info->vref);
  695. return ret;
  696. }
  697. static int vf610_adc_remove(struct platform_device *pdev)
  698. {
  699. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  700. struct vf610_adc *info = iio_priv(indio_dev);
  701. iio_device_unregister(indio_dev);
  702. regulator_disable(info->vref);
  703. clk_disable_unprepare(info->clk);
  704. return 0;
  705. }
  706. #ifdef CONFIG_PM_SLEEP
  707. static int vf610_adc_suspend(struct device *dev)
  708. {
  709. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  710. struct vf610_adc *info = iio_priv(indio_dev);
  711. int hc_cfg;
  712. /* ADC controller enters to stop mode */
  713. hc_cfg = readl(info->regs + VF610_REG_ADC_HC0);
  714. hc_cfg |= VF610_ADC_CONV_DISABLE;
  715. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  716. clk_disable_unprepare(info->clk);
  717. regulator_disable(info->vref);
  718. return 0;
  719. }
  720. static int vf610_adc_resume(struct device *dev)
  721. {
  722. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  723. struct vf610_adc *info = iio_priv(indio_dev);
  724. int ret;
  725. ret = regulator_enable(info->vref);
  726. if (ret)
  727. return ret;
  728. ret = clk_prepare_enable(info->clk);
  729. if (ret)
  730. goto disable_reg;
  731. vf610_adc_hw_init(info);
  732. return 0;
  733. disable_reg:
  734. regulator_disable(info->vref);
  735. return ret;
  736. }
  737. #endif
  738. static SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops, vf610_adc_suspend, vf610_adc_resume);
  739. static struct platform_driver vf610_adc_driver = {
  740. .probe = vf610_adc_probe,
  741. .remove = vf610_adc_remove,
  742. .driver = {
  743. .name = DRIVER_NAME,
  744. .of_match_table = vf610_adc_match,
  745. .pm = &vf610_adc_pm_ops,
  746. },
  747. };
  748. module_platform_driver(vf610_adc_driver);
  749. MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
  750. MODULE_DESCRIPTION("Freescale VF610 ADC driver");
  751. MODULE_LICENSE("GPL v2");