vf610_adc.c 24 KB

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