vf610_adc.c 23 KB

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