vf610_adc.c 23 KB

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