vf610_adc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale Vybrid vf610 ADC driver
  4. *
  5. * Copyright 2013 Freescale Semiconductor, Inc.
  6. */
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/property.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/clk.h>
  17. #include <linux/completion.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/err.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/trigger.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #include <linux/iio/triggered_buffer.h>
  26. /* This will be the driver name the kernel reports */
  27. #define DRIVER_NAME "vf610-adc"
  28. /* Vybrid/IMX ADC registers */
  29. #define VF610_REG_ADC_HC0 0x00
  30. #define VF610_REG_ADC_HC1 0x04
  31. #define VF610_REG_ADC_HS 0x08
  32. #define VF610_REG_ADC_R0 0x0c
  33. #define VF610_REG_ADC_R1 0x10
  34. #define VF610_REG_ADC_CFG 0x14
  35. #define VF610_REG_ADC_GC 0x18
  36. #define VF610_REG_ADC_GS 0x1c
  37. #define VF610_REG_ADC_CV 0x20
  38. #define VF610_REG_ADC_OFS 0x24
  39. #define VF610_REG_ADC_CAL 0x28
  40. #define VF610_REG_ADC_PCTL 0x30
  41. /* Configuration register field define */
  42. #define VF610_ADC_MODE_BIT8 0x00
  43. #define VF610_ADC_MODE_BIT10 0x04
  44. #define VF610_ADC_MODE_BIT12 0x08
  45. #define VF610_ADC_MODE_MASK 0x0c
  46. #define VF610_ADC_BUSCLK2_SEL 0x01
  47. #define VF610_ADC_ALTCLK_SEL 0x02
  48. #define VF610_ADC_ADACK_SEL 0x03
  49. #define VF610_ADC_ADCCLK_MASK 0x03
  50. #define VF610_ADC_CLK_DIV2 0x20
  51. #define VF610_ADC_CLK_DIV4 0x40
  52. #define VF610_ADC_CLK_DIV8 0x60
  53. #define VF610_ADC_CLK_MASK 0x60
  54. #define VF610_ADC_ADLSMP_LONG 0x10
  55. #define VF610_ADC_ADSTS_SHORT 0x100
  56. #define VF610_ADC_ADSTS_NORMAL 0x200
  57. #define VF610_ADC_ADSTS_LONG 0x300
  58. #define VF610_ADC_ADSTS_MASK 0x300
  59. #define VF610_ADC_ADLPC_EN 0x80
  60. #define VF610_ADC_ADHSC_EN 0x400
  61. #define VF610_ADC_REFSEL_VALT 0x800
  62. #define VF610_ADC_REFSEL_VBG 0x1000
  63. #define VF610_ADC_ADTRG_HARD 0x2000
  64. #define VF610_ADC_AVGS_8 0x4000
  65. #define VF610_ADC_AVGS_16 0x8000
  66. #define VF610_ADC_AVGS_32 0xC000
  67. #define VF610_ADC_AVGS_MASK 0xC000
  68. #define VF610_ADC_OVWREN 0x10000
  69. /* General control register field define */
  70. #define VF610_ADC_ADACKEN 0x1
  71. #define VF610_ADC_DMAEN 0x2
  72. #define VF610_ADC_ACREN 0x4
  73. #define VF610_ADC_ACFGT 0x8
  74. #define VF610_ADC_ACFE 0x10
  75. #define VF610_ADC_AVGEN 0x20
  76. #define VF610_ADC_ADCON 0x40
  77. #define VF610_ADC_CAL 0x80
  78. /* Other field define */
  79. #define VF610_ADC_ADCHC(x) ((x) & 0x1F)
  80. #define VF610_ADC_AIEN (0x1 << 7)
  81. #define VF610_ADC_CONV_DISABLE 0x1F
  82. #define VF610_ADC_HS_COCO0 0x1
  83. #define VF610_ADC_CALF 0x2
  84. #define VF610_ADC_TIMEOUT msecs_to_jiffies(100)
  85. #define DEFAULT_SAMPLE_TIME 1000
  86. /* V at 25°C of 696 mV */
  87. #define VF610_VTEMP25_3V0 950
  88. /* V at 25°C of 699 mV */
  89. #define VF610_VTEMP25_3V3 867
  90. /* Typical sensor slope coefficient at all temperatures */
  91. #define VF610_TEMP_SLOPE_COEFF 1840
  92. enum clk_sel {
  93. VF610_ADCIOC_BUSCLK_SET,
  94. VF610_ADCIOC_ALTCLK_SET,
  95. VF610_ADCIOC_ADACK_SET,
  96. };
  97. enum vol_ref {
  98. VF610_ADCIOC_VR_VREF_SET,
  99. VF610_ADCIOC_VR_VALT_SET,
  100. VF610_ADCIOC_VR_VBG_SET,
  101. };
  102. enum average_sel {
  103. VF610_ADC_SAMPLE_1,
  104. VF610_ADC_SAMPLE_4,
  105. VF610_ADC_SAMPLE_8,
  106. VF610_ADC_SAMPLE_16,
  107. VF610_ADC_SAMPLE_32,
  108. };
  109. enum conversion_mode_sel {
  110. VF610_ADC_CONV_NORMAL,
  111. VF610_ADC_CONV_HIGH_SPEED,
  112. VF610_ADC_CONV_LOW_POWER,
  113. };
  114. enum lst_adder_sel {
  115. VF610_ADCK_CYCLES_3,
  116. VF610_ADCK_CYCLES_5,
  117. VF610_ADCK_CYCLES_7,
  118. VF610_ADCK_CYCLES_9,
  119. VF610_ADCK_CYCLES_13,
  120. VF610_ADCK_CYCLES_17,
  121. VF610_ADCK_CYCLES_21,
  122. VF610_ADCK_CYCLES_25,
  123. };
  124. struct vf610_adc_feature {
  125. enum clk_sel clk_sel;
  126. enum vol_ref vol_ref;
  127. enum conversion_mode_sel conv_mode;
  128. int clk_div;
  129. int sample_rate;
  130. int res_mode;
  131. u32 lst_adder_index;
  132. u32 default_sample_time;
  133. bool calibration;
  134. bool ovwren;
  135. };
  136. struct vf610_adc {
  137. struct device *dev;
  138. void __iomem *regs;
  139. struct clk *clk;
  140. u32 vref_uv;
  141. u32 value;
  142. struct regulator *vref;
  143. u32 max_adck_rate[3];
  144. struct vf610_adc_feature adc_feature;
  145. u32 sample_freq_avail[5];
  146. struct completion completion;
  147. /* Ensure the timestamp is naturally aligned */
  148. struct {
  149. u16 chan;
  150. s64 timestamp __aligned(8);
  151. } scan;
  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. adck_rate = ipg_rate / adc_feature->clk_div;
  171. /*
  172. * Determine the long sample time adder value to be used based
  173. * on the default minimum sample time provided.
  174. */
  175. adck_period = NSEC_PER_SEC / adck_rate;
  176. lst_addr_min = adc_feature->default_sample_time / adck_period;
  177. for (i = 0; i < ARRAY_SIZE(vf610_lst_adder); i++) {
  178. if (vf610_lst_adder[i] > lst_addr_min) {
  179. adc_feature->lst_adder_index = i;
  180. break;
  181. }
  182. }
  183. /*
  184. * Calculate ADC sample frequencies
  185. * Sample time unit is ADCK cycles. ADCK clk source is ipg clock,
  186. * which is the same as bus clock.
  187. *
  188. * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
  189. * SFCAdder: fixed to 6 ADCK cycles
  190. * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
  191. * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
  192. * LSTAdder(Long Sample Time): 3, 5, 7, 9, 13, 17, 21, 25 ADCK cycles
  193. */
  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. .scan_index = (_idx), \
  438. .scan_type = { \
  439. .sign = 'u', \
  440. .realbits = 12, \
  441. .storagebits = 16, \
  442. }, \
  443. }
  444. #define VF610_ADC_TEMPERATURE_CHAN(_idx, _chan_type) { \
  445. .type = (_chan_type), \
  446. .channel = (_idx), \
  447. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
  448. .scan_index = (_idx), \
  449. .scan_type = { \
  450. .sign = 'u', \
  451. .realbits = 12, \
  452. .storagebits = 16, \
  453. }, \
  454. }
  455. static const struct iio_chan_spec vf610_adc_iio_channels[] = {
  456. VF610_ADC_CHAN(0, IIO_VOLTAGE),
  457. VF610_ADC_CHAN(1, IIO_VOLTAGE),
  458. VF610_ADC_CHAN(2, IIO_VOLTAGE),
  459. VF610_ADC_CHAN(3, IIO_VOLTAGE),
  460. VF610_ADC_CHAN(4, IIO_VOLTAGE),
  461. VF610_ADC_CHAN(5, IIO_VOLTAGE),
  462. VF610_ADC_CHAN(6, IIO_VOLTAGE),
  463. VF610_ADC_CHAN(7, IIO_VOLTAGE),
  464. VF610_ADC_CHAN(8, IIO_VOLTAGE),
  465. VF610_ADC_CHAN(9, IIO_VOLTAGE),
  466. VF610_ADC_CHAN(10, IIO_VOLTAGE),
  467. VF610_ADC_CHAN(11, IIO_VOLTAGE),
  468. VF610_ADC_CHAN(12, IIO_VOLTAGE),
  469. VF610_ADC_CHAN(13, IIO_VOLTAGE),
  470. VF610_ADC_CHAN(14, IIO_VOLTAGE),
  471. VF610_ADC_CHAN(15, IIO_VOLTAGE),
  472. VF610_ADC_TEMPERATURE_CHAN(26, IIO_TEMP),
  473. IIO_CHAN_SOFT_TIMESTAMP(32),
  474. /* sentinel */
  475. };
  476. static int vf610_adc_read_data(struct vf610_adc *info)
  477. {
  478. int result;
  479. result = readl(info->regs + VF610_REG_ADC_R0);
  480. switch (info->adc_feature.res_mode) {
  481. case 8:
  482. result &= 0xFF;
  483. break;
  484. case 10:
  485. result &= 0x3FF;
  486. break;
  487. case 12:
  488. result &= 0xFFF;
  489. break;
  490. default:
  491. break;
  492. }
  493. return result;
  494. }
  495. static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
  496. {
  497. struct iio_dev *indio_dev = dev_id;
  498. struct vf610_adc *info = iio_priv(indio_dev);
  499. int coco;
  500. coco = readl(info->regs + VF610_REG_ADC_HS);
  501. if (coco & VF610_ADC_HS_COCO0) {
  502. info->value = vf610_adc_read_data(info);
  503. if (iio_buffer_enabled(indio_dev)) {
  504. info->scan.chan = info->value;
  505. iio_push_to_buffers_with_timestamp(indio_dev,
  506. &info->scan,
  507. iio_get_time_ns(indio_dev));
  508. iio_trigger_notify_done(indio_dev->trig);
  509. } else
  510. complete(&info->completion);
  511. }
  512. return IRQ_HANDLED;
  513. }
  514. static ssize_t vf610_show_samp_freq_avail(struct device *dev,
  515. struct device_attribute *attr, char *buf)
  516. {
  517. struct vf610_adc *info = iio_priv(dev_to_iio_dev(dev));
  518. size_t len = 0;
  519. int i;
  520. for (i = 0; i < ARRAY_SIZE(info->sample_freq_avail); i++)
  521. len += scnprintf(buf + len, PAGE_SIZE - len,
  522. "%u ", info->sample_freq_avail[i]);
  523. /* replace trailing space by newline */
  524. buf[len - 1] = '\n';
  525. return len;
  526. }
  527. static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail);
  528. static struct attribute *vf610_attributes[] = {
  529. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  530. NULL
  531. };
  532. static const struct attribute_group vf610_attribute_group = {
  533. .attrs = vf610_attributes,
  534. };
  535. static int vf610_read_raw(struct iio_dev *indio_dev,
  536. struct iio_chan_spec const *chan,
  537. int *val,
  538. int *val2,
  539. long mask)
  540. {
  541. struct vf610_adc *info = iio_priv(indio_dev);
  542. unsigned int hc_cfg;
  543. long ret;
  544. switch (mask) {
  545. case IIO_CHAN_INFO_RAW:
  546. case IIO_CHAN_INFO_PROCESSED:
  547. mutex_lock(&indio_dev->mlock);
  548. if (iio_buffer_enabled(indio_dev)) {
  549. mutex_unlock(&indio_dev->mlock);
  550. return -EBUSY;
  551. }
  552. reinit_completion(&info->completion);
  553. hc_cfg = VF610_ADC_ADCHC(chan->channel);
  554. hc_cfg |= VF610_ADC_AIEN;
  555. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  556. ret = wait_for_completion_interruptible_timeout
  557. (&info->completion, VF610_ADC_TIMEOUT);
  558. if (ret == 0) {
  559. mutex_unlock(&indio_dev->mlock);
  560. return -ETIMEDOUT;
  561. }
  562. if (ret < 0) {
  563. mutex_unlock(&indio_dev->mlock);
  564. return ret;
  565. }
  566. switch (chan->type) {
  567. case IIO_VOLTAGE:
  568. *val = info->value;
  569. break;
  570. case IIO_TEMP:
  571. /*
  572. * Calculate in degree Celsius times 1000
  573. * Using the typical sensor slope of 1.84 mV/°C
  574. * and VREFH_ADC at 3.3V, V at 25°C of 699 mV
  575. */
  576. *val = 25000 - ((int)info->value - VF610_VTEMP25_3V3) *
  577. 1000000 / VF610_TEMP_SLOPE_COEFF;
  578. break;
  579. default:
  580. mutex_unlock(&indio_dev->mlock);
  581. return -EINVAL;
  582. }
  583. mutex_unlock(&indio_dev->mlock);
  584. return IIO_VAL_INT;
  585. case IIO_CHAN_INFO_SCALE:
  586. *val = info->vref_uv / 1000;
  587. *val2 = info->adc_feature.res_mode;
  588. return IIO_VAL_FRACTIONAL_LOG2;
  589. case IIO_CHAN_INFO_SAMP_FREQ:
  590. *val = info->sample_freq_avail[info->adc_feature.sample_rate];
  591. *val2 = 0;
  592. return IIO_VAL_INT;
  593. default:
  594. break;
  595. }
  596. return -EINVAL;
  597. }
  598. static int vf610_write_raw(struct iio_dev *indio_dev,
  599. struct iio_chan_spec const *chan,
  600. int val,
  601. int val2,
  602. long mask)
  603. {
  604. struct vf610_adc *info = iio_priv(indio_dev);
  605. int i;
  606. switch (mask) {
  607. case IIO_CHAN_INFO_SAMP_FREQ:
  608. for (i = 0;
  609. i < ARRAY_SIZE(info->sample_freq_avail);
  610. i++)
  611. if (val == info->sample_freq_avail[i]) {
  612. info->adc_feature.sample_rate = i;
  613. vf610_adc_sample_set(info);
  614. return 0;
  615. }
  616. break;
  617. default:
  618. break;
  619. }
  620. return -EINVAL;
  621. }
  622. static int vf610_adc_buffer_postenable(struct iio_dev *indio_dev)
  623. {
  624. struct vf610_adc *info = iio_priv(indio_dev);
  625. unsigned int channel;
  626. int val;
  627. val = readl(info->regs + VF610_REG_ADC_GC);
  628. val |= VF610_ADC_ADCON;
  629. writel(val, info->regs + VF610_REG_ADC_GC);
  630. channel = find_first_bit(indio_dev->active_scan_mask,
  631. indio_dev->masklength);
  632. val = VF610_ADC_ADCHC(channel);
  633. val |= VF610_ADC_AIEN;
  634. writel(val, info->regs + VF610_REG_ADC_HC0);
  635. return 0;
  636. }
  637. static int vf610_adc_buffer_predisable(struct iio_dev *indio_dev)
  638. {
  639. struct vf610_adc *info = iio_priv(indio_dev);
  640. unsigned int hc_cfg = 0;
  641. int val;
  642. val = readl(info->regs + VF610_REG_ADC_GC);
  643. val &= ~VF610_ADC_ADCON;
  644. writel(val, info->regs + VF610_REG_ADC_GC);
  645. hc_cfg |= VF610_ADC_CONV_DISABLE;
  646. hc_cfg &= ~VF610_ADC_AIEN;
  647. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  648. return 0;
  649. }
  650. static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
  651. .postenable = &vf610_adc_buffer_postenable,
  652. .predisable = &vf610_adc_buffer_predisable,
  653. .validate_scan_mask = &iio_validate_scan_mask_onehot,
  654. };
  655. static int vf610_adc_reg_access(struct iio_dev *indio_dev,
  656. unsigned reg, unsigned writeval,
  657. unsigned *readval)
  658. {
  659. struct vf610_adc *info = iio_priv(indio_dev);
  660. if ((readval == NULL) ||
  661. ((reg % 4) || (reg > VF610_REG_ADC_PCTL)))
  662. return -EINVAL;
  663. *readval = readl(info->regs + reg);
  664. return 0;
  665. }
  666. static const struct iio_info vf610_adc_iio_info = {
  667. .read_raw = &vf610_read_raw,
  668. .write_raw = &vf610_write_raw,
  669. .debugfs_reg_access = &vf610_adc_reg_access,
  670. .attrs = &vf610_attribute_group,
  671. };
  672. static const struct of_device_id vf610_adc_match[] = {
  673. { .compatible = "fsl,vf610-adc", },
  674. { /* sentinel */ }
  675. };
  676. MODULE_DEVICE_TABLE(of, vf610_adc_match);
  677. static int vf610_adc_probe(struct platform_device *pdev)
  678. {
  679. struct device *dev = &pdev->dev;
  680. struct vf610_adc *info;
  681. struct iio_dev *indio_dev;
  682. int irq;
  683. int ret;
  684. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct vf610_adc));
  685. if (!indio_dev) {
  686. dev_err(&pdev->dev, "Failed allocating iio device\n");
  687. return -ENOMEM;
  688. }
  689. info = iio_priv(indio_dev);
  690. info->dev = &pdev->dev;
  691. info->regs = devm_platform_ioremap_resource(pdev, 0);
  692. if (IS_ERR(info->regs))
  693. return PTR_ERR(info->regs);
  694. irq = platform_get_irq(pdev, 0);
  695. if (irq < 0)
  696. return irq;
  697. ret = devm_request_irq(info->dev, irq,
  698. vf610_adc_isr, 0,
  699. dev_name(&pdev->dev), indio_dev);
  700. if (ret < 0) {
  701. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", irq);
  702. return ret;
  703. }
  704. info->clk = devm_clk_get(&pdev->dev, "adc");
  705. if (IS_ERR(info->clk)) {
  706. dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
  707. PTR_ERR(info->clk));
  708. return PTR_ERR(info->clk);
  709. }
  710. info->vref = devm_regulator_get(&pdev->dev, "vref");
  711. if (IS_ERR(info->vref))
  712. return PTR_ERR(info->vref);
  713. ret = regulator_enable(info->vref);
  714. if (ret)
  715. return ret;
  716. info->vref_uv = regulator_get_voltage(info->vref);
  717. device_property_read_u32_array(dev, "fsl,adck-max-frequency", info->max_adck_rate, 3);
  718. info->adc_feature.default_sample_time = DEFAULT_SAMPLE_TIME;
  719. device_property_read_u32(dev, "min-sample-time", &info->adc_feature.default_sample_time);
  720. platform_set_drvdata(pdev, indio_dev);
  721. init_completion(&info->completion);
  722. indio_dev->name = dev_name(&pdev->dev);
  723. indio_dev->info = &vf610_adc_iio_info;
  724. indio_dev->modes = INDIO_DIRECT_MODE;
  725. indio_dev->channels = vf610_adc_iio_channels;
  726. indio_dev->num_channels = ARRAY_SIZE(vf610_adc_iio_channels);
  727. ret = clk_prepare_enable(info->clk);
  728. if (ret) {
  729. dev_err(&pdev->dev,
  730. "Could not prepare or enable the clock.\n");
  731. goto error_adc_clk_enable;
  732. }
  733. vf610_adc_cfg_init(info);
  734. vf610_adc_hw_init(info);
  735. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  736. NULL, &iio_triggered_buffer_setup_ops);
  737. if (ret < 0) {
  738. dev_err(&pdev->dev, "Couldn't initialise the buffer\n");
  739. goto error_iio_device_register;
  740. }
  741. ret = iio_device_register(indio_dev);
  742. if (ret) {
  743. dev_err(&pdev->dev, "Couldn't register the device.\n");
  744. goto error_adc_buffer_init;
  745. }
  746. return 0;
  747. error_adc_buffer_init:
  748. iio_triggered_buffer_cleanup(indio_dev);
  749. error_iio_device_register:
  750. clk_disable_unprepare(info->clk);
  751. error_adc_clk_enable:
  752. regulator_disable(info->vref);
  753. return ret;
  754. }
  755. static int vf610_adc_remove(struct platform_device *pdev)
  756. {
  757. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  758. struct vf610_adc *info = iio_priv(indio_dev);
  759. iio_device_unregister(indio_dev);
  760. iio_triggered_buffer_cleanup(indio_dev);
  761. regulator_disable(info->vref);
  762. clk_disable_unprepare(info->clk);
  763. return 0;
  764. }
  765. static int vf610_adc_suspend(struct device *dev)
  766. {
  767. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  768. struct vf610_adc *info = iio_priv(indio_dev);
  769. int hc_cfg;
  770. /* ADC controller enters to stop mode */
  771. hc_cfg = readl(info->regs + VF610_REG_ADC_HC0);
  772. hc_cfg |= VF610_ADC_CONV_DISABLE;
  773. writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
  774. clk_disable_unprepare(info->clk);
  775. regulator_disable(info->vref);
  776. return 0;
  777. }
  778. static int vf610_adc_resume(struct device *dev)
  779. {
  780. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  781. struct vf610_adc *info = iio_priv(indio_dev);
  782. int ret;
  783. ret = regulator_enable(info->vref);
  784. if (ret)
  785. return ret;
  786. ret = clk_prepare_enable(info->clk);
  787. if (ret)
  788. goto disable_reg;
  789. vf610_adc_hw_init(info);
  790. return 0;
  791. disable_reg:
  792. regulator_disable(info->vref);
  793. return ret;
  794. }
  795. static DEFINE_SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops, vf610_adc_suspend,
  796. vf610_adc_resume);
  797. static struct platform_driver vf610_adc_driver = {
  798. .probe = vf610_adc_probe,
  799. .remove = vf610_adc_remove,
  800. .driver = {
  801. .name = DRIVER_NAME,
  802. .of_match_table = vf610_adc_match,
  803. .pm = pm_sleep_ptr(&vf610_adc_pm_ops),
  804. },
  805. };
  806. module_platform_driver(vf610_adc_driver);
  807. MODULE_AUTHOR("Fugang Duan <[email protected]>");
  808. MODULE_DESCRIPTION("Freescale VF610 ADC driver");
  809. MODULE_LICENSE("GPL v2");