ad5755.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * AD5755, AD5755-1, AD5757, AD5735, AD5737 Digital to analog converters driver
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/slab.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/delay.h>
  16. #include <linux/of.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #include <linux/platform_data/ad5755.h>
  20. #define AD5755_NUM_CHANNELS 4
  21. #define AD5755_ADDR(x) ((x) << 16)
  22. #define AD5755_WRITE_REG_DATA(chan) (chan)
  23. #define AD5755_WRITE_REG_GAIN(chan) (0x08 | (chan))
  24. #define AD5755_WRITE_REG_OFFSET(chan) (0x10 | (chan))
  25. #define AD5755_WRITE_REG_CTRL(chan) (0x1c | (chan))
  26. #define AD5755_READ_REG_DATA(chan) (chan)
  27. #define AD5755_READ_REG_CTRL(chan) (0x4 | (chan))
  28. #define AD5755_READ_REG_GAIN(chan) (0x8 | (chan))
  29. #define AD5755_READ_REG_OFFSET(chan) (0xc | (chan))
  30. #define AD5755_READ_REG_CLEAR(chan) (0x10 | (chan))
  31. #define AD5755_READ_REG_SLEW(chan) (0x14 | (chan))
  32. #define AD5755_READ_REG_STATUS 0x18
  33. #define AD5755_READ_REG_MAIN 0x19
  34. #define AD5755_READ_REG_DC_DC 0x1a
  35. #define AD5755_CTRL_REG_SLEW 0x0
  36. #define AD5755_CTRL_REG_MAIN 0x1
  37. #define AD5755_CTRL_REG_DAC 0x2
  38. #define AD5755_CTRL_REG_DC_DC 0x3
  39. #define AD5755_CTRL_REG_SW 0x4
  40. #define AD5755_READ_FLAG 0x800000
  41. #define AD5755_NOOP 0x1CE000
  42. #define AD5755_DAC_INT_EN BIT(8)
  43. #define AD5755_DAC_CLR_EN BIT(7)
  44. #define AD5755_DAC_OUT_EN BIT(6)
  45. #define AD5755_DAC_INT_CURRENT_SENSE_RESISTOR BIT(5)
  46. #define AD5755_DAC_DC_DC_EN BIT(4)
  47. #define AD5755_DAC_VOLTAGE_OVERRANGE_EN BIT(3)
  48. #define AD5755_DC_DC_MAXV 0
  49. #define AD5755_DC_DC_FREQ_SHIFT 2
  50. #define AD5755_DC_DC_PHASE_SHIFT 4
  51. #define AD5755_EXT_DC_DC_COMP_RES BIT(6)
  52. #define AD5755_SLEW_STEP_SIZE_SHIFT 0
  53. #define AD5755_SLEW_RATE_SHIFT 3
  54. #define AD5755_SLEW_ENABLE BIT(12)
  55. /**
  56. * struct ad5755_chip_info - chip specific information
  57. * @channel_template: channel specification
  58. * @calib_shift: shift for the calibration data registers
  59. * @has_voltage_out: whether the chip has voltage outputs
  60. */
  61. struct ad5755_chip_info {
  62. const struct iio_chan_spec channel_template;
  63. unsigned int calib_shift;
  64. bool has_voltage_out;
  65. };
  66. /**
  67. * struct ad5755_state - driver instance specific data
  68. * @spi: spi device the driver is attached to
  69. * @chip_info: chip model specific constants, available modes etc
  70. * @pwr_down: bitmask which contains hether a channel is powered down or not
  71. * @ctrl: software shadow of the channel ctrl registers
  72. * @channels: iio channel spec for the device
  73. * @data: spi transfer buffers
  74. */
  75. struct ad5755_state {
  76. struct spi_device *spi;
  77. const struct ad5755_chip_info *chip_info;
  78. unsigned int pwr_down;
  79. unsigned int ctrl[AD5755_NUM_CHANNELS];
  80. struct iio_chan_spec channels[AD5755_NUM_CHANNELS];
  81. /*
  82. * DMA (thus cache coherency maintenance) requires the
  83. * transfer buffers to live in their own cache lines.
  84. */
  85. union {
  86. __be32 d32;
  87. u8 d8[4];
  88. } data[2] ____cacheline_aligned;
  89. };
  90. enum ad5755_type {
  91. ID_AD5755,
  92. ID_AD5757,
  93. ID_AD5735,
  94. ID_AD5737,
  95. };
  96. #ifdef CONFIG_OF
  97. static const int ad5755_dcdc_freq_table[][2] = {
  98. { 250000, AD5755_DC_DC_FREQ_250kHZ },
  99. { 410000, AD5755_DC_DC_FREQ_410kHZ },
  100. { 650000, AD5755_DC_DC_FREQ_650kHZ }
  101. };
  102. static const int ad5755_dcdc_maxv_table[][2] = {
  103. { 23000000, AD5755_DC_DC_MAXV_23V },
  104. { 24500000, AD5755_DC_DC_MAXV_24V5 },
  105. { 27000000, AD5755_DC_DC_MAXV_27V },
  106. { 29500000, AD5755_DC_DC_MAXV_29V5 },
  107. };
  108. static const int ad5755_slew_rate_table[][2] = {
  109. { 64000, AD5755_SLEW_RATE_64k },
  110. { 32000, AD5755_SLEW_RATE_32k },
  111. { 16000, AD5755_SLEW_RATE_16k },
  112. { 8000, AD5755_SLEW_RATE_8k },
  113. { 4000, AD5755_SLEW_RATE_4k },
  114. { 2000, AD5755_SLEW_RATE_2k },
  115. { 1000, AD5755_SLEW_RATE_1k },
  116. { 500, AD5755_SLEW_RATE_500 },
  117. { 250, AD5755_SLEW_RATE_250 },
  118. { 125, AD5755_SLEW_RATE_125 },
  119. { 64, AD5755_SLEW_RATE_64 },
  120. { 32, AD5755_SLEW_RATE_32 },
  121. { 16, AD5755_SLEW_RATE_16 },
  122. { 8, AD5755_SLEW_RATE_8 },
  123. { 4, AD5755_SLEW_RATE_4 },
  124. { 0, AD5755_SLEW_RATE_0_5 },
  125. };
  126. static const int ad5755_slew_step_table[][2] = {
  127. { 256, AD5755_SLEW_STEP_SIZE_256 },
  128. { 128, AD5755_SLEW_STEP_SIZE_128 },
  129. { 64, AD5755_SLEW_STEP_SIZE_64 },
  130. { 32, AD5755_SLEW_STEP_SIZE_32 },
  131. { 16, AD5755_SLEW_STEP_SIZE_16 },
  132. { 4, AD5755_SLEW_STEP_SIZE_4 },
  133. { 2, AD5755_SLEW_STEP_SIZE_2 },
  134. { 1, AD5755_SLEW_STEP_SIZE_1 },
  135. };
  136. #endif
  137. static int ad5755_write_unlocked(struct iio_dev *indio_dev,
  138. unsigned int reg, unsigned int val)
  139. {
  140. struct ad5755_state *st = iio_priv(indio_dev);
  141. st->data[0].d32 = cpu_to_be32((reg << 16) | val);
  142. return spi_write(st->spi, &st->data[0].d8[1], 3);
  143. }
  144. static int ad5755_write_ctrl_unlocked(struct iio_dev *indio_dev,
  145. unsigned int channel, unsigned int reg, unsigned int val)
  146. {
  147. return ad5755_write_unlocked(indio_dev,
  148. AD5755_WRITE_REG_CTRL(channel), (reg << 13) | val);
  149. }
  150. static int ad5755_write(struct iio_dev *indio_dev, unsigned int reg,
  151. unsigned int val)
  152. {
  153. int ret;
  154. mutex_lock(&indio_dev->mlock);
  155. ret = ad5755_write_unlocked(indio_dev, reg, val);
  156. mutex_unlock(&indio_dev->mlock);
  157. return ret;
  158. }
  159. static int ad5755_write_ctrl(struct iio_dev *indio_dev, unsigned int channel,
  160. unsigned int reg, unsigned int val)
  161. {
  162. int ret;
  163. mutex_lock(&indio_dev->mlock);
  164. ret = ad5755_write_ctrl_unlocked(indio_dev, channel, reg, val);
  165. mutex_unlock(&indio_dev->mlock);
  166. return ret;
  167. }
  168. static int ad5755_read(struct iio_dev *indio_dev, unsigned int addr)
  169. {
  170. struct ad5755_state *st = iio_priv(indio_dev);
  171. int ret;
  172. struct spi_transfer t[] = {
  173. {
  174. .tx_buf = &st->data[0].d8[1],
  175. .len = 3,
  176. .cs_change = 1,
  177. }, {
  178. .tx_buf = &st->data[1].d8[1],
  179. .rx_buf = &st->data[1].d8[1],
  180. .len = 3,
  181. },
  182. };
  183. mutex_lock(&indio_dev->mlock);
  184. st->data[0].d32 = cpu_to_be32(AD5755_READ_FLAG | (addr << 16));
  185. st->data[1].d32 = cpu_to_be32(AD5755_NOOP);
  186. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  187. if (ret >= 0)
  188. ret = be32_to_cpu(st->data[1].d32) & 0xffff;
  189. mutex_unlock(&indio_dev->mlock);
  190. return ret;
  191. }
  192. static int ad5755_update_dac_ctrl(struct iio_dev *indio_dev,
  193. unsigned int channel, unsigned int set, unsigned int clr)
  194. {
  195. struct ad5755_state *st = iio_priv(indio_dev);
  196. int ret;
  197. st->ctrl[channel] |= set;
  198. st->ctrl[channel] &= ~clr;
  199. ret = ad5755_write_ctrl_unlocked(indio_dev, channel,
  200. AD5755_CTRL_REG_DAC, st->ctrl[channel]);
  201. return ret;
  202. }
  203. static int ad5755_set_channel_pwr_down(struct iio_dev *indio_dev,
  204. unsigned int channel, bool pwr_down)
  205. {
  206. struct ad5755_state *st = iio_priv(indio_dev);
  207. unsigned int mask = BIT(channel);
  208. mutex_lock(&indio_dev->mlock);
  209. if ((bool)(st->pwr_down & mask) == pwr_down)
  210. goto out_unlock;
  211. if (!pwr_down) {
  212. st->pwr_down &= ~mask;
  213. ad5755_update_dac_ctrl(indio_dev, channel,
  214. AD5755_DAC_INT_EN | AD5755_DAC_DC_DC_EN, 0);
  215. udelay(200);
  216. ad5755_update_dac_ctrl(indio_dev, channel,
  217. AD5755_DAC_OUT_EN, 0);
  218. } else {
  219. st->pwr_down |= mask;
  220. ad5755_update_dac_ctrl(indio_dev, channel,
  221. 0, AD5755_DAC_INT_EN | AD5755_DAC_OUT_EN |
  222. AD5755_DAC_DC_DC_EN);
  223. }
  224. out_unlock:
  225. mutex_unlock(&indio_dev->mlock);
  226. return 0;
  227. }
  228. static const int ad5755_min_max_table[][2] = {
  229. [AD5755_MODE_VOLTAGE_0V_5V] = { 0, 5000 },
  230. [AD5755_MODE_VOLTAGE_0V_10V] = { 0, 10000 },
  231. [AD5755_MODE_VOLTAGE_PLUSMINUS_5V] = { -5000, 5000 },
  232. [AD5755_MODE_VOLTAGE_PLUSMINUS_10V] = { -10000, 10000 },
  233. [AD5755_MODE_CURRENT_4mA_20mA] = { 4, 20 },
  234. [AD5755_MODE_CURRENT_0mA_20mA] = { 0, 20 },
  235. [AD5755_MODE_CURRENT_0mA_24mA] = { 0, 24 },
  236. };
  237. static void ad5755_get_min_max(struct ad5755_state *st,
  238. struct iio_chan_spec const *chan, int *min, int *max)
  239. {
  240. enum ad5755_mode mode = st->ctrl[chan->channel] & 7;
  241. *min = ad5755_min_max_table[mode][0];
  242. *max = ad5755_min_max_table[mode][1];
  243. }
  244. static inline int ad5755_get_offset(struct ad5755_state *st,
  245. struct iio_chan_spec const *chan)
  246. {
  247. int min, max;
  248. ad5755_get_min_max(st, chan, &min, &max);
  249. return (min * (1 << chan->scan_type.realbits)) / (max - min);
  250. }
  251. static int ad5755_chan_reg_info(struct ad5755_state *st,
  252. struct iio_chan_spec const *chan, long info, bool write,
  253. unsigned int *reg, unsigned int *shift, unsigned int *offset)
  254. {
  255. switch (info) {
  256. case IIO_CHAN_INFO_RAW:
  257. if (write)
  258. *reg = AD5755_WRITE_REG_DATA(chan->address);
  259. else
  260. *reg = AD5755_READ_REG_DATA(chan->address);
  261. *shift = chan->scan_type.shift;
  262. *offset = 0;
  263. break;
  264. case IIO_CHAN_INFO_CALIBBIAS:
  265. if (write)
  266. *reg = AD5755_WRITE_REG_OFFSET(chan->address);
  267. else
  268. *reg = AD5755_READ_REG_OFFSET(chan->address);
  269. *shift = st->chip_info->calib_shift;
  270. *offset = 32768;
  271. break;
  272. case IIO_CHAN_INFO_CALIBSCALE:
  273. if (write)
  274. *reg = AD5755_WRITE_REG_GAIN(chan->address);
  275. else
  276. *reg = AD5755_READ_REG_GAIN(chan->address);
  277. *shift = st->chip_info->calib_shift;
  278. *offset = 0;
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. return 0;
  284. }
  285. static int ad5755_read_raw(struct iio_dev *indio_dev,
  286. const struct iio_chan_spec *chan, int *val, int *val2, long info)
  287. {
  288. struct ad5755_state *st = iio_priv(indio_dev);
  289. unsigned int reg, shift, offset;
  290. int min, max;
  291. int ret;
  292. switch (info) {
  293. case IIO_CHAN_INFO_SCALE:
  294. ad5755_get_min_max(st, chan, &min, &max);
  295. *val = max - min;
  296. *val2 = chan->scan_type.realbits;
  297. return IIO_VAL_FRACTIONAL_LOG2;
  298. case IIO_CHAN_INFO_OFFSET:
  299. *val = ad5755_get_offset(st, chan);
  300. return IIO_VAL_INT;
  301. default:
  302. ret = ad5755_chan_reg_info(st, chan, info, false,
  303. &reg, &shift, &offset);
  304. if (ret)
  305. return ret;
  306. ret = ad5755_read(indio_dev, reg);
  307. if (ret < 0)
  308. return ret;
  309. *val = (ret - offset) >> shift;
  310. return IIO_VAL_INT;
  311. }
  312. return -EINVAL;
  313. }
  314. static int ad5755_write_raw(struct iio_dev *indio_dev,
  315. const struct iio_chan_spec *chan, int val, int val2, long info)
  316. {
  317. struct ad5755_state *st = iio_priv(indio_dev);
  318. unsigned int shift, reg, offset;
  319. int ret;
  320. ret = ad5755_chan_reg_info(st, chan, info, true,
  321. &reg, &shift, &offset);
  322. if (ret)
  323. return ret;
  324. val <<= shift;
  325. val += offset;
  326. if (val < 0 || val > 0xffff)
  327. return -EINVAL;
  328. return ad5755_write(indio_dev, reg, val);
  329. }
  330. static ssize_t ad5755_read_powerdown(struct iio_dev *indio_dev, uintptr_t priv,
  331. const struct iio_chan_spec *chan, char *buf)
  332. {
  333. struct ad5755_state *st = iio_priv(indio_dev);
  334. return sprintf(buf, "%d\n",
  335. (bool)(st->pwr_down & (1 << chan->channel)));
  336. }
  337. static ssize_t ad5755_write_powerdown(struct iio_dev *indio_dev, uintptr_t priv,
  338. struct iio_chan_spec const *chan, const char *buf, size_t len)
  339. {
  340. bool pwr_down;
  341. int ret;
  342. ret = strtobool(buf, &pwr_down);
  343. if (ret)
  344. return ret;
  345. ret = ad5755_set_channel_pwr_down(indio_dev, chan->channel, pwr_down);
  346. return ret ? ret : len;
  347. }
  348. static const struct iio_info ad5755_info = {
  349. .read_raw = ad5755_read_raw,
  350. .write_raw = ad5755_write_raw,
  351. .driver_module = THIS_MODULE,
  352. };
  353. static const struct iio_chan_spec_ext_info ad5755_ext_info[] = {
  354. {
  355. .name = "powerdown",
  356. .read = ad5755_read_powerdown,
  357. .write = ad5755_write_powerdown,
  358. .shared = IIO_SEPARATE,
  359. },
  360. { },
  361. };
  362. #define AD5755_CHANNEL(_bits) { \
  363. .indexed = 1, \
  364. .output = 1, \
  365. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  366. BIT(IIO_CHAN_INFO_SCALE) | \
  367. BIT(IIO_CHAN_INFO_OFFSET) | \
  368. BIT(IIO_CHAN_INFO_CALIBSCALE) | \
  369. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  370. .scan_type = { \
  371. .sign = 'u', \
  372. .realbits = (_bits), \
  373. .storagebits = 16, \
  374. .shift = 16 - (_bits), \
  375. }, \
  376. .ext_info = ad5755_ext_info, \
  377. }
  378. static const struct ad5755_chip_info ad5755_chip_info_tbl[] = {
  379. [ID_AD5735] = {
  380. .channel_template = AD5755_CHANNEL(14),
  381. .has_voltage_out = true,
  382. .calib_shift = 4,
  383. },
  384. [ID_AD5737] = {
  385. .channel_template = AD5755_CHANNEL(14),
  386. .has_voltage_out = false,
  387. .calib_shift = 4,
  388. },
  389. [ID_AD5755] = {
  390. .channel_template = AD5755_CHANNEL(16),
  391. .has_voltage_out = true,
  392. .calib_shift = 0,
  393. },
  394. [ID_AD5757] = {
  395. .channel_template = AD5755_CHANNEL(16),
  396. .has_voltage_out = false,
  397. .calib_shift = 0,
  398. },
  399. };
  400. static bool ad5755_is_valid_mode(struct ad5755_state *st, enum ad5755_mode mode)
  401. {
  402. switch (mode) {
  403. case AD5755_MODE_VOLTAGE_0V_5V:
  404. case AD5755_MODE_VOLTAGE_0V_10V:
  405. case AD5755_MODE_VOLTAGE_PLUSMINUS_5V:
  406. case AD5755_MODE_VOLTAGE_PLUSMINUS_10V:
  407. return st->chip_info->has_voltage_out;
  408. case AD5755_MODE_CURRENT_4mA_20mA:
  409. case AD5755_MODE_CURRENT_0mA_20mA:
  410. case AD5755_MODE_CURRENT_0mA_24mA:
  411. return true;
  412. default:
  413. return false;
  414. }
  415. }
  416. static int ad5755_setup_pdata(struct iio_dev *indio_dev,
  417. const struct ad5755_platform_data *pdata)
  418. {
  419. struct ad5755_state *st = iio_priv(indio_dev);
  420. unsigned int val;
  421. unsigned int i;
  422. int ret;
  423. if (pdata->dc_dc_phase > AD5755_DC_DC_PHASE_90_DEGREE ||
  424. pdata->dc_dc_freq > AD5755_DC_DC_FREQ_650kHZ ||
  425. pdata->dc_dc_maxv > AD5755_DC_DC_MAXV_29V5)
  426. return -EINVAL;
  427. val = pdata->dc_dc_maxv << AD5755_DC_DC_MAXV;
  428. val |= pdata->dc_dc_freq << AD5755_DC_DC_FREQ_SHIFT;
  429. val |= pdata->dc_dc_phase << AD5755_DC_DC_PHASE_SHIFT;
  430. if (pdata->ext_dc_dc_compenstation_resistor)
  431. val |= AD5755_EXT_DC_DC_COMP_RES;
  432. ret = ad5755_write_ctrl(indio_dev, 0, AD5755_CTRL_REG_DC_DC, val);
  433. if (ret < 0)
  434. return ret;
  435. for (i = 0; i < ARRAY_SIZE(pdata->dac); ++i) {
  436. val = pdata->dac[i].slew.step_size <<
  437. AD5755_SLEW_STEP_SIZE_SHIFT;
  438. val |= pdata->dac[i].slew.rate <<
  439. AD5755_SLEW_RATE_SHIFT;
  440. if (pdata->dac[i].slew.enable)
  441. val |= AD5755_SLEW_ENABLE;
  442. ret = ad5755_write_ctrl(indio_dev, i,
  443. AD5755_CTRL_REG_SLEW, val);
  444. if (ret < 0)
  445. return ret;
  446. }
  447. for (i = 0; i < ARRAY_SIZE(pdata->dac); ++i) {
  448. if (!ad5755_is_valid_mode(st, pdata->dac[i].mode))
  449. return -EINVAL;
  450. val = 0;
  451. if (!pdata->dac[i].ext_current_sense_resistor)
  452. val |= AD5755_DAC_INT_CURRENT_SENSE_RESISTOR;
  453. if (pdata->dac[i].enable_voltage_overrange)
  454. val |= AD5755_DAC_VOLTAGE_OVERRANGE_EN;
  455. val |= pdata->dac[i].mode;
  456. ret = ad5755_update_dac_ctrl(indio_dev, i, val, 0);
  457. if (ret < 0)
  458. return ret;
  459. }
  460. return 0;
  461. }
  462. static bool ad5755_is_voltage_mode(enum ad5755_mode mode)
  463. {
  464. switch (mode) {
  465. case AD5755_MODE_VOLTAGE_0V_5V:
  466. case AD5755_MODE_VOLTAGE_0V_10V:
  467. case AD5755_MODE_VOLTAGE_PLUSMINUS_5V:
  468. case AD5755_MODE_VOLTAGE_PLUSMINUS_10V:
  469. return true;
  470. default:
  471. return false;
  472. }
  473. }
  474. static int ad5755_init_channels(struct iio_dev *indio_dev,
  475. const struct ad5755_platform_data *pdata)
  476. {
  477. struct ad5755_state *st = iio_priv(indio_dev);
  478. struct iio_chan_spec *channels = st->channels;
  479. unsigned int i;
  480. for (i = 0; i < AD5755_NUM_CHANNELS; ++i) {
  481. channels[i] = st->chip_info->channel_template;
  482. channels[i].channel = i;
  483. channels[i].address = i;
  484. if (pdata && ad5755_is_voltage_mode(pdata->dac[i].mode))
  485. channels[i].type = IIO_VOLTAGE;
  486. else
  487. channels[i].type = IIO_CURRENT;
  488. }
  489. indio_dev->channels = channels;
  490. return 0;
  491. }
  492. #define AD5755_DEFAULT_DAC_PDATA { \
  493. .mode = AD5755_MODE_CURRENT_4mA_20mA, \
  494. .ext_current_sense_resistor = true, \
  495. .enable_voltage_overrange = false, \
  496. .slew = { \
  497. .enable = false, \
  498. .rate = AD5755_SLEW_RATE_64k, \
  499. .step_size = AD5755_SLEW_STEP_SIZE_1, \
  500. }, \
  501. }
  502. static const struct ad5755_platform_data ad5755_default_pdata = {
  503. .ext_dc_dc_compenstation_resistor = false,
  504. .dc_dc_phase = AD5755_DC_DC_PHASE_ALL_SAME_EDGE,
  505. .dc_dc_freq = AD5755_DC_DC_FREQ_410kHZ,
  506. .dc_dc_maxv = AD5755_DC_DC_MAXV_23V,
  507. .dac = {
  508. [0] = AD5755_DEFAULT_DAC_PDATA,
  509. [1] = AD5755_DEFAULT_DAC_PDATA,
  510. [2] = AD5755_DEFAULT_DAC_PDATA,
  511. [3] = AD5755_DEFAULT_DAC_PDATA,
  512. },
  513. };
  514. #ifdef CONFIG_OF
  515. static struct ad5755_platform_data *ad5755_parse_dt(struct device *dev)
  516. {
  517. struct device_node *np = dev->of_node;
  518. struct device_node *pp;
  519. struct ad5755_platform_data *pdata;
  520. unsigned int tmp;
  521. unsigned int tmparray[3];
  522. int devnr, i;
  523. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  524. if (!pdata)
  525. return NULL;
  526. pdata->ext_dc_dc_compenstation_resistor =
  527. of_property_read_bool(np, "adi,ext-dc-dc-compenstation-resistor");
  528. if (!of_property_read_u32(np, "adi,dc-dc-phase", &tmp))
  529. pdata->dc_dc_phase = tmp;
  530. else
  531. pdata->dc_dc_phase = AD5755_DC_DC_PHASE_ALL_SAME_EDGE;
  532. pdata->dc_dc_freq = AD5755_DC_DC_FREQ_410kHZ;
  533. if (!of_property_read_u32(np, "adi,dc-dc-freq-hz", &tmp)) {
  534. for (i = 0; i < ARRAY_SIZE(ad5755_dcdc_freq_table); i++) {
  535. if (tmp == ad5755_dcdc_freq_table[i][0]) {
  536. pdata->dc_dc_freq = ad5755_dcdc_freq_table[i][1];
  537. break;
  538. }
  539. }
  540. if (i == ARRAY_SIZE(ad5755_dcdc_freq_table)) {
  541. dev_err(dev,
  542. "adi,dc-dc-freq out of range selecting 410kHz");
  543. }
  544. }
  545. pdata->dc_dc_maxv = AD5755_DC_DC_MAXV_23V;
  546. if (!of_property_read_u32(np, "adi,dc-dc-max-microvolt", &tmp)) {
  547. for (i = 0; i < ARRAY_SIZE(ad5755_dcdc_maxv_table); i++) {
  548. if (tmp == ad5755_dcdc_maxv_table[i][0]) {
  549. pdata->dc_dc_maxv = ad5755_dcdc_maxv_table[i][1];
  550. break;
  551. }
  552. }
  553. if (i == ARRAY_SIZE(ad5755_dcdc_maxv_table)) {
  554. dev_err(dev,
  555. "adi,dc-dc-maxv out of range selecting 23V");
  556. }
  557. }
  558. devnr = 0;
  559. for_each_child_of_node(np, pp) {
  560. if (devnr >= AD5755_NUM_CHANNELS) {
  561. dev_err(dev,
  562. "There is to many channels defined in DT\n");
  563. goto error_out;
  564. }
  565. if (!of_property_read_u32(pp, "adi,mode", &tmp))
  566. pdata->dac[devnr].mode = tmp;
  567. else
  568. pdata->dac[devnr].mode = AD5755_MODE_CURRENT_4mA_20mA;
  569. pdata->dac[devnr].ext_current_sense_resistor =
  570. of_property_read_bool(pp, "adi,ext-current-sense-resistor");
  571. pdata->dac[devnr].enable_voltage_overrange =
  572. of_property_read_bool(pp, "adi,enable-voltage-overrange");
  573. if (!of_property_read_u32_array(pp, "adi,slew", tmparray, 3)) {
  574. pdata->dac[devnr].slew.enable = tmparray[0];
  575. pdata->dac[devnr].slew.rate = AD5755_SLEW_RATE_64k;
  576. for (i = 0; i < ARRAY_SIZE(ad5755_slew_rate_table); i++) {
  577. if (tmparray[1] == ad5755_slew_rate_table[i][0]) {
  578. pdata->dac[devnr].slew.rate =
  579. ad5755_slew_rate_table[i][1];
  580. break;
  581. }
  582. }
  583. if (i == ARRAY_SIZE(ad5755_slew_rate_table)) {
  584. dev_err(dev,
  585. "channel %d slew rate out of range selecting 64kHz",
  586. devnr);
  587. }
  588. pdata->dac[devnr].slew.step_size = AD5755_SLEW_STEP_SIZE_1;
  589. for (i = 0; i < ARRAY_SIZE(ad5755_slew_step_table); i++) {
  590. if (tmparray[2] == ad5755_slew_step_table[i][0]) {
  591. pdata->dac[devnr].slew.step_size =
  592. ad5755_slew_step_table[i][1];
  593. break;
  594. }
  595. }
  596. if (i == ARRAY_SIZE(ad5755_slew_step_table)) {
  597. dev_err(dev,
  598. "channel %d slew step size out of range selecting 1 LSB",
  599. devnr);
  600. }
  601. } else {
  602. pdata->dac[devnr].slew.enable = false;
  603. pdata->dac[devnr].slew.rate = AD5755_SLEW_RATE_64k;
  604. pdata->dac[devnr].slew.step_size =
  605. AD5755_SLEW_STEP_SIZE_1;
  606. }
  607. devnr++;
  608. }
  609. return pdata;
  610. error_out:
  611. devm_kfree(dev, pdata);
  612. return NULL;
  613. }
  614. #else
  615. static
  616. struct ad5755_platform_data *ad5755_parse_dt(struct device *dev)
  617. {
  618. return NULL;
  619. }
  620. #endif
  621. static int ad5755_probe(struct spi_device *spi)
  622. {
  623. enum ad5755_type type = spi_get_device_id(spi)->driver_data;
  624. const struct ad5755_platform_data *pdata = dev_get_platdata(&spi->dev);
  625. struct iio_dev *indio_dev;
  626. struct ad5755_state *st;
  627. int ret;
  628. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  629. if (indio_dev == NULL) {
  630. dev_err(&spi->dev, "Failed to allocate iio device\n");
  631. return -ENOMEM;
  632. }
  633. st = iio_priv(indio_dev);
  634. spi_set_drvdata(spi, indio_dev);
  635. st->chip_info = &ad5755_chip_info_tbl[type];
  636. st->spi = spi;
  637. st->pwr_down = 0xf;
  638. indio_dev->dev.parent = &spi->dev;
  639. indio_dev->name = spi_get_device_id(spi)->name;
  640. indio_dev->info = &ad5755_info;
  641. indio_dev->modes = INDIO_DIRECT_MODE;
  642. indio_dev->num_channels = AD5755_NUM_CHANNELS;
  643. if (spi->dev.of_node)
  644. pdata = ad5755_parse_dt(&spi->dev);
  645. else
  646. pdata = spi->dev.platform_data;
  647. if (!pdata) {
  648. dev_warn(&spi->dev, "no platform data? using default\n");
  649. pdata = &ad5755_default_pdata;
  650. }
  651. ret = ad5755_init_channels(indio_dev, pdata);
  652. if (ret)
  653. return ret;
  654. ret = ad5755_setup_pdata(indio_dev, pdata);
  655. if (ret)
  656. return ret;
  657. return devm_iio_device_register(&spi->dev, indio_dev);
  658. }
  659. static const struct spi_device_id ad5755_id[] = {
  660. { "ad5755", ID_AD5755 },
  661. { "ad5755-1", ID_AD5755 },
  662. { "ad5757", ID_AD5757 },
  663. { "ad5735", ID_AD5735 },
  664. { "ad5737", ID_AD5737 },
  665. {}
  666. };
  667. MODULE_DEVICE_TABLE(spi, ad5755_id);
  668. static const struct of_device_id ad5755_of_match[] = {
  669. { .compatible = "adi,ad5755" },
  670. { .compatible = "adi,ad5755-1" },
  671. { .compatible = "adi,ad5757" },
  672. { .compatible = "adi,ad5735" },
  673. { .compatible = "adi,ad5737" },
  674. { }
  675. };
  676. MODULE_DEVICE_TABLE(of, ad5755_of_match);
  677. static struct spi_driver ad5755_driver = {
  678. .driver = {
  679. .name = "ad5755",
  680. },
  681. .probe = ad5755_probe,
  682. .id_table = ad5755_id,
  683. };
  684. module_spi_driver(ad5755_driver);
  685. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  686. MODULE_DESCRIPTION("Analog Devices AD5755/55-1/57/35/37 DAC");
  687. MODULE_LICENSE("GPL v2");