ad7793.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * AD7785/AD7792/AD7793/AD7794/AD7795 SPI ADC driver
  3. *
  4. * Copyright 2011-2012 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/err.h>
  16. #include <linux/sched.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/trigger.h>
  23. #include <linux/iio/trigger_consumer.h>
  24. #include <linux/iio/triggered_buffer.h>
  25. #include <linux/iio/adc/ad_sigma_delta.h>
  26. #include <linux/platform_data/ad7793.h>
  27. /* Registers */
  28. #define AD7793_REG_COMM 0 /* Communications Register (WO, 8-bit) */
  29. #define AD7793_REG_STAT 0 /* Status Register (RO, 8-bit) */
  30. #define AD7793_REG_MODE 1 /* Mode Register (RW, 16-bit */
  31. #define AD7793_REG_CONF 2 /* Configuration Register (RW, 16-bit) */
  32. #define AD7793_REG_DATA 3 /* Data Register (RO, 16-/24-bit) */
  33. #define AD7793_REG_ID 4 /* ID Register (RO, 8-bit) */
  34. #define AD7793_REG_IO 5 /* IO Register (RO, 8-bit) */
  35. #define AD7793_REG_OFFSET 6 /* Offset Register (RW, 16-bit
  36. * (AD7792)/24-bit (AD7793)) */
  37. #define AD7793_REG_FULLSALE 7 /* Full-Scale Register
  38. * (RW, 16-bit (AD7792)/24-bit (AD7793)) */
  39. /* Communications Register Bit Designations (AD7793_REG_COMM) */
  40. #define AD7793_COMM_WEN (1 << 7) /* Write Enable */
  41. #define AD7793_COMM_WRITE (0 << 6) /* Write Operation */
  42. #define AD7793_COMM_READ (1 << 6) /* Read Operation */
  43. #define AD7793_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */
  44. #define AD7793_COMM_CREAD (1 << 2) /* Continuous Read of Data Register */
  45. /* Status Register Bit Designations (AD7793_REG_STAT) */
  46. #define AD7793_STAT_RDY (1 << 7) /* Ready */
  47. #define AD7793_STAT_ERR (1 << 6) /* Error (Overrange, Underrange) */
  48. #define AD7793_STAT_CH3 (1 << 2) /* Channel 3 */
  49. #define AD7793_STAT_CH2 (1 << 1) /* Channel 2 */
  50. #define AD7793_STAT_CH1 (1 << 0) /* Channel 1 */
  51. /* Mode Register Bit Designations (AD7793_REG_MODE) */
  52. #define AD7793_MODE_SEL(x) (((x) & 0x7) << 13) /* Operation Mode Select */
  53. #define AD7793_MODE_SEL_MASK (0x7 << 13) /* Operation Mode Select mask */
  54. #define AD7793_MODE_CLKSRC(x) (((x) & 0x3) << 6) /* ADC Clock Source Select */
  55. #define AD7793_MODE_RATE(x) ((x) & 0xF) /* Filter Update Rate Select */
  56. #define AD7793_MODE_CONT 0 /* Continuous Conversion Mode */
  57. #define AD7793_MODE_SINGLE 1 /* Single Conversion Mode */
  58. #define AD7793_MODE_IDLE 2 /* Idle Mode */
  59. #define AD7793_MODE_PWRDN 3 /* Power-Down Mode */
  60. #define AD7793_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
  61. #define AD7793_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
  62. #define AD7793_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
  63. #define AD7793_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */
  64. #define AD7793_CLK_INT 0 /* Internal 64 kHz Clock not
  65. * available at the CLK pin */
  66. #define AD7793_CLK_INT_CO 1 /* Internal 64 kHz Clock available
  67. * at the CLK pin */
  68. #define AD7793_CLK_EXT 2 /* External 64 kHz Clock */
  69. #define AD7793_CLK_EXT_DIV2 3 /* External Clock divided by 2 */
  70. /* Configuration Register Bit Designations (AD7793_REG_CONF) */
  71. #define AD7793_CONF_VBIAS(x) (((x) & 0x3) << 14) /* Bias Voltage
  72. * Generator Enable */
  73. #define AD7793_CONF_BO_EN (1 << 13) /* Burnout Current Enable */
  74. #define AD7793_CONF_UNIPOLAR (1 << 12) /* Unipolar/Bipolar Enable */
  75. #define AD7793_CONF_BOOST (1 << 11) /* Boost Enable */
  76. #define AD7793_CONF_GAIN(x) (((x) & 0x7) << 8) /* Gain Select */
  77. #define AD7793_CONF_REFSEL(x) ((x) << 6) /* INT/EXT Reference Select */
  78. #define AD7793_CONF_BUF (1 << 4) /* Buffered Mode Enable */
  79. #define AD7793_CONF_CHAN(x) ((x) & 0xf) /* Channel select */
  80. #define AD7793_CONF_CHAN_MASK 0xf /* Channel select mask */
  81. #define AD7793_CH_AIN1P_AIN1M 0 /* AIN1(+) - AIN1(-) */
  82. #define AD7793_CH_AIN2P_AIN2M 1 /* AIN2(+) - AIN2(-) */
  83. #define AD7793_CH_AIN3P_AIN3M 2 /* AIN3(+) - AIN3(-) */
  84. #define AD7793_CH_AIN1M_AIN1M 3 /* AIN1(-) - AIN1(-) */
  85. #define AD7793_CH_TEMP 6 /* Temp Sensor */
  86. #define AD7793_CH_AVDD_MONITOR 7 /* AVDD Monitor */
  87. #define AD7795_CH_AIN4P_AIN4M 4 /* AIN4(+) - AIN4(-) */
  88. #define AD7795_CH_AIN5P_AIN5M 5 /* AIN5(+) - AIN5(-) */
  89. #define AD7795_CH_AIN6P_AIN6M 6 /* AIN6(+) - AIN6(-) */
  90. #define AD7795_CH_AIN1M_AIN1M 8 /* AIN1(-) - AIN1(-) */
  91. /* ID Register Bit Designations (AD7793_REG_ID) */
  92. #define AD7785_ID 0x3
  93. #define AD7792_ID 0xA
  94. #define AD7793_ID 0xB
  95. #define AD7794_ID 0xF
  96. #define AD7795_ID 0xF
  97. #define AD7796_ID 0xA
  98. #define AD7797_ID 0xB
  99. #define AD7798_ID 0x8
  100. #define AD7799_ID 0x9
  101. #define AD7793_ID_MASK 0xF
  102. /* IO (Excitation Current Sources) Register Bit Designations (AD7793_REG_IO) */
  103. #define AD7793_IO_IEXC1_IOUT1_IEXC2_IOUT2 0 /* IEXC1 connect to IOUT1,
  104. * IEXC2 connect to IOUT2 */
  105. #define AD7793_IO_IEXC1_IOUT2_IEXC2_IOUT1 1 /* IEXC1 connect to IOUT2,
  106. * IEXC2 connect to IOUT1 */
  107. #define AD7793_IO_IEXC1_IEXC2_IOUT1 2 /* Both current sources
  108. * IEXC1,2 connect to IOUT1 */
  109. #define AD7793_IO_IEXC1_IEXC2_IOUT2 3 /* Both current sources
  110. * IEXC1,2 connect to IOUT2 */
  111. #define AD7793_IO_IXCEN_10uA (1 << 0) /* Excitation Current 10uA */
  112. #define AD7793_IO_IXCEN_210uA (2 << 0) /* Excitation Current 210uA */
  113. #define AD7793_IO_IXCEN_1mA (3 << 0) /* Excitation Current 1mA */
  114. /* NOTE:
  115. * The AD7792/AD7793 features a dual use data out ready DOUT/RDY output.
  116. * In order to avoid contentions on the SPI bus, it's therefore necessary
  117. * to use spi bus locking.
  118. *
  119. * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
  120. */
  121. #define AD7793_FLAG_HAS_CLKSEL BIT(0)
  122. #define AD7793_FLAG_HAS_REFSEL BIT(1)
  123. #define AD7793_FLAG_HAS_VBIAS BIT(2)
  124. #define AD7793_HAS_EXITATION_CURRENT BIT(3)
  125. #define AD7793_FLAG_HAS_GAIN BIT(4)
  126. #define AD7793_FLAG_HAS_BUFFER BIT(5)
  127. struct ad7793_chip_info {
  128. unsigned int id;
  129. const struct iio_chan_spec *channels;
  130. unsigned int num_channels;
  131. unsigned int flags;
  132. const struct iio_info *iio_info;
  133. const u16 *sample_freq_avail;
  134. };
  135. struct ad7793_state {
  136. const struct ad7793_chip_info *chip_info;
  137. struct regulator *reg;
  138. u16 int_vref_mv;
  139. u16 mode;
  140. u16 conf;
  141. u32 scale_avail[8][2];
  142. struct ad_sigma_delta sd;
  143. };
  144. enum ad7793_supported_device_ids {
  145. ID_AD7785,
  146. ID_AD7792,
  147. ID_AD7793,
  148. ID_AD7794,
  149. ID_AD7795,
  150. ID_AD7796,
  151. ID_AD7797,
  152. ID_AD7798,
  153. ID_AD7799,
  154. };
  155. static struct ad7793_state *ad_sigma_delta_to_ad7793(struct ad_sigma_delta *sd)
  156. {
  157. return container_of(sd, struct ad7793_state, sd);
  158. }
  159. static int ad7793_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
  160. {
  161. struct ad7793_state *st = ad_sigma_delta_to_ad7793(sd);
  162. st->conf &= ~AD7793_CONF_CHAN_MASK;
  163. st->conf |= AD7793_CONF_CHAN(channel);
  164. return ad_sd_write_reg(&st->sd, AD7793_REG_CONF, 2, st->conf);
  165. }
  166. static int ad7793_set_mode(struct ad_sigma_delta *sd,
  167. enum ad_sigma_delta_mode mode)
  168. {
  169. struct ad7793_state *st = ad_sigma_delta_to_ad7793(sd);
  170. st->mode &= ~AD7793_MODE_SEL_MASK;
  171. st->mode |= AD7793_MODE_SEL(mode);
  172. return ad_sd_write_reg(&st->sd, AD7793_REG_MODE, 2, st->mode);
  173. }
  174. static const struct ad_sigma_delta_info ad7793_sigma_delta_info = {
  175. .set_channel = ad7793_set_channel,
  176. .set_mode = ad7793_set_mode,
  177. .has_registers = true,
  178. .addr_shift = 3,
  179. .read_mask = BIT(6),
  180. };
  181. static const struct ad_sd_calib_data ad7793_calib_arr[6] = {
  182. {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN1P_AIN1M},
  183. {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN1P_AIN1M},
  184. {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN2P_AIN2M},
  185. {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN2P_AIN2M},
  186. {AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN3P_AIN3M},
  187. {AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN3P_AIN3M}
  188. };
  189. static int ad7793_calibrate_all(struct ad7793_state *st)
  190. {
  191. return ad_sd_calibrate_all(&st->sd, ad7793_calib_arr,
  192. ARRAY_SIZE(ad7793_calib_arr));
  193. }
  194. static int ad7793_check_platform_data(struct ad7793_state *st,
  195. const struct ad7793_platform_data *pdata)
  196. {
  197. if ((pdata->current_source_direction == AD7793_IEXEC1_IEXEC2_IOUT1 ||
  198. pdata->current_source_direction == AD7793_IEXEC1_IEXEC2_IOUT2) &&
  199. ((pdata->exitation_current != AD7793_IX_10uA) &&
  200. (pdata->exitation_current != AD7793_IX_210uA)))
  201. return -EINVAL;
  202. if (!(st->chip_info->flags & AD7793_FLAG_HAS_CLKSEL) &&
  203. pdata->clock_src != AD7793_CLK_SRC_INT)
  204. return -EINVAL;
  205. if (!(st->chip_info->flags & AD7793_FLAG_HAS_REFSEL) &&
  206. pdata->refsel != AD7793_REFSEL_REFIN1)
  207. return -EINVAL;
  208. if (!(st->chip_info->flags & AD7793_FLAG_HAS_VBIAS) &&
  209. pdata->bias_voltage != AD7793_BIAS_VOLTAGE_DISABLED)
  210. return -EINVAL;
  211. if (!(st->chip_info->flags & AD7793_HAS_EXITATION_CURRENT) &&
  212. pdata->exitation_current != AD7793_IX_DISABLED)
  213. return -EINVAL;
  214. return 0;
  215. }
  216. static int ad7793_setup(struct iio_dev *indio_dev,
  217. const struct ad7793_platform_data *pdata,
  218. unsigned int vref_mv)
  219. {
  220. struct ad7793_state *st = iio_priv(indio_dev);
  221. int i, ret;
  222. unsigned long long scale_uv;
  223. u32 id;
  224. ret = ad7793_check_platform_data(st, pdata);
  225. if (ret)
  226. return ret;
  227. /* reset the serial interface */
  228. ret = ad_sd_reset(&st->sd, 32);
  229. if (ret < 0)
  230. goto out;
  231. usleep_range(500, 2000); /* Wait for at least 500us */
  232. /* write/read test for device presence */
  233. ret = ad_sd_read_reg(&st->sd, AD7793_REG_ID, 1, &id);
  234. if (ret)
  235. goto out;
  236. id &= AD7793_ID_MASK;
  237. if (id != st->chip_info->id) {
  238. dev_err(&st->sd.spi->dev, "device ID query failed\n");
  239. goto out;
  240. }
  241. st->mode = AD7793_MODE_RATE(1);
  242. st->conf = 0;
  243. if (st->chip_info->flags & AD7793_FLAG_HAS_CLKSEL)
  244. st->mode |= AD7793_MODE_CLKSRC(pdata->clock_src);
  245. if (st->chip_info->flags & AD7793_FLAG_HAS_REFSEL)
  246. st->conf |= AD7793_CONF_REFSEL(pdata->refsel);
  247. if (st->chip_info->flags & AD7793_FLAG_HAS_VBIAS)
  248. st->conf |= AD7793_CONF_VBIAS(pdata->bias_voltage);
  249. if (pdata->buffered || !(st->chip_info->flags & AD7793_FLAG_HAS_BUFFER))
  250. st->conf |= AD7793_CONF_BUF;
  251. if (pdata->boost_enable &&
  252. (st->chip_info->flags & AD7793_FLAG_HAS_VBIAS))
  253. st->conf |= AD7793_CONF_BOOST;
  254. if (pdata->burnout_current)
  255. st->conf |= AD7793_CONF_BO_EN;
  256. if (pdata->unipolar)
  257. st->conf |= AD7793_CONF_UNIPOLAR;
  258. if (!(st->chip_info->flags & AD7793_FLAG_HAS_GAIN))
  259. st->conf |= AD7793_CONF_GAIN(7);
  260. ret = ad7793_set_mode(&st->sd, AD_SD_MODE_IDLE);
  261. if (ret)
  262. goto out;
  263. ret = ad7793_set_channel(&st->sd, 0);
  264. if (ret)
  265. goto out;
  266. if (st->chip_info->flags & AD7793_HAS_EXITATION_CURRENT) {
  267. ret = ad_sd_write_reg(&st->sd, AD7793_REG_IO, 1,
  268. pdata->exitation_current |
  269. (pdata->current_source_direction << 2));
  270. if (ret)
  271. goto out;
  272. }
  273. ret = ad7793_calibrate_all(st);
  274. if (ret)
  275. goto out;
  276. /* Populate available ADC input ranges */
  277. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
  278. scale_uv = ((u64)vref_mv * 100000000)
  279. >> (st->chip_info->channels[0].scan_type.realbits -
  280. (!!(st->conf & AD7793_CONF_UNIPOLAR) ? 0 : 1));
  281. scale_uv >>= i;
  282. st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
  283. st->scale_avail[i][0] = scale_uv;
  284. }
  285. return 0;
  286. out:
  287. dev_err(&st->sd.spi->dev, "setup failed\n");
  288. return ret;
  289. }
  290. static const u16 ad7793_sample_freq_avail[16] = {0, 470, 242, 123, 62, 50, 39,
  291. 33, 19, 17, 16, 12, 10, 8, 6, 4};
  292. static const u16 ad7797_sample_freq_avail[16] = {0, 0, 0, 123, 62, 50, 0,
  293. 33, 0, 17, 16, 12, 10, 8, 6, 4};
  294. static ssize_t ad7793_read_frequency(struct device *dev,
  295. struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  299. struct ad7793_state *st = iio_priv(indio_dev);
  300. return sprintf(buf, "%d\n",
  301. st->chip_info->sample_freq_avail[AD7793_MODE_RATE(st->mode)]);
  302. }
  303. static ssize_t ad7793_write_frequency(struct device *dev,
  304. struct device_attribute *attr,
  305. const char *buf,
  306. size_t len)
  307. {
  308. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  309. struct ad7793_state *st = iio_priv(indio_dev);
  310. long lval;
  311. int i, ret;
  312. mutex_lock(&indio_dev->mlock);
  313. if (iio_buffer_enabled(indio_dev)) {
  314. mutex_unlock(&indio_dev->mlock);
  315. return -EBUSY;
  316. }
  317. mutex_unlock(&indio_dev->mlock);
  318. ret = kstrtol(buf, 10, &lval);
  319. if (ret)
  320. return ret;
  321. if (lval == 0)
  322. return -EINVAL;
  323. ret = -EINVAL;
  324. for (i = 0; i < 16; i++)
  325. if (lval == st->chip_info->sample_freq_avail[i]) {
  326. mutex_lock(&indio_dev->mlock);
  327. st->mode &= ~AD7793_MODE_RATE(-1);
  328. st->mode |= AD7793_MODE_RATE(i);
  329. ad_sd_write_reg(&st->sd, AD7793_REG_MODE,
  330. sizeof(st->mode), st->mode);
  331. mutex_unlock(&indio_dev->mlock);
  332. ret = 0;
  333. }
  334. return ret ? ret : len;
  335. }
  336. static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
  337. ad7793_read_frequency,
  338. ad7793_write_frequency);
  339. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
  340. "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4");
  341. static IIO_CONST_ATTR_NAMED(sampling_frequency_available_ad7797,
  342. sampling_frequency_available, "123 62 50 33 17 16 12 10 8 6 4");
  343. static ssize_t ad7793_show_scale_available(struct device *dev,
  344. struct device_attribute *attr, char *buf)
  345. {
  346. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  347. struct ad7793_state *st = iio_priv(indio_dev);
  348. int i, len = 0;
  349. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
  350. len += sprintf(buf + len, "%d.%09u ", st->scale_avail[i][0],
  351. st->scale_avail[i][1]);
  352. len += sprintf(buf + len, "\n");
  353. return len;
  354. }
  355. static IIO_DEVICE_ATTR_NAMED(in_m_in_scale_available,
  356. in_voltage-voltage_scale_available, S_IRUGO,
  357. ad7793_show_scale_available, NULL, 0);
  358. static struct attribute *ad7793_attributes[] = {
  359. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  360. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  361. &iio_dev_attr_in_m_in_scale_available.dev_attr.attr,
  362. NULL
  363. };
  364. static const struct attribute_group ad7793_attribute_group = {
  365. .attrs = ad7793_attributes,
  366. };
  367. static struct attribute *ad7797_attributes[] = {
  368. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  369. &iio_const_attr_sampling_frequency_available_ad7797.dev_attr.attr,
  370. NULL
  371. };
  372. static const struct attribute_group ad7797_attribute_group = {
  373. .attrs = ad7797_attributes,
  374. };
  375. static int ad7793_read_raw(struct iio_dev *indio_dev,
  376. struct iio_chan_spec const *chan,
  377. int *val,
  378. int *val2,
  379. long m)
  380. {
  381. struct ad7793_state *st = iio_priv(indio_dev);
  382. int ret;
  383. unsigned long long scale_uv;
  384. bool unipolar = !!(st->conf & AD7793_CONF_UNIPOLAR);
  385. switch (m) {
  386. case IIO_CHAN_INFO_RAW:
  387. ret = ad_sigma_delta_single_conversion(indio_dev, chan, val);
  388. if (ret < 0)
  389. return ret;
  390. return IIO_VAL_INT;
  391. case IIO_CHAN_INFO_SCALE:
  392. switch (chan->type) {
  393. case IIO_VOLTAGE:
  394. if (chan->differential) {
  395. *val = st->
  396. scale_avail[(st->conf >> 8) & 0x7][0];
  397. *val2 = st->
  398. scale_avail[(st->conf >> 8) & 0x7][1];
  399. return IIO_VAL_INT_PLUS_NANO;
  400. } else {
  401. /* 1170mV / 2^23 * 6 */
  402. scale_uv = (1170ULL * 1000000000ULL * 6ULL);
  403. }
  404. break;
  405. case IIO_TEMP:
  406. /* 1170mV / 0.81 mV/C / 2^23 */
  407. scale_uv = 1444444444444444ULL;
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. scale_uv >>= (chan->scan_type.realbits - (unipolar ? 0 : 1));
  413. *val = 0;
  414. *val2 = scale_uv;
  415. return IIO_VAL_INT_PLUS_NANO;
  416. case IIO_CHAN_INFO_OFFSET:
  417. if (!unipolar)
  418. *val = -(1 << (chan->scan_type.realbits - 1));
  419. else
  420. *val = 0;
  421. /* Kelvin to Celsius */
  422. if (chan->type == IIO_TEMP) {
  423. unsigned long long offset;
  424. unsigned int shift;
  425. shift = chan->scan_type.realbits - (unipolar ? 0 : 1);
  426. offset = 273ULL << shift;
  427. do_div(offset, 1444);
  428. *val -= offset;
  429. }
  430. return IIO_VAL_INT;
  431. }
  432. return -EINVAL;
  433. }
  434. static int ad7793_write_raw(struct iio_dev *indio_dev,
  435. struct iio_chan_spec const *chan,
  436. int val,
  437. int val2,
  438. long mask)
  439. {
  440. struct ad7793_state *st = iio_priv(indio_dev);
  441. int ret, i;
  442. unsigned int tmp;
  443. mutex_lock(&indio_dev->mlock);
  444. if (iio_buffer_enabled(indio_dev)) {
  445. mutex_unlock(&indio_dev->mlock);
  446. return -EBUSY;
  447. }
  448. switch (mask) {
  449. case IIO_CHAN_INFO_SCALE:
  450. ret = -EINVAL;
  451. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
  452. if (val2 == st->scale_avail[i][1]) {
  453. ret = 0;
  454. tmp = st->conf;
  455. st->conf &= ~AD7793_CONF_GAIN(-1);
  456. st->conf |= AD7793_CONF_GAIN(i);
  457. if (tmp == st->conf)
  458. break;
  459. ad_sd_write_reg(&st->sd, AD7793_REG_CONF,
  460. sizeof(st->conf), st->conf);
  461. ad7793_calibrate_all(st);
  462. break;
  463. }
  464. break;
  465. default:
  466. ret = -EINVAL;
  467. }
  468. mutex_unlock(&indio_dev->mlock);
  469. return ret;
  470. }
  471. static int ad7793_write_raw_get_fmt(struct iio_dev *indio_dev,
  472. struct iio_chan_spec const *chan,
  473. long mask)
  474. {
  475. return IIO_VAL_INT_PLUS_NANO;
  476. }
  477. static const struct iio_info ad7793_info = {
  478. .read_raw = &ad7793_read_raw,
  479. .write_raw = &ad7793_write_raw,
  480. .write_raw_get_fmt = &ad7793_write_raw_get_fmt,
  481. .attrs = &ad7793_attribute_group,
  482. .validate_trigger = ad_sd_validate_trigger,
  483. .driver_module = THIS_MODULE,
  484. };
  485. static const struct iio_info ad7797_info = {
  486. .read_raw = &ad7793_read_raw,
  487. .write_raw = &ad7793_write_raw,
  488. .write_raw_get_fmt = &ad7793_write_raw_get_fmt,
  489. .attrs = &ad7793_attribute_group,
  490. .validate_trigger = ad_sd_validate_trigger,
  491. .driver_module = THIS_MODULE,
  492. };
  493. #define DECLARE_AD7793_CHANNELS(_name, _b, _sb, _s) \
  494. const struct iio_chan_spec _name##_channels[] = { \
  495. AD_SD_DIFF_CHANNEL(0, 0, 0, AD7793_CH_AIN1P_AIN1M, (_b), (_sb), (_s)), \
  496. AD_SD_DIFF_CHANNEL(1, 1, 1, AD7793_CH_AIN2P_AIN2M, (_b), (_sb), (_s)), \
  497. AD_SD_DIFF_CHANNEL(2, 2, 2, AD7793_CH_AIN3P_AIN3M, (_b), (_sb), (_s)), \
  498. AD_SD_SHORTED_CHANNEL(3, 0, AD7793_CH_AIN1M_AIN1M, (_b), (_sb), (_s)), \
  499. AD_SD_TEMP_CHANNEL(4, AD7793_CH_TEMP, (_b), (_sb), (_s)), \
  500. AD_SD_SUPPLY_CHANNEL(5, 3, AD7793_CH_AVDD_MONITOR, (_b), (_sb), (_s)), \
  501. IIO_CHAN_SOFT_TIMESTAMP(6), \
  502. }
  503. #define DECLARE_AD7795_CHANNELS(_name, _b, _sb) \
  504. const struct iio_chan_spec _name##_channels[] = { \
  505. AD_SD_DIFF_CHANNEL(0, 0, 0, AD7793_CH_AIN1P_AIN1M, (_b), (_sb), 0), \
  506. AD_SD_DIFF_CHANNEL(1, 1, 1, AD7793_CH_AIN2P_AIN2M, (_b), (_sb), 0), \
  507. AD_SD_DIFF_CHANNEL(2, 2, 2, AD7793_CH_AIN3P_AIN3M, (_b), (_sb), 0), \
  508. AD_SD_DIFF_CHANNEL(3, 3, 3, AD7795_CH_AIN4P_AIN4M, (_b), (_sb), 0), \
  509. AD_SD_DIFF_CHANNEL(4, 4, 4, AD7795_CH_AIN5P_AIN5M, (_b), (_sb), 0), \
  510. AD_SD_DIFF_CHANNEL(5, 5, 5, AD7795_CH_AIN6P_AIN6M, (_b), (_sb), 0), \
  511. AD_SD_SHORTED_CHANNEL(6, 0, AD7795_CH_AIN1M_AIN1M, (_b), (_sb), 0), \
  512. AD_SD_TEMP_CHANNEL(7, AD7793_CH_TEMP, (_b), (_sb), 0), \
  513. AD_SD_SUPPLY_CHANNEL(8, 3, AD7793_CH_AVDD_MONITOR, (_b), (_sb), 0), \
  514. IIO_CHAN_SOFT_TIMESTAMP(9), \
  515. }
  516. #define DECLARE_AD7797_CHANNELS(_name, _b, _sb) \
  517. const struct iio_chan_spec _name##_channels[] = { \
  518. AD_SD_DIFF_CHANNEL(0, 0, 0, AD7793_CH_AIN1P_AIN1M, (_b), (_sb), 0), \
  519. AD_SD_SHORTED_CHANNEL(1, 0, AD7793_CH_AIN1M_AIN1M, (_b), (_sb), 0), \
  520. AD_SD_TEMP_CHANNEL(2, AD7793_CH_TEMP, (_b), (_sb), 0), \
  521. AD_SD_SUPPLY_CHANNEL(3, 3, AD7793_CH_AVDD_MONITOR, (_b), (_sb), 0), \
  522. IIO_CHAN_SOFT_TIMESTAMP(4), \
  523. }
  524. #define DECLARE_AD7799_CHANNELS(_name, _b, _sb) \
  525. const struct iio_chan_spec _name##_channels[] = { \
  526. AD_SD_DIFF_CHANNEL(0, 0, 0, AD7793_CH_AIN1P_AIN1M, (_b), (_sb), 0), \
  527. AD_SD_DIFF_CHANNEL(1, 1, 1, AD7793_CH_AIN2P_AIN2M, (_b), (_sb), 0), \
  528. AD_SD_DIFF_CHANNEL(2, 2, 2, AD7793_CH_AIN3P_AIN3M, (_b), (_sb), 0), \
  529. AD_SD_SHORTED_CHANNEL(3, 0, AD7793_CH_AIN1M_AIN1M, (_b), (_sb), 0), \
  530. AD_SD_SUPPLY_CHANNEL(4, 3, AD7793_CH_AVDD_MONITOR, (_b), (_sb), 0), \
  531. IIO_CHAN_SOFT_TIMESTAMP(5), \
  532. }
  533. static DECLARE_AD7793_CHANNELS(ad7785, 20, 32, 4);
  534. static DECLARE_AD7793_CHANNELS(ad7792, 16, 32, 0);
  535. static DECLARE_AD7793_CHANNELS(ad7793, 24, 32, 0);
  536. static DECLARE_AD7795_CHANNELS(ad7794, 16, 32);
  537. static DECLARE_AD7795_CHANNELS(ad7795, 24, 32);
  538. static DECLARE_AD7797_CHANNELS(ad7796, 16, 16);
  539. static DECLARE_AD7797_CHANNELS(ad7797, 24, 32);
  540. static DECLARE_AD7799_CHANNELS(ad7798, 16, 16);
  541. static DECLARE_AD7799_CHANNELS(ad7799, 24, 32);
  542. static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
  543. [ID_AD7785] = {
  544. .id = AD7785_ID,
  545. .channels = ad7785_channels,
  546. .num_channels = ARRAY_SIZE(ad7785_channels),
  547. .iio_info = &ad7793_info,
  548. .sample_freq_avail = ad7793_sample_freq_avail,
  549. .flags = AD7793_FLAG_HAS_CLKSEL |
  550. AD7793_FLAG_HAS_REFSEL |
  551. AD7793_FLAG_HAS_VBIAS |
  552. AD7793_HAS_EXITATION_CURRENT |
  553. AD7793_FLAG_HAS_GAIN |
  554. AD7793_FLAG_HAS_BUFFER,
  555. },
  556. [ID_AD7792] = {
  557. .id = AD7792_ID,
  558. .channels = ad7792_channels,
  559. .num_channels = ARRAY_SIZE(ad7792_channels),
  560. .iio_info = &ad7793_info,
  561. .sample_freq_avail = ad7793_sample_freq_avail,
  562. .flags = AD7793_FLAG_HAS_CLKSEL |
  563. AD7793_FLAG_HAS_REFSEL |
  564. AD7793_FLAG_HAS_VBIAS |
  565. AD7793_HAS_EXITATION_CURRENT |
  566. AD7793_FLAG_HAS_GAIN |
  567. AD7793_FLAG_HAS_BUFFER,
  568. },
  569. [ID_AD7793] = {
  570. .id = AD7793_ID,
  571. .channels = ad7793_channels,
  572. .num_channels = ARRAY_SIZE(ad7793_channels),
  573. .iio_info = &ad7793_info,
  574. .sample_freq_avail = ad7793_sample_freq_avail,
  575. .flags = AD7793_FLAG_HAS_CLKSEL |
  576. AD7793_FLAG_HAS_REFSEL |
  577. AD7793_FLAG_HAS_VBIAS |
  578. AD7793_HAS_EXITATION_CURRENT |
  579. AD7793_FLAG_HAS_GAIN |
  580. AD7793_FLAG_HAS_BUFFER,
  581. },
  582. [ID_AD7794] = {
  583. .id = AD7794_ID,
  584. .channels = ad7794_channels,
  585. .num_channels = ARRAY_SIZE(ad7794_channels),
  586. .iio_info = &ad7793_info,
  587. .sample_freq_avail = ad7793_sample_freq_avail,
  588. .flags = AD7793_FLAG_HAS_CLKSEL |
  589. AD7793_FLAG_HAS_REFSEL |
  590. AD7793_FLAG_HAS_VBIAS |
  591. AD7793_HAS_EXITATION_CURRENT |
  592. AD7793_FLAG_HAS_GAIN |
  593. AD7793_FLAG_HAS_BUFFER,
  594. },
  595. [ID_AD7795] = {
  596. .id = AD7795_ID,
  597. .channels = ad7795_channels,
  598. .num_channels = ARRAY_SIZE(ad7795_channels),
  599. .iio_info = &ad7793_info,
  600. .sample_freq_avail = ad7793_sample_freq_avail,
  601. .flags = AD7793_FLAG_HAS_CLKSEL |
  602. AD7793_FLAG_HAS_REFSEL |
  603. AD7793_FLAG_HAS_VBIAS |
  604. AD7793_HAS_EXITATION_CURRENT |
  605. AD7793_FLAG_HAS_GAIN |
  606. AD7793_FLAG_HAS_BUFFER,
  607. },
  608. [ID_AD7796] = {
  609. .id = AD7796_ID,
  610. .channels = ad7796_channels,
  611. .num_channels = ARRAY_SIZE(ad7796_channels),
  612. .iio_info = &ad7797_info,
  613. .sample_freq_avail = ad7797_sample_freq_avail,
  614. .flags = AD7793_FLAG_HAS_CLKSEL,
  615. },
  616. [ID_AD7797] = {
  617. .id = AD7797_ID,
  618. .channels = ad7797_channels,
  619. .num_channels = ARRAY_SIZE(ad7797_channels),
  620. .iio_info = &ad7797_info,
  621. .sample_freq_avail = ad7797_sample_freq_avail,
  622. .flags = AD7793_FLAG_HAS_CLKSEL,
  623. },
  624. [ID_AD7798] = {
  625. .id = AD7798_ID,
  626. .channels = ad7798_channels,
  627. .num_channels = ARRAY_SIZE(ad7798_channels),
  628. .iio_info = &ad7793_info,
  629. .sample_freq_avail = ad7793_sample_freq_avail,
  630. .flags = AD7793_FLAG_HAS_GAIN |
  631. AD7793_FLAG_HAS_BUFFER,
  632. },
  633. [ID_AD7799] = {
  634. .id = AD7799_ID,
  635. .channels = ad7799_channels,
  636. .num_channels = ARRAY_SIZE(ad7799_channels),
  637. .iio_info = &ad7793_info,
  638. .sample_freq_avail = ad7793_sample_freq_avail,
  639. .flags = AD7793_FLAG_HAS_GAIN |
  640. AD7793_FLAG_HAS_BUFFER,
  641. },
  642. };
  643. static int ad7793_probe(struct spi_device *spi)
  644. {
  645. const struct ad7793_platform_data *pdata = spi->dev.platform_data;
  646. struct ad7793_state *st;
  647. struct iio_dev *indio_dev;
  648. int ret, vref_mv = 0;
  649. if (!pdata) {
  650. dev_err(&spi->dev, "no platform data?\n");
  651. return -ENODEV;
  652. }
  653. if (!spi->irq) {
  654. dev_err(&spi->dev, "no IRQ?\n");
  655. return -ENODEV;
  656. }
  657. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  658. if (indio_dev == NULL)
  659. return -ENOMEM;
  660. st = iio_priv(indio_dev);
  661. ad_sd_init(&st->sd, indio_dev, spi, &ad7793_sigma_delta_info);
  662. if (pdata->refsel != AD7793_REFSEL_INTERNAL) {
  663. st->reg = devm_regulator_get(&spi->dev, "refin");
  664. if (IS_ERR(st->reg))
  665. return PTR_ERR(st->reg);
  666. ret = regulator_enable(st->reg);
  667. if (ret)
  668. return ret;
  669. vref_mv = regulator_get_voltage(st->reg);
  670. if (vref_mv < 0) {
  671. ret = vref_mv;
  672. goto error_disable_reg;
  673. }
  674. vref_mv /= 1000;
  675. } else {
  676. vref_mv = 1170; /* Build-in ref */
  677. }
  678. st->chip_info =
  679. &ad7793_chip_info_tbl[spi_get_device_id(spi)->driver_data];
  680. spi_set_drvdata(spi, indio_dev);
  681. indio_dev->dev.parent = &spi->dev;
  682. indio_dev->name = spi_get_device_id(spi)->name;
  683. indio_dev->modes = INDIO_DIRECT_MODE;
  684. indio_dev->channels = st->chip_info->channels;
  685. indio_dev->num_channels = st->chip_info->num_channels;
  686. indio_dev->info = st->chip_info->iio_info;
  687. ret = ad_sd_setup_buffer_and_trigger(indio_dev);
  688. if (ret)
  689. goto error_disable_reg;
  690. ret = ad7793_setup(indio_dev, pdata, vref_mv);
  691. if (ret)
  692. goto error_remove_trigger;
  693. ret = iio_device_register(indio_dev);
  694. if (ret)
  695. goto error_remove_trigger;
  696. return 0;
  697. error_remove_trigger:
  698. ad_sd_cleanup_buffer_and_trigger(indio_dev);
  699. error_disable_reg:
  700. if (pdata->refsel != AD7793_REFSEL_INTERNAL)
  701. regulator_disable(st->reg);
  702. return ret;
  703. }
  704. static int ad7793_remove(struct spi_device *spi)
  705. {
  706. const struct ad7793_platform_data *pdata = spi->dev.platform_data;
  707. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  708. struct ad7793_state *st = iio_priv(indio_dev);
  709. iio_device_unregister(indio_dev);
  710. ad_sd_cleanup_buffer_and_trigger(indio_dev);
  711. if (pdata->refsel != AD7793_REFSEL_INTERNAL)
  712. regulator_disable(st->reg);
  713. return 0;
  714. }
  715. static const struct spi_device_id ad7793_id[] = {
  716. {"ad7785", ID_AD7785},
  717. {"ad7792", ID_AD7792},
  718. {"ad7793", ID_AD7793},
  719. {"ad7794", ID_AD7794},
  720. {"ad7795", ID_AD7795},
  721. {"ad7796", ID_AD7796},
  722. {"ad7797", ID_AD7797},
  723. {"ad7798", ID_AD7798},
  724. {"ad7799", ID_AD7799},
  725. {}
  726. };
  727. MODULE_DEVICE_TABLE(spi, ad7793_id);
  728. static struct spi_driver ad7793_driver = {
  729. .driver = {
  730. .name = "ad7793",
  731. },
  732. .probe = ad7793_probe,
  733. .remove = ad7793_remove,
  734. .id_table = ad7793_id,
  735. };
  736. module_spi_driver(ad7793_driver);
  737. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  738. MODULE_DESCRIPTION("Analog Devices AD7793 and similar ADCs");
  739. MODULE_LICENSE("GPL v2");