ad7124.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD7124 SPI ADC driver
  4. *
  5. * Copyright 2018 Analog Devices Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kfifo.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/adc/ad_sigma_delta.h>
  22. #include <linux/iio/sysfs.h>
  23. /* AD7124 registers */
  24. #define AD7124_COMMS 0x00
  25. #define AD7124_STATUS 0x00
  26. #define AD7124_ADC_CONTROL 0x01
  27. #define AD7124_DATA 0x02
  28. #define AD7124_IO_CONTROL_1 0x03
  29. #define AD7124_IO_CONTROL_2 0x04
  30. #define AD7124_ID 0x05
  31. #define AD7124_ERROR 0x06
  32. #define AD7124_ERROR_EN 0x07
  33. #define AD7124_MCLK_COUNT 0x08
  34. #define AD7124_CHANNEL(x) (0x09 + (x))
  35. #define AD7124_CONFIG(x) (0x19 + (x))
  36. #define AD7124_FILTER(x) (0x21 + (x))
  37. #define AD7124_OFFSET(x) (0x29 + (x))
  38. #define AD7124_GAIN(x) (0x31 + (x))
  39. /* AD7124_STATUS */
  40. #define AD7124_STATUS_POR_FLAG_MSK BIT(4)
  41. /* AD7124_ADC_CONTROL */
  42. #define AD7124_ADC_STATUS_EN_MSK BIT(10)
  43. #define AD7124_ADC_STATUS_EN(x) FIELD_PREP(AD7124_ADC_STATUS_EN_MSK, x)
  44. #define AD7124_ADC_CTRL_REF_EN_MSK BIT(8)
  45. #define AD7124_ADC_CTRL_REF_EN(x) FIELD_PREP(AD7124_ADC_CTRL_REF_EN_MSK, x)
  46. #define AD7124_ADC_CTRL_PWR_MSK GENMASK(7, 6)
  47. #define AD7124_ADC_CTRL_PWR(x) FIELD_PREP(AD7124_ADC_CTRL_PWR_MSK, x)
  48. #define AD7124_ADC_CTRL_MODE_MSK GENMASK(5, 2)
  49. #define AD7124_ADC_CTRL_MODE(x) FIELD_PREP(AD7124_ADC_CTRL_MODE_MSK, x)
  50. /* AD7124 ID */
  51. #define AD7124_DEVICE_ID_MSK GENMASK(7, 4)
  52. #define AD7124_DEVICE_ID_GET(x) FIELD_GET(AD7124_DEVICE_ID_MSK, x)
  53. #define AD7124_SILICON_REV_MSK GENMASK(3, 0)
  54. #define AD7124_SILICON_REV_GET(x) FIELD_GET(AD7124_SILICON_REV_MSK, x)
  55. #define CHIPID_AD7124_4 0x0
  56. #define CHIPID_AD7124_8 0x1
  57. /* AD7124_CHANNEL_X */
  58. #define AD7124_CHANNEL_EN_MSK BIT(15)
  59. #define AD7124_CHANNEL_EN(x) FIELD_PREP(AD7124_CHANNEL_EN_MSK, x)
  60. #define AD7124_CHANNEL_SETUP_MSK GENMASK(14, 12)
  61. #define AD7124_CHANNEL_SETUP(x) FIELD_PREP(AD7124_CHANNEL_SETUP_MSK, x)
  62. #define AD7124_CHANNEL_AINP_MSK GENMASK(9, 5)
  63. #define AD7124_CHANNEL_AINP(x) FIELD_PREP(AD7124_CHANNEL_AINP_MSK, x)
  64. #define AD7124_CHANNEL_AINM_MSK GENMASK(4, 0)
  65. #define AD7124_CHANNEL_AINM(x) FIELD_PREP(AD7124_CHANNEL_AINM_MSK, x)
  66. /* AD7124_CONFIG_X */
  67. #define AD7124_CONFIG_BIPOLAR_MSK BIT(11)
  68. #define AD7124_CONFIG_BIPOLAR(x) FIELD_PREP(AD7124_CONFIG_BIPOLAR_MSK, x)
  69. #define AD7124_CONFIG_REF_SEL_MSK GENMASK(4, 3)
  70. #define AD7124_CONFIG_REF_SEL(x) FIELD_PREP(AD7124_CONFIG_REF_SEL_MSK, x)
  71. #define AD7124_CONFIG_PGA_MSK GENMASK(2, 0)
  72. #define AD7124_CONFIG_PGA(x) FIELD_PREP(AD7124_CONFIG_PGA_MSK, x)
  73. #define AD7124_CONFIG_IN_BUFF_MSK GENMASK(6, 5)
  74. #define AD7124_CONFIG_IN_BUFF(x) FIELD_PREP(AD7124_CONFIG_IN_BUFF_MSK, x)
  75. /* AD7124_FILTER_X */
  76. #define AD7124_FILTER_FS_MSK GENMASK(10, 0)
  77. #define AD7124_FILTER_FS(x) FIELD_PREP(AD7124_FILTER_FS_MSK, x)
  78. #define AD7124_FILTER_TYPE_MSK GENMASK(23, 21)
  79. #define AD7124_FILTER_TYPE_SEL(x) FIELD_PREP(AD7124_FILTER_TYPE_MSK, x)
  80. #define AD7124_SINC3_FILTER 2
  81. #define AD7124_SINC4_FILTER 0
  82. #define AD7124_CONF_ADDR_OFFSET 20
  83. #define AD7124_MAX_CONFIGS 8
  84. #define AD7124_MAX_CHANNELS 16
  85. enum ad7124_ids {
  86. ID_AD7124_4,
  87. ID_AD7124_8,
  88. };
  89. enum ad7124_ref_sel {
  90. AD7124_REFIN1,
  91. AD7124_REFIN2,
  92. AD7124_INT_REF,
  93. AD7124_AVDD_REF,
  94. };
  95. enum ad7124_power_mode {
  96. AD7124_LOW_POWER,
  97. AD7124_MID_POWER,
  98. AD7124_FULL_POWER,
  99. };
  100. static const unsigned int ad7124_gain[8] = {
  101. 1, 2, 4, 8, 16, 32, 64, 128
  102. };
  103. static const unsigned int ad7124_reg_size[] = {
  104. 1, 2, 3, 3, 2, 1, 3, 3, 1, 2, 2, 2, 2,
  105. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  106. 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
  107. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  108. 3, 3, 3, 3, 3
  109. };
  110. static const int ad7124_master_clk_freq_hz[3] = {
  111. [AD7124_LOW_POWER] = 76800,
  112. [AD7124_MID_POWER] = 153600,
  113. [AD7124_FULL_POWER] = 614400,
  114. };
  115. static const char * const ad7124_ref_names[] = {
  116. [AD7124_REFIN1] = "refin1",
  117. [AD7124_REFIN2] = "refin2",
  118. [AD7124_INT_REF] = "int",
  119. [AD7124_AVDD_REF] = "avdd",
  120. };
  121. struct ad7124_chip_info {
  122. const char *name;
  123. unsigned int chip_id;
  124. unsigned int num_inputs;
  125. };
  126. struct ad7124_channel_config {
  127. bool live;
  128. unsigned int cfg_slot;
  129. enum ad7124_ref_sel refsel;
  130. bool bipolar;
  131. bool buf_positive;
  132. bool buf_negative;
  133. unsigned int vref_mv;
  134. unsigned int pga_bits;
  135. unsigned int odr;
  136. unsigned int odr_sel_bits;
  137. unsigned int filter_type;
  138. };
  139. struct ad7124_channel {
  140. unsigned int nr;
  141. struct ad7124_channel_config cfg;
  142. unsigned int ain;
  143. unsigned int slot;
  144. };
  145. struct ad7124_state {
  146. const struct ad7124_chip_info *chip_info;
  147. struct ad_sigma_delta sd;
  148. struct ad7124_channel *channels;
  149. struct regulator *vref[4];
  150. struct clk *mclk;
  151. unsigned int adc_control;
  152. unsigned int num_channels;
  153. struct mutex cfgs_lock; /* lock for configs access */
  154. unsigned long cfg_slots_status; /* bitmap with slot status (1 means it is used) */
  155. DECLARE_KFIFO(live_cfgs_fifo, struct ad7124_channel_config *, AD7124_MAX_CONFIGS);
  156. };
  157. static const struct iio_chan_spec ad7124_channel_template = {
  158. .type = IIO_VOLTAGE,
  159. .indexed = 1,
  160. .differential = 1,
  161. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  162. BIT(IIO_CHAN_INFO_SCALE) |
  163. BIT(IIO_CHAN_INFO_OFFSET) |
  164. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  165. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
  166. .scan_type = {
  167. .sign = 'u',
  168. .realbits = 24,
  169. .storagebits = 32,
  170. .endianness = IIO_BE,
  171. },
  172. };
  173. static struct ad7124_chip_info ad7124_chip_info_tbl[] = {
  174. [ID_AD7124_4] = {
  175. .name = "ad7124-4",
  176. .chip_id = CHIPID_AD7124_4,
  177. .num_inputs = 8,
  178. },
  179. [ID_AD7124_8] = {
  180. .name = "ad7124-8",
  181. .chip_id = CHIPID_AD7124_8,
  182. .num_inputs = 16,
  183. },
  184. };
  185. static int ad7124_find_closest_match(const int *array,
  186. unsigned int size, int val)
  187. {
  188. int i, idx;
  189. unsigned int diff_new, diff_old;
  190. diff_old = U32_MAX;
  191. idx = 0;
  192. for (i = 0; i < size; i++) {
  193. diff_new = abs(val - array[i]);
  194. if (diff_new < diff_old) {
  195. diff_old = diff_new;
  196. idx = i;
  197. }
  198. }
  199. return idx;
  200. }
  201. static int ad7124_spi_write_mask(struct ad7124_state *st,
  202. unsigned int addr,
  203. unsigned long mask,
  204. unsigned int val,
  205. unsigned int bytes)
  206. {
  207. unsigned int readval;
  208. int ret;
  209. ret = ad_sd_read_reg(&st->sd, addr, bytes, &readval);
  210. if (ret < 0)
  211. return ret;
  212. readval &= ~mask;
  213. readval |= val;
  214. return ad_sd_write_reg(&st->sd, addr, bytes, readval);
  215. }
  216. static int ad7124_set_mode(struct ad_sigma_delta *sd,
  217. enum ad_sigma_delta_mode mode)
  218. {
  219. struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
  220. st->adc_control &= ~AD7124_ADC_CTRL_MODE_MSK;
  221. st->adc_control |= AD7124_ADC_CTRL_MODE(mode);
  222. return ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control);
  223. }
  224. static void ad7124_set_channel_odr(struct ad7124_state *st, unsigned int channel, unsigned int odr)
  225. {
  226. unsigned int fclk, odr_sel_bits;
  227. fclk = clk_get_rate(st->mclk);
  228. /*
  229. * FS[10:0] = fCLK / (fADC x 32) where:
  230. * fADC is the output data rate
  231. * fCLK is the master clock frequency
  232. * FS[10:0] are the bits in the filter register
  233. * FS[10:0] can have a value from 1 to 2047
  234. */
  235. odr_sel_bits = DIV_ROUND_CLOSEST(fclk, odr * 32);
  236. if (odr_sel_bits < 1)
  237. odr_sel_bits = 1;
  238. else if (odr_sel_bits > 2047)
  239. odr_sel_bits = 2047;
  240. if (odr_sel_bits != st->channels[channel].cfg.odr_sel_bits)
  241. st->channels[channel].cfg.live = false;
  242. /* fADC = fCLK / (FS[10:0] x 32) */
  243. st->channels[channel].cfg.odr = DIV_ROUND_CLOSEST(fclk, odr_sel_bits * 32);
  244. st->channels[channel].cfg.odr_sel_bits = odr_sel_bits;
  245. }
  246. static int ad7124_get_3db_filter_freq(struct ad7124_state *st,
  247. unsigned int channel)
  248. {
  249. unsigned int fadc;
  250. fadc = st->channels[channel].cfg.odr;
  251. switch (st->channels[channel].cfg.filter_type) {
  252. case AD7124_SINC3_FILTER:
  253. return DIV_ROUND_CLOSEST(fadc * 230, 1000);
  254. case AD7124_SINC4_FILTER:
  255. return DIV_ROUND_CLOSEST(fadc * 262, 1000);
  256. default:
  257. return -EINVAL;
  258. }
  259. }
  260. static void ad7124_set_3db_filter_freq(struct ad7124_state *st, unsigned int channel,
  261. unsigned int freq)
  262. {
  263. unsigned int sinc4_3db_odr;
  264. unsigned int sinc3_3db_odr;
  265. unsigned int new_filter;
  266. unsigned int new_odr;
  267. sinc4_3db_odr = DIV_ROUND_CLOSEST(freq * 1000, 230);
  268. sinc3_3db_odr = DIV_ROUND_CLOSEST(freq * 1000, 262);
  269. if (sinc4_3db_odr > sinc3_3db_odr) {
  270. new_filter = AD7124_SINC3_FILTER;
  271. new_odr = sinc4_3db_odr;
  272. } else {
  273. new_filter = AD7124_SINC4_FILTER;
  274. new_odr = sinc3_3db_odr;
  275. }
  276. if (new_odr != st->channels[channel].cfg.odr)
  277. st->channels[channel].cfg.live = false;
  278. st->channels[channel].cfg.filter_type = new_filter;
  279. st->channels[channel].cfg.odr = new_odr;
  280. }
  281. static struct ad7124_channel_config *ad7124_find_similar_live_cfg(struct ad7124_state *st,
  282. struct ad7124_channel_config *cfg)
  283. {
  284. struct ad7124_channel_config *cfg_aux;
  285. ptrdiff_t cmp_size;
  286. int i;
  287. cmp_size = (u8 *)&cfg->live - (u8 *)cfg;
  288. for (i = 0; i < st->num_channels; i++) {
  289. cfg_aux = &st->channels[i].cfg;
  290. if (cfg_aux->live && !memcmp(cfg, cfg_aux, cmp_size))
  291. return cfg_aux;
  292. }
  293. return NULL;
  294. }
  295. static int ad7124_find_free_config_slot(struct ad7124_state *st)
  296. {
  297. unsigned int free_cfg_slot;
  298. free_cfg_slot = find_first_zero_bit(&st->cfg_slots_status, AD7124_MAX_CONFIGS);
  299. if (free_cfg_slot == AD7124_MAX_CONFIGS)
  300. return -1;
  301. return free_cfg_slot;
  302. }
  303. static int ad7124_init_config_vref(struct ad7124_state *st, struct ad7124_channel_config *cfg)
  304. {
  305. unsigned int refsel = cfg->refsel;
  306. switch (refsel) {
  307. case AD7124_REFIN1:
  308. case AD7124_REFIN2:
  309. case AD7124_AVDD_REF:
  310. if (IS_ERR(st->vref[refsel])) {
  311. dev_err(&st->sd.spi->dev,
  312. "Error, trying to use external voltage reference without a %s regulator.\n",
  313. ad7124_ref_names[refsel]);
  314. return PTR_ERR(st->vref[refsel]);
  315. }
  316. cfg->vref_mv = regulator_get_voltage(st->vref[refsel]);
  317. /* Conversion from uV to mV */
  318. cfg->vref_mv /= 1000;
  319. return 0;
  320. case AD7124_INT_REF:
  321. cfg->vref_mv = 2500;
  322. st->adc_control &= ~AD7124_ADC_CTRL_REF_EN_MSK;
  323. st->adc_control |= AD7124_ADC_CTRL_REF_EN(1);
  324. return ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL,
  325. 2, st->adc_control);
  326. default:
  327. dev_err(&st->sd.spi->dev, "Invalid reference %d\n", refsel);
  328. return -EINVAL;
  329. }
  330. }
  331. static int ad7124_write_config(struct ad7124_state *st, struct ad7124_channel_config *cfg,
  332. unsigned int cfg_slot)
  333. {
  334. unsigned int tmp;
  335. unsigned int val;
  336. int ret;
  337. cfg->cfg_slot = cfg_slot;
  338. tmp = (cfg->buf_positive << 1) + cfg->buf_negative;
  339. val = AD7124_CONFIG_BIPOLAR(cfg->bipolar) | AD7124_CONFIG_REF_SEL(cfg->refsel) |
  340. AD7124_CONFIG_IN_BUFF(tmp);
  341. ret = ad_sd_write_reg(&st->sd, AD7124_CONFIG(cfg->cfg_slot), 2, val);
  342. if (ret < 0)
  343. return ret;
  344. tmp = AD7124_FILTER_TYPE_SEL(cfg->filter_type);
  345. ret = ad7124_spi_write_mask(st, AD7124_FILTER(cfg->cfg_slot), AD7124_FILTER_TYPE_MSK,
  346. tmp, 3);
  347. if (ret < 0)
  348. return ret;
  349. ret = ad7124_spi_write_mask(st, AD7124_FILTER(cfg->cfg_slot), AD7124_FILTER_FS_MSK,
  350. AD7124_FILTER_FS(cfg->odr_sel_bits), 3);
  351. if (ret < 0)
  352. return ret;
  353. return ad7124_spi_write_mask(st, AD7124_CONFIG(cfg->cfg_slot), AD7124_CONFIG_PGA_MSK,
  354. AD7124_CONFIG_PGA(cfg->pga_bits), 2);
  355. }
  356. static struct ad7124_channel_config *ad7124_pop_config(struct ad7124_state *st)
  357. {
  358. struct ad7124_channel_config *lru_cfg;
  359. struct ad7124_channel_config *cfg;
  360. int ret;
  361. int i;
  362. /*
  363. * Pop least recently used config from the fifo
  364. * in order to make room for the new one
  365. */
  366. ret = kfifo_get(&st->live_cfgs_fifo, &lru_cfg);
  367. if (ret <= 0)
  368. return NULL;
  369. lru_cfg->live = false;
  370. /* mark slot as free */
  371. assign_bit(lru_cfg->cfg_slot, &st->cfg_slots_status, 0);
  372. /* invalidate all other configs that pointed to this one */
  373. for (i = 0; i < st->num_channels; i++) {
  374. cfg = &st->channels[i].cfg;
  375. if (cfg->cfg_slot == lru_cfg->cfg_slot)
  376. cfg->live = false;
  377. }
  378. return lru_cfg;
  379. }
  380. static int ad7124_push_config(struct ad7124_state *st, struct ad7124_channel_config *cfg)
  381. {
  382. struct ad7124_channel_config *lru_cfg;
  383. int free_cfg_slot;
  384. free_cfg_slot = ad7124_find_free_config_slot(st);
  385. if (free_cfg_slot >= 0) {
  386. /* push the new config in configs queue */
  387. kfifo_put(&st->live_cfgs_fifo, cfg);
  388. } else {
  389. /* pop one config to make room for the new one */
  390. lru_cfg = ad7124_pop_config(st);
  391. if (!lru_cfg)
  392. return -EINVAL;
  393. /* push the new config in configs queue */
  394. free_cfg_slot = lru_cfg->cfg_slot;
  395. kfifo_put(&st->live_cfgs_fifo, cfg);
  396. }
  397. /* mark slot as used */
  398. assign_bit(free_cfg_slot, &st->cfg_slots_status, 1);
  399. return ad7124_write_config(st, cfg, free_cfg_slot);
  400. }
  401. static int ad7124_enable_channel(struct ad7124_state *st, struct ad7124_channel *ch)
  402. {
  403. ch->cfg.live = true;
  404. return ad_sd_write_reg(&st->sd, AD7124_CHANNEL(ch->nr), 2, ch->ain |
  405. AD7124_CHANNEL_SETUP(ch->cfg.cfg_slot) | AD7124_CHANNEL_EN(1));
  406. }
  407. static int ad7124_prepare_read(struct ad7124_state *st, int address)
  408. {
  409. struct ad7124_channel_config *cfg = &st->channels[address].cfg;
  410. struct ad7124_channel_config *live_cfg;
  411. /*
  412. * Before doing any reads assign the channel a configuration.
  413. * Check if channel's config is on the device
  414. */
  415. if (!cfg->live) {
  416. /* check if config matches another one */
  417. live_cfg = ad7124_find_similar_live_cfg(st, cfg);
  418. if (!live_cfg)
  419. ad7124_push_config(st, cfg);
  420. else
  421. cfg->cfg_slot = live_cfg->cfg_slot;
  422. }
  423. /* point channel to the config slot and enable */
  424. return ad7124_enable_channel(st, &st->channels[address]);
  425. }
  426. static int __ad7124_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
  427. {
  428. struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
  429. return ad7124_prepare_read(st, channel);
  430. }
  431. static int ad7124_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
  432. {
  433. struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
  434. int ret;
  435. mutex_lock(&st->cfgs_lock);
  436. ret = __ad7124_set_channel(sd, channel);
  437. mutex_unlock(&st->cfgs_lock);
  438. return ret;
  439. }
  440. static int ad7124_append_status(struct ad_sigma_delta *sd, bool append)
  441. {
  442. struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
  443. unsigned int adc_control = st->adc_control;
  444. int ret;
  445. adc_control &= ~AD7124_ADC_STATUS_EN_MSK;
  446. adc_control |= AD7124_ADC_STATUS_EN(append);
  447. ret = ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, adc_control);
  448. if (ret < 0)
  449. return ret;
  450. st->adc_control = adc_control;
  451. return 0;
  452. }
  453. static int ad7124_disable_all(struct ad_sigma_delta *sd)
  454. {
  455. struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
  456. int ret;
  457. int i;
  458. for (i = 0; i < st->num_channels; i++) {
  459. ret = ad7124_spi_write_mask(st, AD7124_CHANNEL(i), AD7124_CHANNEL_EN_MSK, 0, 2);
  460. if (ret < 0)
  461. return ret;
  462. }
  463. return 0;
  464. }
  465. static const struct ad_sigma_delta_info ad7124_sigma_delta_info = {
  466. .set_channel = ad7124_set_channel,
  467. .append_status = ad7124_append_status,
  468. .disable_all = ad7124_disable_all,
  469. .set_mode = ad7124_set_mode,
  470. .has_registers = true,
  471. .addr_shift = 0,
  472. .read_mask = BIT(6),
  473. .status_ch_mask = GENMASK(3, 0),
  474. .data_reg = AD7124_DATA,
  475. .num_slots = 8,
  476. .irq_flags = IRQF_TRIGGER_FALLING,
  477. };
  478. static int ad7124_read_raw(struct iio_dev *indio_dev,
  479. struct iio_chan_spec const *chan,
  480. int *val, int *val2, long info)
  481. {
  482. struct ad7124_state *st = iio_priv(indio_dev);
  483. int idx, ret;
  484. switch (info) {
  485. case IIO_CHAN_INFO_RAW:
  486. ret = ad_sigma_delta_single_conversion(indio_dev, chan, val);
  487. if (ret < 0)
  488. return ret;
  489. /* After the conversion is performed, disable the channel */
  490. ret = ad_sd_write_reg(&st->sd, AD7124_CHANNEL(chan->address), 2,
  491. st->channels[chan->address].ain | AD7124_CHANNEL_EN(0));
  492. if (ret < 0)
  493. return ret;
  494. return IIO_VAL_INT;
  495. case IIO_CHAN_INFO_SCALE:
  496. mutex_lock(&st->cfgs_lock);
  497. idx = st->channels[chan->address].cfg.pga_bits;
  498. *val = st->channels[chan->address].cfg.vref_mv;
  499. if (st->channels[chan->address].cfg.bipolar)
  500. *val2 = chan->scan_type.realbits - 1 + idx;
  501. else
  502. *val2 = chan->scan_type.realbits + idx;
  503. mutex_unlock(&st->cfgs_lock);
  504. return IIO_VAL_FRACTIONAL_LOG2;
  505. case IIO_CHAN_INFO_OFFSET:
  506. mutex_lock(&st->cfgs_lock);
  507. if (st->channels[chan->address].cfg.bipolar)
  508. *val = -(1 << (chan->scan_type.realbits - 1));
  509. else
  510. *val = 0;
  511. mutex_unlock(&st->cfgs_lock);
  512. return IIO_VAL_INT;
  513. case IIO_CHAN_INFO_SAMP_FREQ:
  514. mutex_lock(&st->cfgs_lock);
  515. *val = st->channels[chan->address].cfg.odr;
  516. mutex_unlock(&st->cfgs_lock);
  517. return IIO_VAL_INT;
  518. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  519. mutex_lock(&st->cfgs_lock);
  520. *val = ad7124_get_3db_filter_freq(st, chan->scan_index);
  521. mutex_unlock(&st->cfgs_lock);
  522. return IIO_VAL_INT;
  523. default:
  524. return -EINVAL;
  525. }
  526. }
  527. static int ad7124_write_raw(struct iio_dev *indio_dev,
  528. struct iio_chan_spec const *chan,
  529. int val, int val2, long info)
  530. {
  531. struct ad7124_state *st = iio_priv(indio_dev);
  532. unsigned int res, gain, full_scale, vref;
  533. int ret = 0;
  534. mutex_lock(&st->cfgs_lock);
  535. switch (info) {
  536. case IIO_CHAN_INFO_SAMP_FREQ:
  537. if (val2 != 0) {
  538. ret = -EINVAL;
  539. break;
  540. }
  541. ad7124_set_channel_odr(st, chan->address, val);
  542. break;
  543. case IIO_CHAN_INFO_SCALE:
  544. if (val != 0) {
  545. ret = -EINVAL;
  546. break;
  547. }
  548. if (st->channels[chan->address].cfg.bipolar)
  549. full_scale = 1 << (chan->scan_type.realbits - 1);
  550. else
  551. full_scale = 1 << chan->scan_type.realbits;
  552. vref = st->channels[chan->address].cfg.vref_mv * 1000000LL;
  553. res = DIV_ROUND_CLOSEST(vref, full_scale);
  554. gain = DIV_ROUND_CLOSEST(res, val2);
  555. res = ad7124_find_closest_match(ad7124_gain, ARRAY_SIZE(ad7124_gain), gain);
  556. if (st->channels[chan->address].cfg.pga_bits != res)
  557. st->channels[chan->address].cfg.live = false;
  558. st->channels[chan->address].cfg.pga_bits = res;
  559. break;
  560. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  561. if (val2 != 0) {
  562. ret = -EINVAL;
  563. break;
  564. }
  565. ad7124_set_3db_filter_freq(st, chan->address, val);
  566. break;
  567. default:
  568. ret = -EINVAL;
  569. }
  570. mutex_unlock(&st->cfgs_lock);
  571. return ret;
  572. }
  573. static int ad7124_reg_access(struct iio_dev *indio_dev,
  574. unsigned int reg,
  575. unsigned int writeval,
  576. unsigned int *readval)
  577. {
  578. struct ad7124_state *st = iio_priv(indio_dev);
  579. int ret;
  580. if (reg >= ARRAY_SIZE(ad7124_reg_size))
  581. return -EINVAL;
  582. if (readval)
  583. ret = ad_sd_read_reg(&st->sd, reg, ad7124_reg_size[reg],
  584. readval);
  585. else
  586. ret = ad_sd_write_reg(&st->sd, reg, ad7124_reg_size[reg],
  587. writeval);
  588. return ret;
  589. }
  590. static IIO_CONST_ATTR(in_voltage_scale_available,
  591. "0.000001164 0.000002328 0.000004656 0.000009313 0.000018626 0.000037252 0.000074505 0.000149011 0.000298023");
  592. static struct attribute *ad7124_attributes[] = {
  593. &iio_const_attr_in_voltage_scale_available.dev_attr.attr,
  594. NULL,
  595. };
  596. static const struct attribute_group ad7124_attrs_group = {
  597. .attrs = ad7124_attributes,
  598. };
  599. static int ad7124_update_scan_mode(struct iio_dev *indio_dev,
  600. const unsigned long *scan_mask)
  601. {
  602. struct ad7124_state *st = iio_priv(indio_dev);
  603. bool bit_set;
  604. int ret;
  605. int i;
  606. mutex_lock(&st->cfgs_lock);
  607. for (i = 0; i < st->num_channels; i++) {
  608. bit_set = test_bit(i, scan_mask);
  609. if (bit_set)
  610. ret = __ad7124_set_channel(&st->sd, i);
  611. else
  612. ret = ad7124_spi_write_mask(st, AD7124_CHANNEL(i), AD7124_CHANNEL_EN_MSK,
  613. 0, 2);
  614. if (ret < 0) {
  615. mutex_unlock(&st->cfgs_lock);
  616. return ret;
  617. }
  618. }
  619. mutex_unlock(&st->cfgs_lock);
  620. return 0;
  621. }
  622. static const struct iio_info ad7124_info = {
  623. .read_raw = ad7124_read_raw,
  624. .write_raw = ad7124_write_raw,
  625. .debugfs_reg_access = &ad7124_reg_access,
  626. .validate_trigger = ad_sd_validate_trigger,
  627. .update_scan_mode = ad7124_update_scan_mode,
  628. .attrs = &ad7124_attrs_group,
  629. };
  630. static int ad7124_soft_reset(struct ad7124_state *st)
  631. {
  632. unsigned int readval, timeout;
  633. int ret;
  634. ret = ad_sd_reset(&st->sd, 64);
  635. if (ret < 0)
  636. return ret;
  637. timeout = 100;
  638. do {
  639. ret = ad_sd_read_reg(&st->sd, AD7124_STATUS, 1, &readval);
  640. if (ret < 0)
  641. return ret;
  642. if (!(readval & AD7124_STATUS_POR_FLAG_MSK))
  643. return 0;
  644. /* The AD7124 requires typically 2ms to power up and settle */
  645. usleep_range(100, 2000);
  646. } while (--timeout);
  647. dev_err(&st->sd.spi->dev, "Soft reset failed\n");
  648. return -EIO;
  649. }
  650. static int ad7124_check_chip_id(struct ad7124_state *st)
  651. {
  652. unsigned int readval, chip_id, silicon_rev;
  653. int ret;
  654. ret = ad_sd_read_reg(&st->sd, AD7124_ID, 1, &readval);
  655. if (ret < 0)
  656. return ret;
  657. chip_id = AD7124_DEVICE_ID_GET(readval);
  658. silicon_rev = AD7124_SILICON_REV_GET(readval);
  659. if (chip_id != st->chip_info->chip_id) {
  660. dev_err(&st->sd.spi->dev,
  661. "Chip ID mismatch: expected %u, got %u\n",
  662. st->chip_info->chip_id, chip_id);
  663. return -ENODEV;
  664. }
  665. if (silicon_rev == 0) {
  666. dev_err(&st->sd.spi->dev,
  667. "Silicon revision empty. Chip may not be present\n");
  668. return -ENODEV;
  669. }
  670. return 0;
  671. }
  672. static int ad7124_of_parse_channel_config(struct iio_dev *indio_dev,
  673. struct device_node *np)
  674. {
  675. struct ad7124_state *st = iio_priv(indio_dev);
  676. struct ad7124_channel_config *cfg;
  677. struct ad7124_channel *channels;
  678. struct device_node *child;
  679. struct iio_chan_spec *chan;
  680. unsigned int ain[2], channel = 0, tmp;
  681. int ret;
  682. st->num_channels = of_get_available_child_count(np);
  683. if (!st->num_channels) {
  684. dev_err(indio_dev->dev.parent, "no channel children\n");
  685. return -ENODEV;
  686. }
  687. chan = devm_kcalloc(indio_dev->dev.parent, st->num_channels,
  688. sizeof(*chan), GFP_KERNEL);
  689. if (!chan)
  690. return -ENOMEM;
  691. channels = devm_kcalloc(indio_dev->dev.parent, st->num_channels, sizeof(*channels),
  692. GFP_KERNEL);
  693. if (!channels)
  694. return -ENOMEM;
  695. indio_dev->channels = chan;
  696. indio_dev->num_channels = st->num_channels;
  697. st->channels = channels;
  698. for_each_available_child_of_node(np, child) {
  699. cfg = &st->channels[channel].cfg;
  700. ret = of_property_read_u32(child, "reg", &channel);
  701. if (ret)
  702. goto err;
  703. if (channel >= indio_dev->num_channels) {
  704. dev_err(indio_dev->dev.parent,
  705. "Channel index >= number of channels\n");
  706. ret = -EINVAL;
  707. goto err;
  708. }
  709. ret = of_property_read_u32_array(child, "diff-channels",
  710. ain, 2);
  711. if (ret)
  712. goto err;
  713. st->channels[channel].nr = channel;
  714. st->channels[channel].ain = AD7124_CHANNEL_AINP(ain[0]) |
  715. AD7124_CHANNEL_AINM(ain[1]);
  716. cfg->bipolar = of_property_read_bool(child, "bipolar");
  717. ret = of_property_read_u32(child, "adi,reference-select", &tmp);
  718. if (ret)
  719. cfg->refsel = AD7124_INT_REF;
  720. else
  721. cfg->refsel = tmp;
  722. cfg->buf_positive = of_property_read_bool(child, "adi,buffered-positive");
  723. cfg->buf_negative = of_property_read_bool(child, "adi,buffered-negative");
  724. chan[channel] = ad7124_channel_template;
  725. chan[channel].address = channel;
  726. chan[channel].scan_index = channel;
  727. chan[channel].channel = ain[0];
  728. chan[channel].channel2 = ain[1];
  729. }
  730. return 0;
  731. err:
  732. of_node_put(child);
  733. return ret;
  734. }
  735. static int ad7124_setup(struct ad7124_state *st)
  736. {
  737. unsigned int fclk, power_mode;
  738. int i, ret;
  739. fclk = clk_get_rate(st->mclk);
  740. if (!fclk)
  741. return -EINVAL;
  742. /* The power mode changes the master clock frequency */
  743. power_mode = ad7124_find_closest_match(ad7124_master_clk_freq_hz,
  744. ARRAY_SIZE(ad7124_master_clk_freq_hz),
  745. fclk);
  746. if (fclk != ad7124_master_clk_freq_hz[power_mode]) {
  747. ret = clk_set_rate(st->mclk, fclk);
  748. if (ret)
  749. return ret;
  750. }
  751. /* Set the power mode */
  752. st->adc_control &= ~AD7124_ADC_CTRL_PWR_MSK;
  753. st->adc_control |= AD7124_ADC_CTRL_PWR(power_mode);
  754. ret = ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control);
  755. if (ret < 0)
  756. return ret;
  757. mutex_init(&st->cfgs_lock);
  758. INIT_KFIFO(st->live_cfgs_fifo);
  759. for (i = 0; i < st->num_channels; i++) {
  760. ret = ad7124_init_config_vref(st, &st->channels[i].cfg);
  761. if (ret < 0)
  762. return ret;
  763. /*
  764. * 9.38 SPS is the minimum output data rate supported
  765. * regardless of the selected power mode. Round it up to 10 and
  766. * set all channels to this default value.
  767. */
  768. ad7124_set_channel_odr(st, i, 10);
  769. }
  770. return ret;
  771. }
  772. static void ad7124_reg_disable(void *r)
  773. {
  774. regulator_disable(r);
  775. }
  776. static int ad7124_probe(struct spi_device *spi)
  777. {
  778. const struct ad7124_chip_info *info;
  779. struct ad7124_state *st;
  780. struct iio_dev *indio_dev;
  781. int i, ret;
  782. info = of_device_get_match_data(&spi->dev);
  783. if (!info)
  784. return -ENODEV;
  785. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  786. if (!indio_dev)
  787. return -ENOMEM;
  788. st = iio_priv(indio_dev);
  789. st->chip_info = info;
  790. indio_dev->name = st->chip_info->name;
  791. indio_dev->modes = INDIO_DIRECT_MODE;
  792. indio_dev->info = &ad7124_info;
  793. ret = ad_sd_init(&st->sd, indio_dev, spi, &ad7124_sigma_delta_info);
  794. if (ret < 0)
  795. return ret;
  796. ret = ad7124_of_parse_channel_config(indio_dev, spi->dev.of_node);
  797. if (ret < 0)
  798. return ret;
  799. for (i = 0; i < ARRAY_SIZE(st->vref); i++) {
  800. if (i == AD7124_INT_REF)
  801. continue;
  802. st->vref[i] = devm_regulator_get_optional(&spi->dev,
  803. ad7124_ref_names[i]);
  804. if (PTR_ERR(st->vref[i]) == -ENODEV)
  805. continue;
  806. else if (IS_ERR(st->vref[i]))
  807. return PTR_ERR(st->vref[i]);
  808. ret = regulator_enable(st->vref[i]);
  809. if (ret)
  810. return ret;
  811. ret = devm_add_action_or_reset(&spi->dev, ad7124_reg_disable,
  812. st->vref[i]);
  813. if (ret)
  814. return ret;
  815. }
  816. st->mclk = devm_clk_get_enabled(&spi->dev, "mclk");
  817. if (IS_ERR(st->mclk))
  818. return PTR_ERR(st->mclk);
  819. ret = ad7124_soft_reset(st);
  820. if (ret < 0)
  821. return ret;
  822. ret = ad7124_check_chip_id(st);
  823. if (ret)
  824. return ret;
  825. ret = ad7124_setup(st);
  826. if (ret < 0)
  827. return ret;
  828. ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev);
  829. if (ret < 0)
  830. return ret;
  831. return devm_iio_device_register(&spi->dev, indio_dev);
  832. }
  833. static const struct of_device_id ad7124_of_match[] = {
  834. { .compatible = "adi,ad7124-4",
  835. .data = &ad7124_chip_info_tbl[ID_AD7124_4], },
  836. { .compatible = "adi,ad7124-8",
  837. .data = &ad7124_chip_info_tbl[ID_AD7124_8], },
  838. { },
  839. };
  840. MODULE_DEVICE_TABLE(of, ad7124_of_match);
  841. static struct spi_driver ad71124_driver = {
  842. .driver = {
  843. .name = "ad7124",
  844. .of_match_table = ad7124_of_match,
  845. },
  846. .probe = ad7124_probe,
  847. };
  848. module_spi_driver(ad71124_driver);
  849. MODULE_AUTHOR("Stefan Popa <[email protected]>");
  850. MODULE_DESCRIPTION("Analog Devices AD7124 SPI driver");
  851. MODULE_LICENSE("GPL");
  852. MODULE_IMPORT_NS(IIO_AD_SIGMA_DELTA);